/**
* Behaviors for this model
*/
public function behaviors(){
return array(
'sluggable' => array(
'class'=>'ext.behaviors.SluggableBehavior.SluggableBehavior',
'columns' => array('name'),
'unique' => true,
'update' => true,
),
);
}
My question is, can I avoid adding this same piece of code in all my models? Can I add it somewhere where it applies to all models or to models that I specify? If so, how can it be done?
I created a file called MyActiveRecord.php in /protected/components with the following code:
<?php
class MyActiveRecord extends CActiveRecord
{
/**
* Behaviors for this model
*/
public function behaviors(){
return array(
'sluggable' => array(
'class'=>'ext.behaviors.SluggableBehavior.SluggableBehavior',
'columns' => array('name'),
'unique' => true,
'update' => true,
),
);
}
}
Then in each model I want to apply the slug behavior, I edit it and instead of extending CActiveRecord, it extends MyActiveRecord Class, which also extends CActiveRecord.
Yes basically its a good approach to extend CactiveRecord class and do the required changes in that class and change the extended class in your model class where ever you want…