Yii Framework Forum: CSerializeBehavior - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CSerializeBehavior new extension Rate Topic: -----

#1 User is offline   nsbucky 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 03-June 10

Posted 03 June 2010 - 02:14 PM

had to create this for a project I'm working on, its useful for me, don't know if someone else would find it useful. but hey beats rewriting the same stuff in the models over and over.

/**
* CSerializeBehavior allows a model to specify some attributes to be
* arrays and serialized upon save and unserialized after a Find() function
* is called on the model.
*
*<pre>
* public function behaviors()
* {
* return array(
* 'CSerializeBehavior' => array(
* 'class' => 'application.extensions.CSerializeBehavior',
* 'serialAttributes' => array('validator_options'),
* )
* );
* }
* </pre>
*
*/


then $model = new Model;
$model->example_attribute = array('fun');
$model->save();

// internally now example_attribute is saved into db as a serialized array
a:1:{i:0;s:3:"fun";}

then $model = Model::model()->find(1);
print_r($model->example_attribute);
Array
(
[0] => fun
)
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 04 June 2010 - 01:43 AM

Very nice: first post and already giving. Thanks for sharing! :)

Some notes:
  • You can upload your extension to the extension repository. But you might need some posts here in the forum first
  • Please avoid the "C" prefix for your classes. This letter is reserved for framework classes.
  • You can highlight code examples in this forum. Just select the text and click the <> icon in the editor or enclose it in [ code] ... [ /code] (without spaces in brackets)

Have fun!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users