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?
モデルの両方を拡張するクラスを作成できます。
class CommonClass extends CActiveRecord
{
public function beforeValidate(){
...
}
}
class A extends CommonClass{
}
class B extends CommonClass{
}
または、振る舞いを定義して、この振る舞いを両方のモデルに追加することもできます!
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',
),
);
}
}