12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751 |
- <?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)
- {
- if (empty($relation->via) && $relation->link) {
- foreach ($relation->link as $attribute) {
- $this->_relationsDependencies[$attribute][$name] = $name;
- }
- } elseif ($relation->via instanceof ActiveQueryInterface) {
- $this->setRelationDependencies($name, $relation->via);
- } elseif (is_array($relation->via)) {
- list(, $viaQuery) = $relation->via;
- $this->setRelationDependencies($name, $viaQuery);
- }
- }
- }
|