|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638 |
- <?php
-
-
- namespace yii\db;
-
- use yii\base\InvalidConfigException;
- use yii\base\Event;
- use yii\base\Model;
- use yii\base\InvalidParamException;
- use yii\base\ModelEvent;
- use yii\base\NotSupportedException;
- use yii\base\UnknownMethodException;
- use yii\base\InvalidCallException;
- use yii\helpers\ArrayHelper;
-
-
- abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
- {
-
-
- const EVENT_INIT = 'init';
-
-
- const EVENT_AFTER_FIND = 'afterFind';
-
-
- const EVENT_BEFORE_INSERT = 'beforeInsert';
-
-
- const EVENT_AFTER_INSERT = 'afterInsert';
-
-
- const EVENT_BEFORE_UPDATE = 'beforeUpdate';
-
-
- const EVENT_AFTER_UPDATE = 'afterUpdate';
-
-
- const EVENT_BEFORE_DELETE = 'beforeDelete';
-
-
- const EVENT_AFTER_DELETE = 'afterDelete';
-
-
- const EVENT_AFTER_REFRESH = 'afterRefresh';
-
-
-
- private $_attributes = [];
-
-
- private $_oldAttributes;
-
-
- private $_related = [];
-
-
-
-
- public static function findOne($condition)
- {
- return static::findByCondition($condition)->one();
- }
-
-
-
- public static function findAll($condition)
- {
- return static::findByCondition($condition)->all();
- }
-
-
-
- protected static function findByCondition($condition)
- {
- $query = static::find();
-
- if (!ArrayHelper::isAssociative($condition)) {
-
- $primaryKey = static::primaryKey();
- if (isset($primaryKey[0])) {
- $condition = [$primaryKey[0] => $condition];
- } else {
- throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');
- }
- }
-
- return $query->andWhere($condition);
- }
-
-
-
- public static function updateAll($attributes, $condition = '')
- {
- throw new NotSupportedException(__METHOD__ . ' is not supported.');
- }
-
-
-
- public static function updateAllCounters($counters, $condition = '')
- {
- throw new NotSupportedException(__METHOD__ . ' is not supported.');
- }
-
-
-
- public static function deleteAll($condition = '', $params = [])
- {
- throw new NotSupportedException(__METHOD__ . ' is not supported.');
- }
-
-
-
- public function optimisticLock()
- {
- return null;
- }
-
-
-
- public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
- {
- if (parent::canGetProperty($name, $checkVars, $checkBehaviors)) {
- return true;
- }
-
- try {
- return $this->hasAttribute($name);
- } catch (\Exception $e) {
-
- return false;
- }
- }
-
-
-
- public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
- {
- if (parent::canSetProperty($name, $checkVars, $checkBehaviors)) {
- return true;
- }
-
- try {
- return $this->hasAttribute($name);
- } catch (\Exception $e) {
-
- return false;
- }
- }
-
-
-
- public function __get($name)
- {
- if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) {
- return $this->_attributes[$name];
- } elseif ($this->hasAttribute($name)) {
- return null;
- } else {
- if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) {
- return $this->_related[$name];
- }
- $value = parent::__get($name);
- if ($value instanceof ActiveQueryInterface) {
- return $this->_related[$name] = $value->findFor($name, $this);
- } else {
- return $value;
- }
- }
- }
-
-
-
- public function __set($name, $value)
- {
- if ($this->hasAttribute($name)) {
- $this->_attributes[$name] = $value;
- } else {
- parent::__set($name, $value);
- }
- }
-
-
-
- public function __isset($name)
- {
- try {
- return $this->__get($name) !== null;
- } catch (\Exception $e) {
- return false;
- }
- }
-
-
-
- public function __unset($name)
- {
- if ($this->hasAttribute($name)) {
- unset($this->_attributes[$name]);
- } elseif (array_key_exists($name, $this->_related)) {
- unset($this->_related[$name]);
- } elseif ($this->getRelation($name, false) === null) {
- parent::__unset($name);
- }
- }
-
-
-
- public function hasOne($class, $link)
- {
-
-
- $query = $class::find();
- $query->primaryModel = $this;
- $query->link = $link;
- $query->multiple = false;
- return $query;
- }
-
-
-
- public function hasMany($class, $link)
- {
-
-
- $query = $class::find();
- $query->primaryModel = $this;
- $query->link = $link;
- $query->multiple = true;
- return $query;
- }
-
-
-
- public function populateRelation($name, $records)
- {
- $this->_related[$name] = $records;
- }
-
-
-
- public function isRelationPopulated($name)
- {
- return array_key_exists($name, $this->_related);
- }
-
-
-
- public function getRelatedRecords()
- {
- return $this->_related;
- }
-
-
-
- public function hasAttribute($name)
- {
- return isset($this->_attributes[$name]) || in_array($name, $this->attributes(), true);
- }
-
-
-
- public function getAttribute($name)
- {
- return isset($this->_attributes[$name]) ? $this->_attributes[$name] : null;
- }
-
-
-
- public function setAttribute($name, $value)
- {
- if ($this->hasAttribute($name)) {
- $this->_attributes[$name] = $value;
- } else {
- throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".');
- }
- }
-
-
-
- public function getOldAttributes()
- {
- return $this->_oldAttributes === null ? [] : $this->_oldAttributes;
- }
-
-
-
- public function setOldAttributes($values)
- {
- $this->_oldAttributes = $values;
- }
-
-
-
- public function getOldAttribute($name)
- {
- return isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
- }
-
-
-
- public function setOldAttribute($name, $value)
- {
- if (isset($this->_oldAttributes[$name]) || $this->hasAttribute($name)) {
- $this->_oldAttributes[$name] = $value;
- } else {
- throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".');
- }
- }
-
-
-
- public function markAttributeDirty($name)
- {
- unset($this->_oldAttributes[$name]);
- }
-
-
-
- public function isAttributeChanged($name, $identical = true)
- {
- if (isset($this->_attributes[$name], $this->_oldAttributes[$name])) {
- if ($identical) {
- return $this->_attributes[$name] !== $this->_oldAttributes[$name];
- } else {
- return $this->_attributes[$name] != $this->_oldAttributes[$name];
- }
- } else {
- return isset($this->_attributes[$name]) || isset($this->_oldAttributes[$name]);
- }
- }
-
-
-
- public function getDirtyAttributes($names = null)
- {
- if ($names === null) {
- $names = $this->attributes();
- }
- $names = array_flip($names);
- $attributes = [];
- if ($this->_oldAttributes === null) {
- foreach ($this->_attributes as $name => $value) {
- if (isset($names[$name])) {
- $attributes[$name] = $value;
- }
- }
- } else {
- foreach ($this->_attributes as $name => $value) {
- if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $value !== $this->_oldAttributes[$name])) {
- $attributes[$name] = $value;
- }
- }
- }
- return $attributes;
- }
-
-
-
- public function save($runValidation = true, $attributeNames = null)
- {
- if ($this->getIsNewRecord()) {
- return $this->insert($runValidation, $attributeNames);
- } else {
- return $this->update($runValidation, $attributeNames) !== false;
- }
- }
-
-
-
- public function update($runValidation = true, $attributeNames = null)
- {
- if ($runValidation && !$this->validate($attributeNames)) {
- return false;
- }
- return $this->updateInternal($attributeNames);
- }
-
-
-
- public function updateAttributes($attributes)
- {
- $attrs = [];
- foreach ($attributes as $name => $value) {
- if (is_int($name)) {
- $attrs[] = $value;
- } else {
- $this->$name = $value;
- $attrs[] = $name;
- }
- }
-
- $values = $this->getDirtyAttributes($attrs);
- if (empty($values) || $this->getIsNewRecord()) {
- return 0;
- }
-
- $rows = static::updateAll($values, $this->getOldPrimaryKey(true));
-
- foreach ($values as $name => $value) {
- $this->_oldAttributes[$name] = $this->_attributes[$name];
- }
-
- return $rows;
- }
-
-
-
- protected function updateInternal($attributes = null)
- {
- if (!$this->beforeSave(false)) {
- return false;
- }
- $values = $this->getDirtyAttributes($attributes);
- if (empty($values)) {
- $this->afterSave(false, $values);
- return 0;
- }
- $condition = $this->getOldPrimaryKey(true);
- $lock = $this->optimisticLock();
- if ($lock !== null) {
- $values[$lock] = $this->$lock + 1;
- $condition[$lock] = $this->$lock;
- }
-
-
- $rows = static::updateAll($values, $condition);
-
- if ($lock !== null && !$rows) {
- throw new StaleObjectException('The object being updated is outdated.');
- }
-
- if (isset($values[$lock])) {
- $this->$lock = $values[$lock];
- }
-
- $changedAttributes = [];
- foreach ($values as $name => $value) {
- $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
- $this->_oldAttributes[$name] = $value;
- }
- $this->afterSave(false, $changedAttributes);
-
- return $rows;
- }
-
-
-
- public function updateCounters($counters)
- {
- if (static::updateAllCounters($counters, $this->getOldPrimaryKey(true)) > 0) {
- foreach ($counters as $name => $value) {
- if (!isset($this->_attributes[$name])) {
- $this->_attributes[$name] = $value;
- } else {
- $this->_attributes[$name] += $value;
- }
- $this->_oldAttributes[$name] = $this->_attributes[$name];
- }
- return true;
- } else {
- return false;
- }
- }
-
-
-
- public function delete()
- {
- $result = false;
- if ($this->beforeDelete()) {
-
-
- $condition = $this->getOldPrimaryKey(true);
- $lock = $this->optimisticLock();
- if ($lock !== null) {
- $condition[$lock] = $this->$lock;
- }
- $result = static::deleteAll($condition);
- if ($lock !== null && !$result) {
- throw new StaleObjectException('The object being deleted is outdated.');
- }
- $this->_oldAttributes = null;
- $this->afterDelete();
- }
-
- return $result;
- }
-
-
-
- public function getIsNewRecord()
- {
- return $this->_oldAttributes === null;
- }
-
-
-
- public function setIsNewRecord($value)
- {
- $this->_oldAttributes = $value ? null : $this->_attributes;
- }
-
-
-
- public function init()
- {
- parent::init();
- $this->trigger(self::EVENT_INIT);
- }
-
-
-
- public function afterFind()
- {
- $this->trigger(self::EVENT_AFTER_FIND);
- }
-
-
-
- public function beforeSave($insert)
- {
- $event = new ModelEvent;
- $this->trigger($insert ? self::EVENT_BEFORE_INSERT : self::EVENT_BEFORE_UPDATE, $event);
-
- return $event->isValid;
- }
-
-
-
- public function afterSave($insert, $changedAttributes)
- {
- $this->trigger($insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE, new AfterSaveEvent([
- 'changedAttributes' => $changedAttributes,
- ]));
- }
-
-
-
- public function beforeDelete()
- {
- $event = new ModelEvent;
- $this->trigger(self::EVENT_BEFORE_DELETE, $event);
-
- return $event->isValid;
- }
-
-
-
- public function afterDelete()
- {
- $this->trigger(self::EVENT_AFTER_DELETE);
- }
-
-
-
- public function refresh()
- {
-
- $record = static::findOne($this->getPrimaryKey(true));
- if ($record === null) {
- return false;
- }
- foreach ($this->attributes() as $name) {
- $this->_attributes[$name] = isset($record->_attributes[$name]) ? $record->_attributes[$name] : null;
- }
- $this->_oldAttributes = $record->_oldAttributes;
- $this->_related = [];
- $this->afterRefresh();
-
- return true;
- }
-
-
-
- public function afterRefresh()
- {
- $this->trigger(self::EVENT_AFTER_REFRESH);
- }
-
-
-
- public function equals($record)
- {
- if ($this->getIsNewRecord() || $record->getIsNewRecord()) {
- return false;
- }
-
- return get_class($this) === get_class($record) && $this->getPrimaryKey() === $record->getPrimaryKey();
- }
-
-
-
- public function getPrimaryKey($asArray = false)
- {
- $keys = $this->primaryKey();
- if (!$asArray && count($keys) === 1) {
- return isset($this->_attributes[$keys[0]]) ? $this->_attributes[$keys[0]] : null;
- } else {
- $values = [];
- foreach ($keys as $name) {
- $values[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : null;
- }
-
- return $values;
- }
- }
-
-
-
- public function getOldPrimaryKey($asArray = false)
- {
- $keys = $this->primaryKey();
- if (empty($keys)) {
- throw new Exception(get_class($this) . ' does not have a primary key. You should either define a primary key for the corresponding table or override the primaryKey() method.');
- }
- if (!$asArray && count($keys) === 1) {
- return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
- } else {
- $values = [];
- foreach ($keys as $name) {
- $values[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
- }
-
- return $values;
- }
- }
-
-
-
- public static function populateRecord($record, $row)
- {
- $columns = array_flip($record->attributes());
- foreach ($row as $name => $value) {
- if (isset($columns[$name])) {
- $record->_attributes[$name] = $value;
- } elseif ($record->canSetProperty($name)) {
- $record->$name = $value;
- }
- }
- $record->_oldAttributes = $record->_attributes;
- }
-
-
-
- public static function instantiate($row)
- {
- return new static;
- }
-
-
-
- public function offsetExists($offset)
- {
- return $this->__isset($offset);
- }
-
-
-
- public function getRelation($name, $throwException = true)
- {
- $getter = 'get' . $name;
- try {
-
- $relation = $this->$getter();
- } catch (UnknownMethodException $e) {
- if ($throwException) {
- throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
- } else {
- return null;
- }
- }
- if (!$relation instanceof ActiveQueryInterface) {
- if ($throwException) {
- throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".');
- } else {
- return null;
- }
- }
-
- if (method_exists($this, $getter)) {
-
- $method = new \ReflectionMethod($this, $getter);
- $realName = lcfirst(substr($method->getName(), 3));
- if ($realName !== $name) {
- if ($throwException) {
- throw new InvalidParamException('Relation names are case sensitive. ' . get_class($this) . " has a relation named \"$realName\" instead of \"$name\".");
- } else {
- return null;
- }
- }
- }
-
- return $relation;
- }
-
-
-
- public function link($name, $model, $extraColumns = [])
- {
- $relation = $this->getRelation($name);
-
- if ($relation->via !== null) {
- if ($this->getIsNewRecord() || $model->getIsNewRecord()) {
- throw new InvalidCallException('Unable to link models: the models being linked cannot be newly created.');
- }
- if (is_array($relation->via)) {
-
- list($viaName, $viaRelation) = $relation->via;
- $viaClass = $viaRelation->modelClass;
-
- unset($this->_related[$viaName]);
- } else {
- $viaRelation = $relation->via;
- $viaTable = reset($relation->via->from);
- }
- $columns = [];
- foreach ($viaRelation->link as $a => $b) {
- $columns[$a] = $this->$b;
- }
- foreach ($relation->link as $a => $b) {
- $columns[$b] = $model->$a;
- }
- foreach ($extraColumns as $k => $v) {
- $columns[$k] = $v;
- }
- if (is_array($relation->via)) {
-
-
- $record = new $viaClass();
- foreach ($columns as $column => $value) {
- $record->$column = $value;
- }
- $record->insert(false);
- } else {
-
- static::getDb()->createCommand()
- ->insert($viaTable, $columns)->execute();
- }
- } else {
- $p1 = $model->isPrimaryKey(array_keys($relation->link));
- $p2 = static::isPrimaryKey(array_values($relation->link));
- if ($p1 && $p2) {
- if ($this->getIsNewRecord() && $model->getIsNewRecord()) {
- throw new InvalidCallException('Unable to link models: at most one model can be newly created.');
- } elseif ($this->getIsNewRecord()) {
- $this->bindModels(array_flip($relation->link), $this, $model);
- } else {
- $this->bindModels($relation->link, $model, $this);
- }
- } elseif ($p1) {
- $this->bindModels(array_flip($relation->link), $this, $model);
- } elseif ($p2) {
- $this->bindModels($relation->link, $model, $this);
- } else {
- throw new InvalidCallException('Unable to link models: the link defining the relation does not involve any primary key.');
- }
- }
-
-
- if (!$relation->multiple) {
- $this->_related[$name] = $model;
- } elseif (isset($this->_related[$name])) {
- if ($relation->indexBy !== null) {
- if ($relation->indexBy instanceof \Closure) {
- $index = call_user_func($relation->indexBy, $model);
- } else {
- $index = $model->{$relation->indexBy};
- }
- $this->_related[$name][$index] = $model;
- } else {
- $this->_related[$name][] = $model;
- }
- }
- }
-
-
-
- public function unlink($name, $model, $delete = false)
- {
- $relation = $this->getRelation($name);
-
- if ($relation->via !== null) {
- if (is_array($relation->via)) {
-
- list($viaName, $viaRelation) = $relation->via;
- $viaClass = $viaRelation->modelClass;
- unset($this->_related[$viaName]);
- } else {
- $viaRelation = $relation->via;
- $viaTable = reset($relation->via->from);
- }
- $columns = [];
- foreach ($viaRelation->link as $a => $b) {
- $columns[$a] = $this->$b;
- }
- foreach ($relation->link as $a => $b) {
- $columns[$b] = $model->$a;
- }
- $nulls = [];
- foreach (array_keys($columns) as $a) {
- $nulls[$a] = null;
- }
- if (is_array($relation->via)) {
-
- if ($delete) {
- $viaClass::deleteAll($columns);
- } else {
- $viaClass::updateAll($nulls, $columns);
- }
- } else {
-
-
- $command = static::getDb()->createCommand();
- if ($delete) {
- $command->delete($viaTable, $columns)->execute();
- } else {
- $command->update($viaTable, $nulls, $columns)->execute();
- }
- }
- } else {
- $p1 = $model->isPrimaryKey(array_keys($relation->link));
- $p2 = static::isPrimaryKey(array_values($relation->link));
- if ($p2) {
- if ($delete) {
- $model->delete();
- } else {
- foreach ($relation->link as $a => $b) {
- $model->$a = null;
- }
- $model->save(false);
- }
- } elseif ($p1) {
- foreach ($relation->link as $a => $b) {
- if (is_array($this->$b)) {
- if (($key = array_search($model->$a, $this->$b, false)) !== false) {
- $values = $this->$b;
- unset($values[$key]);
- $this->$b = array_values($values);
- }
- } else {
- $this->$b = null;
- }
- }
- $delete ? $this->delete() : $this->save(false);
- } else {
- throw new InvalidCallException('Unable to unlink models: the link does not involve any primary key.');
- }
- }
-
- if (!$relation->multiple) {
- unset($this->_related[$name]);
- } elseif (isset($this->_related[$name])) {
-
- foreach ($this->_related[$name] as $a => $b) {
- if ($model->getPrimaryKey() === $b->getPrimaryKey()) {
- unset($this->_related[$name][$a]);
- }
- }
- }
- }
-
-
-
- public function unlinkAll($name, $delete = false)
- {
- $relation = $this->getRelation($name);
-
- if ($relation->via !== null) {
- if (is_array($relation->via)) {
-
- list($viaName, $viaRelation) = $relation->via;
- $viaClass = $viaRelation->modelClass;
- unset($this->_related[$viaName]);
- } else {
- $viaRelation = $relation->via;
- $viaTable = reset($relation->via->from);
- }
- $condition = [];
- $nulls = [];
- foreach ($viaRelation->link as $a => $b) {
- $nulls[$a] = null;
- $condition[$a] = $this->$b;
- }
- if (!empty($viaRelation->where)) {
- $condition = ['and', $condition, $viaRelation->where];
- }
- if (is_array($relation->via)) {
-
- if ($delete) {
- $viaClass::deleteAll($condition);
- } else {
- $viaClass::updateAll($nulls, $condition);
- }
- } else {
-
-
- $command = static::getDb()->createCommand();
- if ($delete) {
- $command->delete($viaTable, $condition)->execute();
- } else {
- $command->update($viaTable, $nulls, $condition)->execute();
- }
- }
- } else {
-
- $relatedModel = $relation->modelClass;
- if (!$delete && count($relation->link) === 1 && is_array($this->{$b = reset($relation->link)})) {
-
- $this->$b = [];
- $this->save(false);
- } else {
- $nulls = [];
- $condition = [];
- foreach ($relation->link as $a => $b) {
- $nulls[$a] = null;
- $condition[$a] = $this->$b;
- }
- if (!empty($relation->where)) {
- $condition = ['and', $condition, $relation->where];
- }
- if ($delete) {
- $relatedModel::deleteAll($condition);
- } else {
- $relatedModel::updateAll($nulls, $condition);
- }
- }
- }
-
- unset($this->_related[$name]);
- }
-
-
-
- private function bindModels($link, $foreignModel, $primaryModel)
- {
- foreach ($link as $fk => $pk) {
- $value = $primaryModel->$pk;
- if ($value === null) {
- throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');
- }
- if (is_array($foreignModel->$fk)) {
- $foreignModel->$fk = array_merge($foreignModel->$fk, [$value]);
- } else {
- $foreignModel->$fk = $value;
- }
- }
- $foreignModel->save(false);
- }
-
-
-
- public static function isPrimaryKey($keys)
- {
- $pks = static::primaryKey();
- if (count($keys) === count($pks)) {
- return count(array_intersect($keys, $pks)) === count($pks);
- } else {
- return false;
- }
- }
-
-
-
- public function getAttributeLabel($attribute)
- {
- $labels = $this->attributeLabels();
- if (isset($labels[$attribute])) {
- return $labels[$attribute];
- } elseif (strpos($attribute, '.')) {
- $attributeParts = explode('.', $attribute);
- $neededAttribute = array_pop($attributeParts);
-
- $relatedModel = $this;
- foreach ($attributeParts as $relationName) {
- if ($relatedModel->isRelationPopulated($relationName) && $relatedModel->$relationName instanceof self) {
- $relatedModel = $relatedModel->$relationName;
- } else {
- try {
- $relation = $relatedModel->getRelation($relationName);
- } catch (InvalidParamException $e) {
- return $this->generateAttributeLabel($attribute);
- }
- $relatedModel = new $relation->modelClass;
- }
- }
-
- $labels = $relatedModel->attributeLabels();
- if (isset($labels[$neededAttribute])) {
- return $labels[$neededAttribute];
- }
- }
-
- return $this->generateAttributeLabel($attribute);
- }
-
-
-
- public function getAttributeHint($attribute)
- {
- $hints = $this->attributeHints();
- if (isset($hints[$attribute])) {
- return $hints[$attribute];
- } elseif (strpos($attribute, '.')) {
- $attributeParts = explode('.', $attribute);
- $neededAttribute = array_pop($attributeParts);
-
- $relatedModel = $this;
- foreach ($attributeParts as $relationName) {
- if ($relatedModel->isRelationPopulated($relationName) && $relatedModel->$relationName instanceof self) {
- $relatedModel = $relatedModel->$relationName;
- } else {
- try {
- $relation = $relatedModel->getRelation($relationName);
- } catch (InvalidParamException $e) {
- return '';
- }
- $relatedModel = new $relation->modelClass;
- }
- }
-
- $hints = $relatedModel->attributeHints();
- if (isset($hints[$neededAttribute])) {
- return $hints[$neededAttribute];
- }
- }
- return '';
- }
-
-
-
- public function fields()
- {
- $fields = array_keys($this->_attributes);
-
- return array_combine($fields, $fields);
- }
-
-
-
- public function extraFields()
- {
- $fields = array_keys($this->getRelatedRecords());
-
- return array_combine($fields, $fields);
- }
-
-
-
- public function offsetUnset($offset)
- {
- if (property_exists($this, $offset)) {
- $this->$offset = null;
- } else {
- unset($this->$offset);
- }
- }
- }
|