CSerializeBehavior

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' =&gt; array(
    
  •  		'class' =&gt; 'application.extensions.CSerializeBehavior',
    
  •  		'serialAttributes' =&gt; 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] =&gt; fun

)

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!