0

I'm using Datamapper ORM for CodeIgniter I have rules 'serialized' and get_rules 'unserialized' for a field in my model. This field will store serialized data and when I retrieve back, get_rules will unserialize it.

However, after calling save(), I'm trying to re-access the field, but it still return serialized string, instead of array.

Is there any way to re-call or refresh my object so that the get_rules is called again and the field now return array?

Here's my model:

class User extends DataMapper{
  public $validation = array(
    'password' => array(
      'label' => 'Password',
      'rules' => array('encrypt')
    ),
    'preferences' => array(
      'rules' => array('serialize'),
      'get_rules'=> array('unserialize')
    )
  );

  function __construct($id = NULL)
  {
    parent::__construct($id);
  }

  function post_model_init($from_cache = FALSE)
  {
  }

  public function _encrypt($field)
  {
    if (!empty($this->{$field}))
    {
      $this->{$field} = md5($this->{$field});
    }
  }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Ricky L
  • 23
  • 4

2 Answers2

0

Datamapper ORM, afaik, will only use the get_rules when actually performing a get(). A few things you could try:

Given the following

$a = new Fruit();
$a->name = 'grapes';
$a->colors = serialize(array("purple","green"));
$a->save();

1. Create a new datamapper object and re-fetch:

$b = new Fruit();
$b->where('id', $a->id)->get();
$colors = $b->colors;

2. unserialize() the field yourself...

$colors = unserialize($a->colors);

3. You might even be able to use get_clone()

//not tested...
$b = $a->get_clone();
$colors = $b->colors;
Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • hi Jordan, thanks for your answer. However my point is to simplify the controller, so I want to put all post-process in Model instead of Controller. Currently, I use your suggestion #1, but I think it will have less performance. Somehow I still think that get_rules after save() would be the ideal way. – Ricky L Sep 17 '12 at 08:36
  • You're right #1 is a performance hit. #2 would be the best from a performance perspective; internal DM code would have to run `unserialize()` anyway. Another option for you is to [write an extension](http://datamapper.wanwizard.eu/pages/extwrite.html). In any case, if you have a feature request, Wanwizard is very active in the [DM ORM CI Forum](http://codeigniter.com/forums/viewthread/205637/). – Jordan Arsenault Sep 17 '12 at 18:42
  • Thanks! I just write a post in the forum. Will wait for Wanwizard answers :) – Ricky L Sep 18 '12 at 04:23
0

This has been fixed here: https://bitbucket.org/wanwizard/datamapper/commits/db6ad5f2e10650b0c00c8ef9b7176d49a8e85163

Get the latest Datamapper library from bitbucket.

WanWizard
  • 2,574
  • 15
  • 13