composite-model Combine several models into one model

  1. Requirements
  2. Usage

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;
        }
}
1 0
5 followers
1 741 downloads
Yii Version: Unknown
License: BSD-2-Clause
Category: Others
Developed by: Victor Porton
Created on: Aug 16, 2013
Last updated: 11 years ago

Downloads

show all

Related Extensions