Example:
models / base / BaseUser.php - standard class that is generated via Gii
models / User.php - a class that inherits from BaseUser and has a model () method in it
/** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return User the static model class */ public static function model($className=__CLASS__) { return parent::model($className); }
'components' => array( // ... 'modelRegistry'=>array( 'class' => 'ModelRegistry' ) // ... )
class ModelRegistry { protected $registries = array(); public function init() {} /** * * @param string $name * @param mixed $attr * @return ExtActiveRecord */ public function & registry($name, $attr = array()) { $key = $name . md5(serialize($attr)); if (!isset($this->registries[$key])) { $model = ucfirst($name); $obj = $model::model(); if (!is_array($attr)) $attr = array($attr); $this->registries[$key] = call_user_func_array(array(&$obj, 'registry'), $attr); } // & return $this->registries[$key]; } /** * */ public function saveAll() { foreach ($this->registries as $obj) { $obj->save(); } } }
/** * @property integer $id * @property integer $exp * @property integer $money * @property integer $name */ class User extends BaseUser { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return User the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * * @param int $userID * @return User|bool */ public function registry($userID) { if ($obj = $this->findByPk($userID)) { $res = $obj; } else { $res = false; } return $res; } }
/** * @var ModelRegistry */ protected $reg; public function actionRun() { $userID = 1; $this->reg = &Yii::app()->modelRegistry; $this->firstChange($userID); $this->secondChange($userID); $this->reg->saveAll(); } public function firstChange($userID) { // . // & , $user = &$this->reg->registry('user', $userID); $user->exp = 10; } public function secondChange($userID) { // , . // & $user = &$this->reg->registry('user', $userID); $user->money = 20; }
class ExtActiveRecord extends CActiveRecord { protected $_oldAttributes = array(); /** * , */ public function registry() {} /** * */ public function memoryAttributes() { $this->_oldAttributes = $this->attributes; } /** * . * , false * @return array|false */ protected function getChanges() { $res = array(); if (empty($this->_oldAttributes)) { $res = false; } else { foreach ($this->_oldAttributes as $key => $value) { if ($this->$key != $value) { $res[] = $key; } } } return $res; } /** * * @return bool */ public function save() { if (($attr = $this->getChanges()) === false) { $res = parent::save(); } elseif ($attr) { $res = $this->update($attr); } else { $res = false; } return $res; } }
public function registry($userID) { if ($obj = $this->findByPk($userID)) { $res = $obj; } else { $res = false; } if ($res) { // $res->memoryAttributes(); } return $res; }
class ModelList { /** * @var array ExtActiveRecord */ public $list = array(); /** * * @param array|bool $list ExtActiveRecord * @return ModelList */ public static function make($list = array()) { if (!is_array($list) && empty($list)) { $list = array(); } $obj = new ModelList(); $obj->list = $list; return $obj; } /** * * @param ExtActiveRecord $obj */ public function pushObject($obj) { $this->list[] = $obj; } /** * * @param string $name */ public function callMethod($name) { foreach ($this->list as &$obj) { $obj->$name(); } } /** * */ public function save() { $this->callMethod('save'); } }
<?php /** * @property integer $id * @property integer $user_id * @property integer $car_id * @property integer $speed */ class Car extends BaseCar { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return Car the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * * @param int $userID * @return ModelList */ public function registry($userID) { $list = $this->findAllByAttributes(array('user_id'=>$userID)); $res = ModelList::make($list); // $res->callMethod('memoryAttributes'); return $res; } /** * , . , ModelList pushObject * @param int $userID * @param int $carID * @return Car */ public static function make($userID, $carID) { $obj = new Car(); $obj->user_id = $userID; $obj->car_id = $dict->area_id; $obj->speed = 10; return $obj; } }
$carList = &Yii::app()->modelRegistry->registry('car', 1);
Source: https://habr.com/ru/post/177181/
All Articles