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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

1 Answers1

3

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',
        ),      
    );
}
}
darkheir
  • 8,844
  • 6
  • 45
  • 66