This extension allows you to treat model attributes as arrays, and they are automatically serialized for insertion and unserialized upon select, as long as you use the active record find methods.
Requirements ¶
Yii 1.1 or above
Usage ¶
Just add the behavior to your behaviors list in your model (after placing the behavior in your app.behaviors folder)
In your model:
public function behaviors()
{
return array(
'CSerializeBehavior' => array(
'class' => 'application.behaviors.CSerializeBehavior',
'serialAttributes' => array('attribute_to_serialize'), // name of attribute(s)
)
);
}
And then you can use your attributes like this:
$m = new MyModel;
$m->attribute_to_serialize = array(...);
$m->save();
$m = MyModel::model()->findByPk(...);
$a = $m->attribute_to_serialize;
echo is_array($a); // should be true
serialize everything!
First of all nice and useful extension.
But in current version there is the capability to only serialize arrays otherwise you get null in return.
I had to do a refactor of your source code to enable the serialization for every serializable value!
And I am sharing it here:
link: serialize behavior
Because the use the prefix "C" is reserved from yii I have fixed that also. So now is SerializeBehavior instead of CSerializeBehavior.
Please let me know what you think and then merge and update this extension accordingly.
Once more it is a useful solution and a nice extesion :)
only arrays
I made this back when I was starting with YII, its easy enough to change the C to an E if you want. I would suggest just keeping it to arrays, as if you are serializing objects, when you unserialize it you have to make sure that class definition exists, or you get errors.
Your download/viewing link doesn't work, can you repost?
imho better not use a prefix at all
It's an upload on the multiple servers that multiupload.nl supports.
Seems to be working fine for me.
But either ways I have an extension which is now depended from SerializeBehavior.
So you may go to my extension: dynamic-redirection
and download the file models_v2.zip
This zip contains only three files. You will find it in there :)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.