1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756 |
- <?php
- namespace yii\db;
- use Yii;
- use yii\base\InvalidArgumentException;
- use yii\base\InvalidCallException;
- use yii\base\InvalidConfigException;
- use yii\base\InvalidParamException;
- use yii\base\Model;
- use yii\base\ModelEvent;
- use yii\base\NotSupportedException;
- use yii\base\UnknownMethodException;
- 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 = [];
-
- private $_relationsDependencies = [];
-
- 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] => is_array($condition) ? array_values($condition) : $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 = null)
- {
- 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];
- }
- if ($this->hasAttribute($name)) {
- return null;
- }
- if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) {
- return $this->_related[$name];
- }
- $value = parent::__get($name);
- if ($value instanceof ActiveQueryInterface) {
- $this->setRelationDependencies($name, $value);
- return $this->_related[$name] = $value->findFor($name, $this);
- }
- return $value;
- }
-
- public function __set($name, $value)
- {
- if ($this->hasAttribute($name)) {
- if (
- !empty($this->_relationsDependencies[$name])
- && (!array_key_exists($name, $this->_attributes) || $this->_attributes[$name] !== $value)
- ) {
- $this->resetDependentRelations($name);
- }
- $this->_attributes[$name] = $value;
- } else {
- parent::__set($name, $value);
- }
- }
-
- public function __isset($name)
- {
- try {
- return $this->__get($name) !== null;
- } catch (\Throwable $t) {
- return false;
- } catch (\Exception $e) {
- return false;
- }
- }
-
- public function __unset($name)
- {
- if ($this->hasAttribute($name)) {
- unset($this->_attributes[$name]);
- if (!empty($this->_relationsDependencies[$name])) {
- $this->resetDependentRelations($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)
- {
- return $this->createRelationQuery($class, $link, false);
- }
-
- public function hasMany($class, $link)
- {
- return $this->createRelationQuery($class, $link, true);
- }
-
- protected function createRelationQuery($class, $link, $multiple)
- {
-
-
- $query = $class::find();
- $query->primaryModel = $this;
- $query->link = $link;
- $query->multiple = $multiple;
- return $query;
- }
-
- public function populateRelation($name, $records)
- {
- foreach ($this->_relationsDependencies as &$relationNames) {
- unset($relationNames[$name]);
- }
- $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)) {
- if (
- !empty($this->_relationsDependencies[$name])
- && (!array_key_exists($name, $this->_attributes) || $this->_attributes[$name] !== $value)
- ) {
- $this->resetDependentRelations($name);
- }
- $this->_attributes[$name] = $value;
- } else {
- throw new InvalidArgumentException(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 InvalidArgumentException(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];
- }
- return $this->_attributes[$name] != $this->_oldAttributes[$name];
- }
- 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);
- }
- 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;
- }
- 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));
- return $this->refreshInternal($record);
- }
-
- protected function refreshInternal($record)
- {
- 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->_relationsDependencies = [];
- $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;
- }
- $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;
- }
- $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;
- $record->_related = [];
- $record->_relationsDependencies = [];
- }
-
- 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 InvalidArgumentException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
- }
- return null;
- }
- if (!$relation instanceof ActiveQueryInterface) {
- if ($throwException) {
- throw new InvalidArgumentException(get_class($this) . ' has no relation named "' . $name . '".');
- }
- 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 InvalidArgumentException('Relation names are case sensitive. ' . get_class($this) . " has a relation named \"$realName\" instead of \"$name\".");
- }
- 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 = Yii::createObject($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 (!empty($viaRelation->on)) {
- $condition = ['and', $condition, $viaRelation->on];
- }
- 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 (!empty($relation->on)) {
- $condition = ['and', $condition, $relation->on];
- }
- 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}[] = $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);
- }
- 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);
- }
-
- $modelClass = $relation->modelClass;
- $relatedModel = $modelClass::instance();
- }
- }
- $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 '';
- }
-
- $modelClass = $relation->modelClass;
- $relatedModel = $modelClass::instance();
- }
- }
- $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);
- }
- }
-
- private function resetDependentRelations($attribute)
- {
- foreach ($this->_relationsDependencies[$attribute] as $relation) {
- unset($this->_related[$relation]);
- }
- unset($this->_relationsDependencies[$attribute]);
- }
-
- private function setRelationDependencies($name, $relation, $viaRelationName = null)
- {
- if (empty($relation->via) && $relation->link) {
- foreach ($relation->link as $attribute) {
- $this->_relationsDependencies[$attribute][$name] = $name;
- if ($viaRelationName !== null) {
- $this->_relationsDependencies[$attribute][] = $viaRelationName;
- }
- }
- } elseif ($relation->via instanceof ActiveQueryInterface) {
- $this->setRelationDependencies($name, $relation->via);
- } elseif (is_array($relation->via)) {
- list($viaRelationName, $viaQuery) = $relation->via;
- $this->setRelationDependencies($name, $viaQuery, $viaRelationName);
- }
- }
- }
|