I have a Yii app, and within 2 of the models, I have the code for the beforeValidation method.
Does yii have a solution for this, or should I create a component and use parameters for this common code?
I have a Yii app, and within 2 of the models, I have the code for the beforeValidation method.
Does yii have a solution for this, or should I create a component and use parameters for this common code?
You could create a class which both of your model extends:
class CommonClass extends CActiveRecord
{
public function beforeValidate(){
...
}
}
class A extends CommonClass{
}
class B extends CommonClass{
}
Or you could define a behavior and add this behavior to both of your models!
class YourBehavior extends CActiveRecordBehavior
{
public function beforeValidate($event)
{
...
}
}
class A extends CActiveRecord{
public function behaviors(){
return array(
'YourBehavior' => array(
'class' => 'components.YourBehavior',
),
);
}
}
class B extends CActiveRecord{
public function behaviors(){
return array(
'YourBehavior' => array(
'class' => 'components.YourBehavior',
),
);
}
}