0

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?

4

1 に答える 1

3

モデルの両方を拡張するクラスを作成できます。

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',
        ),      
    );
}
}
于 2013-03-29T10:38:21.670 に答える