composite-model

Combine several models into one model
5 followers

Instances of my class ECompositeModel contain several models and are models themselves. The set of attributes of an ECompositeModel instance is the union of the sets of attributes of all contained models.

It can be used for instance to edit data spread among several database tables (and several active records) in one active form.

Requirements

Yii 1.1 or above

Usage

Example:

Yii::import('application.extensions.ECompositeModel');
 
class EmployeeWithPerson extends ECompositeModel {
        public function __construct($employee, $scenario=null) {
        if(!is_object($employee)) {
            $scenario = $employee;
            $employee = new Employee($scenario);
        }
        if(!$employee->person) $employee->person = new Person($scenario);
        parent::__construct(array($employee->person, $employee), $scenario);
    }
 
    protected function beforeItemSave($i) {
        if($i == 1)
            $this->models[1]->personid = $this->models[0]->id;
    }
 
        function search() {
                $dataProvider = $this->models[1]->search();
                $criteria = $dataProvider->criteria;
                $criteria->compare('email',$this->email,true);
                $criteria->compare('firstnameru',$this->firstnameru,true);
                $criteria->compare('firstnamehe',$this->firstnamehe,true);
                $criteria->compare('lastnameru',$this->lastnameru,true);
                $criteria->compare('lastnamehe',$this->lastnamehe,true);
                return $dataProvider;
        }
}

Be the first person to leave a comment

Please to leave your comment.

Create extension