BaseActiveRecord.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use Yii;
  9. use yii\base\InvalidArgumentException;
  10. use yii\base\InvalidCallException;
  11. use yii\base\InvalidConfigException;
  12. use yii\base\InvalidParamException;
  13. use yii\base\Model;
  14. use yii\base\ModelEvent;
  15. use yii\base\NotSupportedException;
  16. use yii\base\UnknownMethodException;
  17. use yii\helpers\ArrayHelper;
  18. /**
  19. * ActiveRecord is the base class for classes representing relational data in terms of objects.
  20. *
  21. * See [[\yii\db\ActiveRecord]] for a concrete implementation.
  22. *
  23. * @property array $dirtyAttributes The changed attribute values (name-value pairs). This property is
  24. * read-only.
  25. * @property bool $isNewRecord Whether the record is new and should be inserted when calling [[save()]].
  26. * @property array $oldAttributes The old attribute values (name-value pairs). Note that the type of this
  27. * property differs in getter and setter. See [[getOldAttributes()]] and [[setOldAttributes()]] for details.
  28. * @property mixed $oldPrimaryKey The old primary key value. An array (column name => column value) is
  29. * returned if the primary key is composite. A string is returned otherwise (null will be returned if the key
  30. * value is null). This property is read-only.
  31. * @property mixed $primaryKey The primary key value. An array (column name => column value) is returned if
  32. * the primary key is composite. A string is returned otherwise (null will be returned if the key value is null).
  33. * This property is read-only.
  34. * @property array $relatedRecords An array of related records indexed by relation names. This property is
  35. * read-only.
  36. *
  37. * @author Qiang Xue <qiang.xue@gmail.com>
  38. * @author Carsten Brandt <mail@cebe.cc>
  39. * @since 2.0
  40. */
  41. abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
  42. {
  43. /**
  44. * @event Event an event that is triggered when the record is initialized via [[init()]].
  45. */
  46. const EVENT_INIT = 'init';
  47. /**
  48. * @event Event an event that is triggered after the record is created and populated with query result.
  49. */
  50. const EVENT_AFTER_FIND = 'afterFind';
  51. /**
  52. * @event ModelEvent an event that is triggered before inserting a record.
  53. * You may set [[ModelEvent::isValid]] to be `false` to stop the insertion.
  54. */
  55. const EVENT_BEFORE_INSERT = 'beforeInsert';
  56. /**
  57. * @event AfterSaveEvent an event that is triggered after a record is inserted.
  58. */
  59. const EVENT_AFTER_INSERT = 'afterInsert';
  60. /**
  61. * @event ModelEvent an event that is triggered before updating a record.
  62. * You may set [[ModelEvent::isValid]] to be `false` to stop the update.
  63. */
  64. const EVENT_BEFORE_UPDATE = 'beforeUpdate';
  65. /**
  66. * @event AfterSaveEvent an event that is triggered after a record is updated.
  67. */
  68. const EVENT_AFTER_UPDATE = 'afterUpdate';
  69. /**
  70. * @event ModelEvent an event that is triggered before deleting a record.
  71. * You may set [[ModelEvent::isValid]] to be `false` to stop the deletion.
  72. */
  73. const EVENT_BEFORE_DELETE = 'beforeDelete';
  74. /**
  75. * @event Event an event that is triggered after a record is deleted.
  76. */
  77. const EVENT_AFTER_DELETE = 'afterDelete';
  78. /**
  79. * @event Event an event that is triggered after a record is refreshed.
  80. * @since 2.0.8
  81. */
  82. const EVENT_AFTER_REFRESH = 'afterRefresh';
  83. /**
  84. * @var array attribute values indexed by attribute names
  85. */
  86. private $_attributes = [];
  87. /**
  88. * @var array|null old attribute values indexed by attribute names.
  89. * This is `null` if the record [[isNewRecord|is new]].
  90. */
  91. private $_oldAttributes;
  92. /**
  93. * @var array related models indexed by the relation names
  94. */
  95. private $_related = [];
  96. /**
  97. * @var array relation names indexed by their link attributes
  98. */
  99. private $_relationsDependencies = [];
  100. /**
  101. * {@inheritdoc}
  102. * @return static|null ActiveRecord instance matching the condition, or `null` if nothing matches.
  103. */
  104. public static function findOne($condition)
  105. {
  106. return static::findByCondition($condition)->one();
  107. }
  108. /**
  109. * {@inheritdoc}
  110. * @return static[] an array of ActiveRecord instances, or an empty array if nothing matches.
  111. */
  112. public static function findAll($condition)
  113. {
  114. return static::findByCondition($condition)->all();
  115. }
  116. /**
  117. * Finds ActiveRecord instance(s) by the given condition.
  118. * This method is internally called by [[findOne()]] and [[findAll()]].
  119. * @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
  120. * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance.
  121. * @throws InvalidConfigException if there is no primary key defined
  122. * @internal
  123. */
  124. protected static function findByCondition($condition)
  125. {
  126. $query = static::find();
  127. if (!ArrayHelper::isAssociative($condition)) {
  128. // query by primary key
  129. $primaryKey = static::primaryKey();
  130. if (isset($primaryKey[0])) {
  131. // if condition is scalar, search for a single primary key, if it is array, search for multiple primary key values
  132. $condition = [$primaryKey[0] => is_array($condition) ? array_values($condition) : $condition];
  133. } else {
  134. throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');
  135. }
  136. }
  137. return $query->andWhere($condition);
  138. }
  139. /**
  140. * Updates the whole table using the provided attribute values and conditions.
  141. *
  142. * For example, to change the status to be 1 for all customers whose status is 2:
  143. *
  144. * ```php
  145. * Customer::updateAll(['status' => 1], 'status = 2');
  146. * ```
  147. *
  148. * @param array $attributes attribute values (name-value pairs) to be saved into the table
  149. * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
  150. * Please refer to [[Query::where()]] on how to specify this parameter.
  151. * @return int the number of rows updated
  152. * @throws NotSupportedException if not overridden
  153. */
  154. public static function updateAll($attributes, $condition = '')
  155. {
  156. throw new NotSupportedException(__METHOD__ . ' is not supported.');
  157. }
  158. /**
  159. * Updates the whole table using the provided counter changes and conditions.
  160. *
  161. * For example, to increment all customers' age by 1,
  162. *
  163. * ```php
  164. * Customer::updateAllCounters(['age' => 1]);
  165. * ```
  166. *
  167. * @param array $counters the counters to be updated (attribute name => increment value).
  168. * Use negative values if you want to decrement the counters.
  169. * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
  170. * Please refer to [[Query::where()]] on how to specify this parameter.
  171. * @return int the number of rows updated
  172. * @throws NotSupportedException if not overrided
  173. */
  174. public static function updateAllCounters($counters, $condition = '')
  175. {
  176. throw new NotSupportedException(__METHOD__ . ' is not supported.');
  177. }
  178. /**
  179. * Deletes rows in the table using the provided conditions.
  180. * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
  181. *
  182. * For example, to delete all customers whose status is 3:
  183. *
  184. * ```php
  185. * Customer::deleteAll('status = 3');
  186. * ```
  187. *
  188. * @param string|array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
  189. * Please refer to [[Query::where()]] on how to specify this parameter.
  190. * @return int the number of rows deleted
  191. * @throws NotSupportedException if not overridden.
  192. */
  193. public static function deleteAll($condition = null)
  194. {
  195. throw new NotSupportedException(__METHOD__ . ' is not supported.');
  196. }
  197. /**
  198. * Returns the name of the column that stores the lock version for implementing optimistic locking.
  199. *
  200. * Optimistic locking allows multiple users to access the same record for edits and avoids
  201. * potential conflicts. In case when a user attempts to save the record upon some staled data
  202. * (because another user has modified the data), a [[StaleObjectException]] exception will be thrown,
  203. * and the update or deletion is skipped.
  204. *
  205. * Optimistic locking is only supported by [[update()]] and [[delete()]].
  206. *
  207. * To use Optimistic locking:
  208. *
  209. * 1. Create a column to store the version number of each row. The column type should be `BIGINT DEFAULT 0`.
  210. * Override this method to return the name of this column.
  211. * 2. Ensure the version value is submitted and loaded to your model before any update or delete.
  212. * Or add [[\yii\behaviors\OptimisticLockBehavior|OptimisticLockBehavior]] to your model
  213. * class in order to automate the process.
  214. * 3. In the Web form that collects the user input, add a hidden field that stores
  215. * the lock version of the recording being updated.
  216. * 4. In the controller action that does the data updating, try to catch the [[StaleObjectException]]
  217. * and implement necessary business logic (e.g. merging the changes, prompting stated data)
  218. * to resolve the conflict.
  219. *
  220. * @return string the column name that stores the lock version of a table row.
  221. * If `null` is returned (default implemented), optimistic locking will not be supported.
  222. */
  223. public function optimisticLock()
  224. {
  225. return null;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
  231. {
  232. if (parent::canGetProperty($name, $checkVars, $checkBehaviors)) {
  233. return true;
  234. }
  235. try {
  236. return $this->hasAttribute($name);
  237. } catch (\Exception $e) {
  238. // `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used
  239. return false;
  240. }
  241. }
  242. /**
  243. * {@inheritdoc}
  244. */
  245. public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
  246. {
  247. if (parent::canSetProperty($name, $checkVars, $checkBehaviors)) {
  248. return true;
  249. }
  250. try {
  251. return $this->hasAttribute($name);
  252. } catch (\Exception $e) {
  253. // `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used
  254. return false;
  255. }
  256. }
  257. /**
  258. * PHP getter magic method.
  259. * This method is overridden so that attributes and related objects can be accessed like properties.
  260. *
  261. * @param string $name property name
  262. * @throws InvalidArgumentException if relation name is wrong
  263. * @return mixed property value
  264. * @see getAttribute()
  265. */
  266. public function __get($name)
  267. {
  268. if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) {
  269. return $this->_attributes[$name];
  270. }
  271. if ($this->hasAttribute($name)) {
  272. return null;
  273. }
  274. if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) {
  275. return $this->_related[$name];
  276. }
  277. $value = parent::__get($name);
  278. if ($value instanceof ActiveQueryInterface) {
  279. $this->setRelationDependencies($name, $value);
  280. return $this->_related[$name] = $value->findFor($name, $this);
  281. }
  282. return $value;
  283. }
  284. /**
  285. * PHP setter magic method.
  286. * This method is overridden so that AR attributes can be accessed like properties.
  287. * @param string $name property name
  288. * @param mixed $value property value
  289. */
  290. public function __set($name, $value)
  291. {
  292. if ($this->hasAttribute($name)) {
  293. if (
  294. !empty($this->_relationsDependencies[$name])
  295. && (!array_key_exists($name, $this->_attributes) || $this->_attributes[$name] !== $value)
  296. ) {
  297. $this->resetDependentRelations($name);
  298. }
  299. $this->_attributes[$name] = $value;
  300. } else {
  301. parent::__set($name, $value);
  302. }
  303. }
  304. /**
  305. * Checks if a property value is null.
  306. * This method overrides the parent implementation by checking if the named attribute is `null` or not.
  307. * @param string $name the property name or the event name
  308. * @return bool whether the property value is null
  309. */
  310. public function __isset($name)
  311. {
  312. try {
  313. return $this->__get($name) !== null;
  314. } catch (\Throwable $t) {
  315. return false;
  316. } catch (\Exception $e) {
  317. return false;
  318. }
  319. }
  320. /**
  321. * Sets a component property to be null.
  322. * This method overrides the parent implementation by clearing
  323. * the specified attribute value.
  324. * @param string $name the property name or the event name
  325. */
  326. public function __unset($name)
  327. {
  328. if ($this->hasAttribute($name)) {
  329. unset($this->_attributes[$name]);
  330. if (!empty($this->_relationsDependencies[$name])) {
  331. $this->resetDependentRelations($name);
  332. }
  333. } elseif (array_key_exists($name, $this->_related)) {
  334. unset($this->_related[$name]);
  335. } elseif ($this->getRelation($name, false) === null) {
  336. parent::__unset($name);
  337. }
  338. }
  339. /**
  340. * Declares a `has-one` relation.
  341. * The declaration is returned in terms of a relational [[ActiveQuery]] instance
  342. * through which the related record can be queried and retrieved back.
  343. *
  344. * A `has-one` relation means that there is at most one related record matching
  345. * the criteria set by this relation, e.g., a customer has one country.
  346. *
  347. * For example, to declare the `country` relation for `Customer` class, we can write
  348. * the following code in the `Customer` class:
  349. *
  350. * ```php
  351. * public function getCountry()
  352. * {
  353. * return $this->hasOne(Country::className(), ['id' => 'country_id']);
  354. * }
  355. * ```
  356. *
  357. * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name
  358. * in the related class `Country`, while the 'country_id' value refers to an attribute name
  359. * in the current AR class.
  360. *
  361. * Call methods declared in [[ActiveQuery]] to further customize the relation.
  362. *
  363. * @param string $class the class name of the related record
  364. * @param array $link the primary-foreign key constraint. The keys of the array refer to
  365. * the attributes of the record associated with the `$class` model, while the values of the
  366. * array refer to the corresponding attributes in **this** AR class.
  367. * @return ActiveQueryInterface the relational query object.
  368. */
  369. public function hasOne($class, $link)
  370. {
  371. return $this->createRelationQuery($class, $link, false);
  372. }
  373. /**
  374. * Declares a `has-many` relation.
  375. * The declaration is returned in terms of a relational [[ActiveQuery]] instance
  376. * through which the related record can be queried and retrieved back.
  377. *
  378. * A `has-many` relation means that there are multiple related records matching
  379. * the criteria set by this relation, e.g., a customer has many orders.
  380. *
  381. * For example, to declare the `orders` relation for `Customer` class, we can write
  382. * the following code in the `Customer` class:
  383. *
  384. * ```php
  385. * public function getOrders()
  386. * {
  387. * return $this->hasMany(Order::className(), ['customer_id' => 'id']);
  388. * }
  389. * ```
  390. *
  391. * Note that in the above, the 'customer_id' key in the `$link` parameter refers to
  392. * an attribute name in the related class `Order`, while the 'id' value refers to
  393. * an attribute name in the current AR class.
  394. *
  395. * Call methods declared in [[ActiveQuery]] to further customize the relation.
  396. *
  397. * @param string $class the class name of the related record
  398. * @param array $link the primary-foreign key constraint. The keys of the array refer to
  399. * the attributes of the record associated with the `$class` model, while the values of the
  400. * array refer to the corresponding attributes in **this** AR class.
  401. * @return ActiveQueryInterface the relational query object.
  402. */
  403. public function hasMany($class, $link)
  404. {
  405. return $this->createRelationQuery($class, $link, true);
  406. }
  407. /**
  408. * Creates a query instance for `has-one` or `has-many` relation.
  409. * @param string $class the class name of the related record.
  410. * @param array $link the primary-foreign key constraint.
  411. * @param bool $multiple whether this query represents a relation to more than one record.
  412. * @return ActiveQueryInterface the relational query object.
  413. * @since 2.0.12
  414. * @see hasOne()
  415. * @see hasMany()
  416. */
  417. protected function createRelationQuery($class, $link, $multiple)
  418. {
  419. /* @var $class ActiveRecordInterface */
  420. /* @var $query ActiveQuery */
  421. $query = $class::find();
  422. $query->primaryModel = $this;
  423. $query->link = $link;
  424. $query->multiple = $multiple;
  425. return $query;
  426. }
  427. /**
  428. * Populates the named relation with the related records.
  429. * Note that this method does not check if the relation exists or not.
  430. * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive).
  431. * @param ActiveRecordInterface|array|null $records the related records to be populated into the relation.
  432. * @see getRelation()
  433. */
  434. public function populateRelation($name, $records)
  435. {
  436. foreach ($this->_relationsDependencies as &$relationNames) {
  437. unset($relationNames[$name]);
  438. }
  439. $this->_related[$name] = $records;
  440. }
  441. /**
  442. * Check whether the named relation has been populated with records.
  443. * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive).
  444. * @return bool whether relation has been populated with records.
  445. * @see getRelation()
  446. */
  447. public function isRelationPopulated($name)
  448. {
  449. return array_key_exists($name, $this->_related);
  450. }
  451. /**
  452. * Returns all populated related records.
  453. * @return array an array of related records indexed by relation names.
  454. * @see getRelation()
  455. */
  456. public function getRelatedRecords()
  457. {
  458. return $this->_related;
  459. }
  460. /**
  461. * Returns a value indicating whether the model has an attribute with the specified name.
  462. * @param string $name the name of the attribute
  463. * @return bool whether the model has an attribute with the specified name.
  464. */
  465. public function hasAttribute($name)
  466. {
  467. return isset($this->_attributes[$name]) || in_array($name, $this->attributes(), true);
  468. }
  469. /**
  470. * Returns the named attribute value.
  471. * If this record is the result of a query and the attribute is not loaded,
  472. * `null` will be returned.
  473. * @param string $name the attribute name
  474. * @return mixed the attribute value. `null` if the attribute is not set or does not exist.
  475. * @see hasAttribute()
  476. */
  477. public function getAttribute($name)
  478. {
  479. return isset($this->_attributes[$name]) ? $this->_attributes[$name] : null;
  480. }
  481. /**
  482. * Sets the named attribute value.
  483. * @param string $name the attribute name
  484. * @param mixed $value the attribute value.
  485. * @throws InvalidArgumentException if the named attribute does not exist.
  486. * @see hasAttribute()
  487. */
  488. public function setAttribute($name, $value)
  489. {
  490. if ($this->hasAttribute($name)) {
  491. if (
  492. !empty($this->_relationsDependencies[$name])
  493. && (!array_key_exists($name, $this->_attributes) || $this->_attributes[$name] !== $value)
  494. ) {
  495. $this->resetDependentRelations($name);
  496. }
  497. $this->_attributes[$name] = $value;
  498. } else {
  499. throw new InvalidArgumentException(get_class($this) . ' has no attribute named "' . $name . '".');
  500. }
  501. }
  502. /**
  503. * Returns the old attribute values.
  504. * @return array the old attribute values (name-value pairs)
  505. */
  506. public function getOldAttributes()
  507. {
  508. return $this->_oldAttributes === null ? [] : $this->_oldAttributes;
  509. }
  510. /**
  511. * Sets the old attribute values.
  512. * All existing old attribute values will be discarded.
  513. * @param array|null $values old attribute values to be set.
  514. * If set to `null` this record is considered to be [[isNewRecord|new]].
  515. */
  516. public function setOldAttributes($values)
  517. {
  518. $this->_oldAttributes = $values;
  519. }
  520. /**
  521. * Returns the old value of the named attribute.
  522. * If this record is the result of a query and the attribute is not loaded,
  523. * `null` will be returned.
  524. * @param string $name the attribute name
  525. * @return mixed the old attribute value. `null` if the attribute is not loaded before
  526. * or does not exist.
  527. * @see hasAttribute()
  528. */
  529. public function getOldAttribute($name)
  530. {
  531. return isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
  532. }
  533. /**
  534. * Sets the old value of the named attribute.
  535. * @param string $name the attribute name
  536. * @param mixed $value the old attribute value.
  537. * @throws InvalidArgumentException if the named attribute does not exist.
  538. * @see hasAttribute()
  539. */
  540. public function setOldAttribute($name, $value)
  541. {
  542. if (isset($this->_oldAttributes[$name]) || $this->hasAttribute($name)) {
  543. $this->_oldAttributes[$name] = $value;
  544. } else {
  545. throw new InvalidArgumentException(get_class($this) . ' has no attribute named "' . $name . '".');
  546. }
  547. }
  548. /**
  549. * Marks an attribute dirty.
  550. * This method may be called to force updating a record when calling [[update()]],
  551. * even if there is no change being made to the record.
  552. * @param string $name the attribute name
  553. */
  554. public function markAttributeDirty($name)
  555. {
  556. unset($this->_oldAttributes[$name]);
  557. }
  558. /**
  559. * Returns a value indicating whether the named attribute has been changed.
  560. * @param string $name the name of the attribute.
  561. * @param bool $identical whether the comparison of new and old value is made for
  562. * identical values using `===`, defaults to `true`. Otherwise `==` is used for comparison.
  563. * This parameter is available since version 2.0.4.
  564. * @return bool whether the attribute has been changed
  565. */
  566. public function isAttributeChanged($name, $identical = true)
  567. {
  568. if (isset($this->_attributes[$name], $this->_oldAttributes[$name])) {
  569. if ($identical) {
  570. return $this->_attributes[$name] !== $this->_oldAttributes[$name];
  571. }
  572. return $this->_attributes[$name] != $this->_oldAttributes[$name];
  573. }
  574. return isset($this->_attributes[$name]) || isset($this->_oldAttributes[$name]);
  575. }
  576. /**
  577. * Returns the attribute values that have been modified since they are loaded or saved most recently.
  578. *
  579. * The comparison of new and old values is made for identical values using `===`.
  580. *
  581. * @param string[]|null $names the names of the attributes whose values may be returned if they are
  582. * changed recently. If null, [[attributes()]] will be used.
  583. * @return array the changed attribute values (name-value pairs)
  584. */
  585. public function getDirtyAttributes($names = null)
  586. {
  587. if ($names === null) {
  588. $names = $this->attributes();
  589. }
  590. $names = array_flip($names);
  591. $attributes = [];
  592. if ($this->_oldAttributes === null) {
  593. foreach ($this->_attributes as $name => $value) {
  594. if (isset($names[$name])) {
  595. $attributes[$name] = $value;
  596. }
  597. }
  598. } else {
  599. foreach ($this->_attributes as $name => $value) {
  600. if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $value !== $this->_oldAttributes[$name])) {
  601. $attributes[$name] = $value;
  602. }
  603. }
  604. }
  605. return $attributes;
  606. }
  607. /**
  608. * Saves the current record.
  609. *
  610. * This method will call [[insert()]] when [[isNewRecord]] is `true`, or [[update()]]
  611. * when [[isNewRecord]] is `false`.
  612. *
  613. * For example, to save a customer record:
  614. *
  615. * ```php
  616. * $customer = new Customer; // or $customer = Customer::findOne($id);
  617. * $customer->name = $name;
  618. * $customer->email = $email;
  619. * $customer->save();
  620. * ```
  621. *
  622. * @param bool $runValidation whether to perform validation (calling [[validate()]])
  623. * before saving the record. Defaults to `true`. If the validation fails, the record
  624. * will not be saved to the database and this method will return `false`.
  625. * @param array $attributeNames list of attribute names that need to be saved. Defaults to null,
  626. * meaning all attributes that are loaded from DB will be saved.
  627. * @return bool whether the saving succeeded (i.e. no validation errors occurred).
  628. */
  629. public function save($runValidation = true, $attributeNames = null)
  630. {
  631. if ($this->getIsNewRecord()) {
  632. return $this->insert($runValidation, $attributeNames);
  633. }
  634. return $this->update($runValidation, $attributeNames) !== false;
  635. }
  636. /**
  637. * Saves the changes to this active record into the associated database table.
  638. *
  639. * This method performs the following steps in order:
  640. *
  641. * 1. call [[beforeValidate()]] when `$runValidation` is `true`. If [[beforeValidate()]]
  642. * returns `false`, the rest of the steps will be skipped;
  643. * 2. call [[afterValidate()]] when `$runValidation` is `true`. If validation
  644. * failed, the rest of the steps will be skipped;
  645. * 3. call [[beforeSave()]]. If [[beforeSave()]] returns `false`,
  646. * the rest of the steps will be skipped;
  647. * 4. save the record into database. If this fails, it will skip the rest of the steps;
  648. * 5. call [[afterSave()]];
  649. *
  650. * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]],
  651. * [[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_UPDATE]], and [[EVENT_AFTER_UPDATE]]
  652. * will be raised by the corresponding methods.
  653. *
  654. * Only the [[dirtyAttributes|changed attribute values]] will be saved into database.
  655. *
  656. * For example, to update a customer record:
  657. *
  658. * ```php
  659. * $customer = Customer::findOne($id);
  660. * $customer->name = $name;
  661. * $customer->email = $email;
  662. * $customer->update();
  663. * ```
  664. *
  665. * Note that it is possible the update does not affect any row in the table.
  666. * In this case, this method will return 0. For this reason, you should use the following
  667. * code to check if update() is successful or not:
  668. *
  669. * ```php
  670. * if ($customer->update() !== false) {
  671. * // update successful
  672. * } else {
  673. * // update failed
  674. * }
  675. * ```
  676. *
  677. * @param bool $runValidation whether to perform validation (calling [[validate()]])
  678. * before saving the record. Defaults to `true`. If the validation fails, the record
  679. * will not be saved to the database and this method will return `false`.
  680. * @param array $attributeNames list of attribute names that need to be saved. Defaults to null,
  681. * meaning all attributes that are loaded from DB will be saved.
  682. * @return int|false the number of rows affected, or `false` if validation fails
  683. * or [[beforeSave()]] stops the updating process.
  684. * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
  685. * being updated is outdated.
  686. * @throws Exception in case update failed.
  687. */
  688. public function update($runValidation = true, $attributeNames = null)
  689. {
  690. if ($runValidation && !$this->validate($attributeNames)) {
  691. return false;
  692. }
  693. return $this->updateInternal($attributeNames);
  694. }
  695. /**
  696. * Updates the specified attributes.
  697. *
  698. * This method is a shortcut to [[update()]] when data validation is not needed
  699. * and only a small set attributes need to be updated.
  700. *
  701. * You may specify the attributes to be updated as name list or name-value pairs.
  702. * If the latter, the corresponding attribute values will be modified accordingly.
  703. * The method will then save the specified attributes into database.
  704. *
  705. * Note that this method will **not** perform data validation and will **not** trigger events.
  706. *
  707. * @param array $attributes the attributes (names or name-value pairs) to be updated
  708. * @return int the number of rows affected.
  709. */
  710. public function updateAttributes($attributes)
  711. {
  712. $attrs = [];
  713. foreach ($attributes as $name => $value) {
  714. if (is_int($name)) {
  715. $attrs[] = $value;
  716. } else {
  717. $this->$name = $value;
  718. $attrs[] = $name;
  719. }
  720. }
  721. $values = $this->getDirtyAttributes($attrs);
  722. if (empty($values) || $this->getIsNewRecord()) {
  723. return 0;
  724. }
  725. $rows = static::updateAll($values, $this->getOldPrimaryKey(true));
  726. foreach ($values as $name => $value) {
  727. $this->_oldAttributes[$name] = $this->_attributes[$name];
  728. }
  729. return $rows;
  730. }
  731. /**
  732. * @see update()
  733. * @param array $attributes attributes to update
  734. * @return int|false the number of rows affected, or false if [[beforeSave()]] stops the updating process.
  735. * @throws StaleObjectException
  736. */
  737. protected function updateInternal($attributes = null)
  738. {
  739. if (!$this->beforeSave(false)) {
  740. return false;
  741. }
  742. $values = $this->getDirtyAttributes($attributes);
  743. if (empty($values)) {
  744. $this->afterSave(false, $values);
  745. return 0;
  746. }
  747. $condition = $this->getOldPrimaryKey(true);
  748. $lock = $this->optimisticLock();
  749. if ($lock !== null) {
  750. $values[$lock] = $this->$lock + 1;
  751. $condition[$lock] = $this->$lock;
  752. }
  753. // We do not check the return value of updateAll() because it's possible
  754. // that the UPDATE statement doesn't change anything and thus returns 0.
  755. $rows = static::updateAll($values, $condition);
  756. if ($lock !== null && !$rows) {
  757. throw new StaleObjectException('The object being updated is outdated.');
  758. }
  759. if (isset($values[$lock])) {
  760. $this->$lock = $values[$lock];
  761. }
  762. $changedAttributes = [];
  763. foreach ($values as $name => $value) {
  764. $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
  765. $this->_oldAttributes[$name] = $value;
  766. }
  767. $this->afterSave(false, $changedAttributes);
  768. return $rows;
  769. }
  770. /**
  771. * Updates one or several counter columns for the current AR object.
  772. * Note that this method differs from [[updateAllCounters()]] in that it only
  773. * saves counters for the current AR object.
  774. *
  775. * An example usage is as follows:
  776. *
  777. * ```php
  778. * $post = Post::findOne($id);
  779. * $post->updateCounters(['view_count' => 1]);
  780. * ```
  781. *
  782. * @param array $counters the counters to be updated (attribute name => increment value)
  783. * Use negative values if you want to decrement the counters.
  784. * @return bool whether the saving is successful
  785. * @see updateAllCounters()
  786. */
  787. public function updateCounters($counters)
  788. {
  789. if (static::updateAllCounters($counters, $this->getOldPrimaryKey(true)) > 0) {
  790. foreach ($counters as $name => $value) {
  791. if (!isset($this->_attributes[$name])) {
  792. $this->_attributes[$name] = $value;
  793. } else {
  794. $this->_attributes[$name] += $value;
  795. }
  796. $this->_oldAttributes[$name] = $this->_attributes[$name];
  797. }
  798. return true;
  799. }
  800. return false;
  801. }
  802. /**
  803. * Deletes the table row corresponding to this active record.
  804. *
  805. * This method performs the following steps in order:
  806. *
  807. * 1. call [[beforeDelete()]]. If the method returns `false`, it will skip the
  808. * rest of the steps;
  809. * 2. delete the record from the database;
  810. * 3. call [[afterDelete()]].
  811. *
  812. * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]]
  813. * will be raised by the corresponding methods.
  814. *
  815. * @return int|false the number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
  816. * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
  817. * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
  818. * being deleted is outdated.
  819. * @throws Exception in case delete failed.
  820. */
  821. public function delete()
  822. {
  823. $result = false;
  824. if ($this->beforeDelete()) {
  825. // we do not check the return value of deleteAll() because it's possible
  826. // the record is already deleted in the database and thus the method will return 0
  827. $condition = $this->getOldPrimaryKey(true);
  828. $lock = $this->optimisticLock();
  829. if ($lock !== null) {
  830. $condition[$lock] = $this->$lock;
  831. }
  832. $result = static::deleteAll($condition);
  833. if ($lock !== null && !$result) {
  834. throw new StaleObjectException('The object being deleted is outdated.');
  835. }
  836. $this->_oldAttributes = null;
  837. $this->afterDelete();
  838. }
  839. return $result;
  840. }
  841. /**
  842. * Returns a value indicating whether the current record is new.
  843. * @return bool whether the record is new and should be inserted when calling [[save()]].
  844. */
  845. public function getIsNewRecord()
  846. {
  847. return $this->_oldAttributes === null;
  848. }
  849. /**
  850. * Sets the value indicating whether the record is new.
  851. * @param bool $value whether the record is new and should be inserted when calling [[save()]].
  852. * @see getIsNewRecord()
  853. */
  854. public function setIsNewRecord($value)
  855. {
  856. $this->_oldAttributes = $value ? null : $this->_attributes;
  857. }
  858. /**
  859. * Initializes the object.
  860. * This method is called at the end of the constructor.
  861. * The default implementation will trigger an [[EVENT_INIT]] event.
  862. */
  863. public function init()
  864. {
  865. parent::init();
  866. $this->trigger(self::EVENT_INIT);
  867. }
  868. /**
  869. * This method is called when the AR object is created and populated with the query result.
  870. * The default implementation will trigger an [[EVENT_AFTER_FIND]] event.
  871. * When overriding this method, make sure you call the parent implementation to ensure the
  872. * event is triggered.
  873. */
  874. public function afterFind()
  875. {
  876. $this->trigger(self::EVENT_AFTER_FIND);
  877. }
  878. /**
  879. * This method is called at the beginning of inserting or updating a record.
  880. *
  881. * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is `true`,
  882. * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is `false`.
  883. * When overriding this method, make sure you call the parent implementation like the following:
  884. *
  885. * ```php
  886. * public function beforeSave($insert)
  887. * {
  888. * if (!parent::beforeSave($insert)) {
  889. * return false;
  890. * }
  891. *
  892. * // ...custom code here...
  893. * return true;
  894. * }
  895. * ```
  896. *
  897. * @param bool $insert whether this method called while inserting a record.
  898. * If `false`, it means the method is called while updating a record.
  899. * @return bool whether the insertion or updating should continue.
  900. * If `false`, the insertion or updating will be cancelled.
  901. */
  902. public function beforeSave($insert)
  903. {
  904. $event = new ModelEvent();
  905. $this->trigger($insert ? self::EVENT_BEFORE_INSERT : self::EVENT_BEFORE_UPDATE, $event);
  906. return $event->isValid;
  907. }
  908. /**
  909. * This method is called at the end of inserting or updating a record.
  910. * The default implementation will trigger an [[EVENT_AFTER_INSERT]] event when `$insert` is `true`,
  911. * or an [[EVENT_AFTER_UPDATE]] event if `$insert` is `false`. The event class used is [[AfterSaveEvent]].
  912. * When overriding this method, make sure you call the parent implementation so that
  913. * the event is triggered.
  914. * @param bool $insert whether this method called while inserting a record.
  915. * If `false`, it means the method is called while updating a record.
  916. * @param array $changedAttributes The old values of attributes that had changed and were saved.
  917. * You can use this parameter to take action based on the changes made for example send an email
  918. * when the password had changed or implement audit trail that tracks all the changes.
  919. * `$changedAttributes` gives you the old attribute values while the active record (`$this`) has
  920. * already the new, updated values.
  921. *
  922. * Note that no automatic type conversion performed by default. You may use
  923. * [[\yii\behaviors\AttributeTypecastBehavior]] to facilitate attribute typecasting.
  924. * See http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#attributes-typecasting.
  925. */
  926. public function afterSave($insert, $changedAttributes)
  927. {
  928. $this->trigger($insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE, new AfterSaveEvent([
  929. 'changedAttributes' => $changedAttributes,
  930. ]));
  931. }
  932. /**
  933. * This method is invoked before deleting a record.
  934. *
  935. * The default implementation raises the [[EVENT_BEFORE_DELETE]] event.
  936. * When overriding this method, make sure you call the parent implementation like the following:
  937. *
  938. * ```php
  939. * public function beforeDelete()
  940. * {
  941. * if (!parent::beforeDelete()) {
  942. * return false;
  943. * }
  944. *
  945. * // ...custom code here...
  946. * return true;
  947. * }
  948. * ```
  949. *
  950. * @return bool whether the record should be deleted. Defaults to `true`.
  951. */
  952. public function beforeDelete()
  953. {
  954. $event = new ModelEvent();
  955. $this->trigger(self::EVENT_BEFORE_DELETE, $event);
  956. return $event->isValid;
  957. }
  958. /**
  959. * This method is invoked after deleting a record.
  960. * The default implementation raises the [[EVENT_AFTER_DELETE]] event.
  961. * You may override this method to do postprocessing after the record is deleted.
  962. * Make sure you call the parent implementation so that the event is raised properly.
  963. */
  964. public function afterDelete()
  965. {
  966. $this->trigger(self::EVENT_AFTER_DELETE);
  967. }
  968. /**
  969. * Repopulates this active record with the latest data.
  970. *
  971. * If the refresh is successful, an [[EVENT_AFTER_REFRESH]] event will be triggered.
  972. * This event is available since version 2.0.8.
  973. *
  974. * @return bool whether the row still exists in the database. If `true`, the latest data
  975. * will be populated to this active record. Otherwise, this record will remain unchanged.
  976. */
  977. public function refresh()
  978. {
  979. /* @var $record BaseActiveRecord */
  980. $record = static::findOne($this->getPrimaryKey(true));
  981. return $this->refreshInternal($record);
  982. }
  983. /**
  984. * Repopulates this active record with the latest data from a newly fetched instance.
  985. * @param BaseActiveRecord $record the record to take attributes from.
  986. * @return bool whether refresh was successful.
  987. * @see refresh()
  988. * @since 2.0.13
  989. */
  990. protected function refreshInternal($record)
  991. {
  992. if ($record === null) {
  993. return false;
  994. }
  995. foreach ($this->attributes() as $name) {
  996. $this->_attributes[$name] = isset($record->_attributes[$name]) ? $record->_attributes[$name] : null;
  997. }
  998. $this->_oldAttributes = $record->_oldAttributes;
  999. $this->_related = [];
  1000. $this->_relationsDependencies = [];
  1001. $this->afterRefresh();
  1002. return true;
  1003. }
  1004. /**
  1005. * This method is called when the AR object is refreshed.
  1006. * The default implementation will trigger an [[EVENT_AFTER_REFRESH]] event.
  1007. * When overriding this method, make sure you call the parent implementation to ensure the
  1008. * event is triggered.
  1009. * @since 2.0.8
  1010. */
  1011. public function afterRefresh()
  1012. {
  1013. $this->trigger(self::EVENT_AFTER_REFRESH);
  1014. }
  1015. /**
  1016. * Returns a value indicating whether the given active record is the same as the current one.
  1017. * The comparison is made by comparing the table names and the primary key values of the two active records.
  1018. * If one of the records [[isNewRecord|is new]] they are also considered not equal.
  1019. * @param ActiveRecordInterface $record record to compare to
  1020. * @return bool whether the two active records refer to the same row in the same database table.
  1021. */
  1022. public function equals($record)
  1023. {
  1024. if ($this->getIsNewRecord() || $record->getIsNewRecord()) {
  1025. return false;
  1026. }
  1027. return get_class($this) === get_class($record) && $this->getPrimaryKey() === $record->getPrimaryKey();
  1028. }
  1029. /**
  1030. * Returns the primary key value(s).
  1031. * @param bool $asArray whether to return the primary key value as an array. If `true`,
  1032. * the return value will be an array with column names as keys and column values as values.
  1033. * Note that for composite primary keys, an array will always be returned regardless of this parameter value.
  1034. * @property mixed The primary key value. An array (column name => column value) is returned if
  1035. * the primary key is composite. A string is returned otherwise (null will be returned if
  1036. * the key value is null).
  1037. * @return mixed the primary key value. An array (column name => column value) is returned if the primary key
  1038. * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if
  1039. * the key value is null).
  1040. */
  1041. public function getPrimaryKey($asArray = false)
  1042. {
  1043. $keys = $this->primaryKey();
  1044. if (!$asArray && count($keys) === 1) {
  1045. return isset($this->_attributes[$keys[0]]) ? $this->_attributes[$keys[0]] : null;
  1046. }
  1047. $values = [];
  1048. foreach ($keys as $name) {
  1049. $values[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : null;
  1050. }
  1051. return $values;
  1052. }
  1053. /**
  1054. * Returns the old primary key value(s).
  1055. * This refers to the primary key value that is populated into the record
  1056. * after executing a find method (e.g. find(), findOne()).
  1057. * The value remains unchanged even if the primary key attribute is manually assigned with a different value.
  1058. * @param bool $asArray whether to return the primary key value as an array. If `true`,
  1059. * the return value will be an array with column name as key and column value as value.
  1060. * If this is `false` (default), a scalar value will be returned for non-composite primary key.
  1061. * @property mixed The old primary key value. An array (column name => column value) is
  1062. * returned if the primary key is composite. A string is returned otherwise (null will be
  1063. * returned if the key value is null).
  1064. * @return mixed the old primary key value. An array (column name => column value) is returned if the primary key
  1065. * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if
  1066. * the key value is null).
  1067. * @throws Exception if the AR model does not have a primary key
  1068. */
  1069. public function getOldPrimaryKey($asArray = false)
  1070. {
  1071. $keys = $this->primaryKey();
  1072. if (empty($keys)) {
  1073. 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.');
  1074. }
  1075. if (!$asArray && count($keys) === 1) {
  1076. return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
  1077. }
  1078. $values = [];
  1079. foreach ($keys as $name) {
  1080. $values[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
  1081. }
  1082. return $values;
  1083. }
  1084. /**
  1085. * Populates an active record object using a row of data from the database/storage.
  1086. *
  1087. * This is an internal method meant to be called to create active record objects after
  1088. * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate
  1089. * the query results into active records.
  1090. *
  1091. * When calling this method manually you should call [[afterFind()]] on the created
  1092. * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]].
  1093. *
  1094. * @param BaseActiveRecord $record the record to be populated. In most cases this will be an instance
  1095. * created by [[instantiate()]] beforehand.
  1096. * @param array $row attribute values (name => value)
  1097. */
  1098. public static function populateRecord($record, $row)
  1099. {
  1100. $columns = array_flip($record->attributes());
  1101. foreach ($row as $name => $value) {
  1102. if (isset($columns[$name])) {
  1103. $record->_attributes[$name] = $value;
  1104. } elseif ($record->canSetProperty($name)) {
  1105. $record->$name = $value;
  1106. }
  1107. }
  1108. $record->_oldAttributes = $record->_attributes;
  1109. $record->_related = [];
  1110. $record->_relationsDependencies = [];
  1111. }
  1112. /**
  1113. * Creates an active record instance.
  1114. *
  1115. * This method is called together with [[populateRecord()]] by [[ActiveQuery]].
  1116. * It is not meant to be used for creating new records directly.
  1117. *
  1118. * You may override this method if the instance being created
  1119. * depends on the row data to be populated into the record.
  1120. * For example, by creating a record based on the value of a column,
  1121. * you may implement the so-called single-table inheritance mapping.
  1122. * @param array $row row data to be populated into the record.
  1123. * @return static the newly created active record
  1124. */
  1125. public static function instantiate($row)
  1126. {
  1127. return new static();
  1128. }
  1129. /**
  1130. * Returns whether there is an element at the specified offset.
  1131. * This method is required by the interface [[\ArrayAccess]].
  1132. * @param mixed $offset the offset to check on
  1133. * @return bool whether there is an element at the specified offset.
  1134. */
  1135. public function offsetExists($offset)
  1136. {
  1137. return $this->__isset($offset);
  1138. }
  1139. /**
  1140. * Returns the relation object with the specified name.
  1141. * A relation is defined by a getter method which returns an [[ActiveQueryInterface]] object.
  1142. * It can be declared in either the Active Record class itself or one of its behaviors.
  1143. * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive).
  1144. * @param bool $throwException whether to throw exception if the relation does not exist.
  1145. * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist
  1146. * and `$throwException` is `false`, `null` will be returned.
  1147. * @throws InvalidArgumentException if the named relation does not exist.
  1148. */
  1149. public function getRelation($name, $throwException = true)
  1150. {
  1151. $getter = 'get' . $name;
  1152. try {
  1153. // the relation could be defined in a behavior
  1154. $relation = $this->$getter();
  1155. } catch (UnknownMethodException $e) {
  1156. if ($throwException) {
  1157. throw new InvalidArgumentException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
  1158. }
  1159. return null;
  1160. }
  1161. if (!$relation instanceof ActiveQueryInterface) {
  1162. if ($throwException) {
  1163. throw new InvalidArgumentException(get_class($this) . ' has no relation named "' . $name . '".');
  1164. }
  1165. return null;
  1166. }
  1167. if (method_exists($this, $getter)) {
  1168. // relation name is case sensitive, trying to validate it when the relation is defined within this class
  1169. $method = new \ReflectionMethod($this, $getter);
  1170. $realName = lcfirst(substr($method->getName(), 3));
  1171. if ($realName !== $name) {
  1172. if ($throwException) {
  1173. throw new InvalidArgumentException('Relation names are case sensitive. ' . get_class($this) . " has a relation named \"$realName\" instead of \"$name\".");
  1174. }
  1175. return null;
  1176. }
  1177. }
  1178. return $relation;
  1179. }
  1180. /**
  1181. * Establishes the relationship between two models.
  1182. *
  1183. * The relationship is established by setting the foreign key value(s) in one model
  1184. * to be the corresponding primary key value(s) in the other model.
  1185. * The model with the foreign key will be saved into database without performing validation.
  1186. *
  1187. * If the relationship involves a junction table, a new row will be inserted into the
  1188. * junction table which contains the primary key values from both models.
  1189. *
  1190. * Note that this method requires that the primary key value is not null.
  1191. *
  1192. * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method.
  1193. * @param ActiveRecordInterface $model the model to be linked with the current one.
  1194. * @param array $extraColumns additional column values to be saved into the junction table.
  1195. * This parameter is only meaningful for a relationship involving a junction table
  1196. * (i.e., a relation set with [[ActiveRelationTrait::via()]] or [[ActiveQuery::viaTable()]].)
  1197. * @throws InvalidCallException if the method is unable to link two models.
  1198. */
  1199. public function link($name, $model, $extraColumns = [])
  1200. {
  1201. $relation = $this->getRelation($name);
  1202. if ($relation->via !== null) {
  1203. if ($this->getIsNewRecord() || $model->getIsNewRecord()) {
  1204. throw new InvalidCallException('Unable to link models: the models being linked cannot be newly created.');
  1205. }
  1206. if (is_array($relation->via)) {
  1207. /* @var $viaRelation ActiveQuery */
  1208. list($viaName, $viaRelation) = $relation->via;
  1209. $viaClass = $viaRelation->modelClass;
  1210. // unset $viaName so that it can be reloaded to reflect the change
  1211. unset($this->_related[$viaName]);
  1212. } else {
  1213. $viaRelation = $relation->via;
  1214. $viaTable = reset($relation->via->from);
  1215. }
  1216. $columns = [];
  1217. foreach ($viaRelation->link as $a => $b) {
  1218. $columns[$a] = $this->$b;
  1219. }
  1220. foreach ($relation->link as $a => $b) {
  1221. $columns[$b] = $model->$a;
  1222. }
  1223. foreach ($extraColumns as $k => $v) {
  1224. $columns[$k] = $v;
  1225. }
  1226. if (is_array($relation->via)) {
  1227. /* @var $viaClass ActiveRecordInterface */
  1228. /* @var $record ActiveRecordInterface */
  1229. $record = Yii::createObject($viaClass);
  1230. foreach ($columns as $column => $value) {
  1231. $record->$column = $value;
  1232. }
  1233. $record->insert(false);
  1234. } else {
  1235. /* @var $viaTable string */
  1236. static::getDb()->createCommand()
  1237. ->insert($viaTable, $columns)->execute();
  1238. }
  1239. } else {
  1240. $p1 = $model->isPrimaryKey(array_keys($relation->link));
  1241. $p2 = static::isPrimaryKey(array_values($relation->link));
  1242. if ($p1 && $p2) {
  1243. if ($this->getIsNewRecord() && $model->getIsNewRecord()) {
  1244. throw new InvalidCallException('Unable to link models: at most one model can be newly created.');
  1245. } elseif ($this->getIsNewRecord()) {
  1246. $this->bindModels(array_flip($relation->link), $this, $model);
  1247. } else {
  1248. $this->bindModels($relation->link, $model, $this);
  1249. }
  1250. } elseif ($p1) {
  1251. $this->bindModels(array_flip($relation->link), $this, $model);
  1252. } elseif ($p2) {
  1253. $this->bindModels($relation->link, $model, $this);
  1254. } else {
  1255. throw new InvalidCallException('Unable to link models: the link defining the relation does not involve any primary key.');
  1256. }
  1257. }
  1258. // update lazily loaded related objects
  1259. if (!$relation->multiple) {
  1260. $this->_related[$name] = $model;
  1261. } elseif (isset($this->_related[$name])) {
  1262. if ($relation->indexBy !== null) {
  1263. if ($relation->indexBy instanceof \Closure) {
  1264. $index = call_user_func($relation->indexBy, $model);
  1265. } else {
  1266. $index = $model->{$relation->indexBy};
  1267. }
  1268. $this->_related[$name][$index] = $model;
  1269. } else {
  1270. $this->_related[$name][] = $model;
  1271. }
  1272. }
  1273. }
  1274. /**
  1275. * Destroys the relationship between two models.
  1276. *
  1277. * The model with the foreign key of the relationship will be deleted if `$delete` is `true`.
  1278. * Otherwise, the foreign key will be set `null` and the model will be saved without validation.
  1279. *
  1280. * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method.
  1281. * @param ActiveRecordInterface $model the model to be unlinked from the current one.
  1282. * You have to make sure that the model is really related with the current model as this method
  1283. * does not check this.
  1284. * @param bool $delete whether to delete the model that contains the foreign key.
  1285. * If `false`, the model's foreign key will be set `null` and saved.
  1286. * If `true`, the model containing the foreign key will be deleted.
  1287. * @throws InvalidCallException if the models cannot be unlinked
  1288. */
  1289. public function unlink($name, $model, $delete = false)
  1290. {
  1291. $relation = $this->getRelation($name);
  1292. if ($relation->via !== null) {
  1293. if (is_array($relation->via)) {
  1294. /* @var $viaRelation ActiveQuery */
  1295. list($viaName, $viaRelation) = $relation->via;
  1296. $viaClass = $viaRelation->modelClass;
  1297. unset($this->_related[$viaName]);
  1298. } else {
  1299. $viaRelation = $relation->via;
  1300. $viaTable = reset($relation->via->from);
  1301. }
  1302. $columns = [];
  1303. foreach ($viaRelation->link as $a => $b) {
  1304. $columns[$a] = $this->$b;
  1305. }
  1306. foreach ($relation->link as $a => $b) {
  1307. $columns[$b] = $model->$a;
  1308. }
  1309. $nulls = [];
  1310. foreach (array_keys($columns) as $a) {
  1311. $nulls[$a] = null;
  1312. }
  1313. if (is_array($relation->via)) {
  1314. /* @var $viaClass ActiveRecordInterface */
  1315. if ($delete) {
  1316. $viaClass::deleteAll($columns);
  1317. } else {
  1318. $viaClass::updateAll($nulls, $columns);
  1319. }
  1320. } else {
  1321. /* @var $viaTable string */
  1322. /* @var $command Command */
  1323. $command = static::getDb()->createCommand();
  1324. if ($delete) {
  1325. $command->delete($viaTable, $columns)->execute();
  1326. } else {
  1327. $command->update($viaTable, $nulls, $columns)->execute();
  1328. }
  1329. }
  1330. } else {
  1331. $p1 = $model->isPrimaryKey(array_keys($relation->link));
  1332. $p2 = static::isPrimaryKey(array_values($relation->link));
  1333. if ($p2) {
  1334. if ($delete) {
  1335. $model->delete();
  1336. } else {
  1337. foreach ($relation->link as $a => $b) {
  1338. $model->$a = null;
  1339. }
  1340. $model->save(false);
  1341. }
  1342. } elseif ($p1) {
  1343. foreach ($relation->link as $a => $b) {
  1344. if (is_array($this->$b)) { // relation via array valued attribute
  1345. if (($key = array_search($model->$a, $this->$b, false)) !== false) {
  1346. $values = $this->$b;
  1347. unset($values[$key]);
  1348. $this->$b = array_values($values);
  1349. }
  1350. } else {
  1351. $this->$b = null;
  1352. }
  1353. }
  1354. $delete ? $this->delete() : $this->save(false);
  1355. } else {
  1356. throw new InvalidCallException('Unable to unlink models: the link does not involve any primary key.');
  1357. }
  1358. }
  1359. if (!$relation->multiple) {
  1360. unset($this->_related[$name]);
  1361. } elseif (isset($this->_related[$name])) {
  1362. /* @var $b ActiveRecordInterface */
  1363. foreach ($this->_related[$name] as $a => $b) {
  1364. if ($model->getPrimaryKey() === $b->getPrimaryKey()) {
  1365. unset($this->_related[$name][$a]);
  1366. }
  1367. }
  1368. }
  1369. }
  1370. /**
  1371. * Destroys the relationship in current model.
  1372. *
  1373. * The model with the foreign key of the relationship will be deleted if `$delete` is `true`.
  1374. * Otherwise, the foreign key will be set `null` and the model will be saved without validation.
  1375. *
  1376. * Note that to destroy the relationship without removing records make sure your keys can be set to null
  1377. *
  1378. * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method.
  1379. * @param bool $delete whether to delete the model that contains the foreign key.
  1380. *
  1381. * Note that the deletion will be performed using [[deleteAll()]], which will not trigger any events on the related models.
  1382. * If you need [[EVENT_BEFORE_DELETE]] or [[EVENT_AFTER_DELETE]] to be triggered, you need to [[find()|find]] the models first
  1383. * and then call [[delete()]] on each of them.
  1384. */
  1385. public function unlinkAll($name, $delete = false)
  1386. {
  1387. $relation = $this->getRelation($name);
  1388. if ($relation->via !== null) {
  1389. if (is_array($relation->via)) {
  1390. /* @var $viaRelation ActiveQuery */
  1391. list($viaName, $viaRelation) = $relation->via;
  1392. $viaClass = $viaRelation->modelClass;
  1393. unset($this->_related[$viaName]);
  1394. } else {
  1395. $viaRelation = $relation->via;
  1396. $viaTable = reset($relation->via->from);
  1397. }
  1398. $condition = [];
  1399. $nulls = [];
  1400. foreach ($viaRelation->link as $a => $b) {
  1401. $nulls[$a] = null;
  1402. $condition[$a] = $this->$b;
  1403. }
  1404. if (!empty($viaRelation->where)) {
  1405. $condition = ['and', $condition, $viaRelation->where];
  1406. }
  1407. if (!empty($viaRelation->on)) {
  1408. $condition = ['and', $condition, $viaRelation->on];
  1409. }
  1410. if (is_array($relation->via)) {
  1411. /* @var $viaClass ActiveRecordInterface */
  1412. if ($delete) {
  1413. $viaClass::deleteAll($condition);
  1414. } else {
  1415. $viaClass::updateAll($nulls, $condition);
  1416. }
  1417. } else {
  1418. /* @var $viaTable string */
  1419. /* @var $command Command */
  1420. $command = static::getDb()->createCommand();
  1421. if ($delete) {
  1422. $command->delete($viaTable, $condition)->execute();
  1423. } else {
  1424. $command->update($viaTable, $nulls, $condition)->execute();
  1425. }
  1426. }
  1427. } else {
  1428. /* @var $relatedModel ActiveRecordInterface */
  1429. $relatedModel = $relation->modelClass;
  1430. if (!$delete && count($relation->link) === 1 && is_array($this->{$b = reset($relation->link)})) {
  1431. // relation via array valued attribute
  1432. $this->$b = [];
  1433. $this->save(false);
  1434. } else {
  1435. $nulls = [];
  1436. $condition = [];
  1437. foreach ($relation->link as $a => $b) {
  1438. $nulls[$a] = null;
  1439. $condition[$a] = $this->$b;
  1440. }
  1441. if (!empty($relation->where)) {
  1442. $condition = ['and', $condition, $relation->where];
  1443. }
  1444. if (!empty($relation->on)) {
  1445. $condition = ['and', $condition, $relation->on];
  1446. }
  1447. if ($delete) {
  1448. $relatedModel::deleteAll($condition);
  1449. } else {
  1450. $relatedModel::updateAll($nulls, $condition);
  1451. }
  1452. }
  1453. }
  1454. unset($this->_related[$name]);
  1455. }
  1456. /**
  1457. * @param array $link
  1458. * @param ActiveRecordInterface $foreignModel
  1459. * @param ActiveRecordInterface $primaryModel
  1460. * @throws InvalidCallException
  1461. */
  1462. private function bindModels($link, $foreignModel, $primaryModel)
  1463. {
  1464. foreach ($link as $fk => $pk) {
  1465. $value = $primaryModel->$pk;
  1466. if ($value === null) {
  1467. throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');
  1468. }
  1469. if (is_array($foreignModel->$fk)) { // relation via array valued attribute
  1470. $foreignModel->{$fk}[] = $value;
  1471. } else {
  1472. $foreignModel->{$fk} = $value;
  1473. }
  1474. }
  1475. $foreignModel->save(false);
  1476. }
  1477. /**
  1478. * Returns a value indicating whether the given set of attributes represents the primary key for this model.
  1479. * @param array $keys the set of attributes to check
  1480. * @return bool whether the given set of attributes represents the primary key for this model
  1481. */
  1482. public static function isPrimaryKey($keys)
  1483. {
  1484. $pks = static::primaryKey();
  1485. if (count($keys) === count($pks)) {
  1486. return count(array_intersect($keys, $pks)) === count($pks);
  1487. }
  1488. return false;
  1489. }
  1490. /**
  1491. * Returns the text label for the specified attribute.
  1492. * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model.
  1493. * @param string $attribute the attribute name
  1494. * @return string the attribute label
  1495. * @see generateAttributeLabel()
  1496. * @see attributeLabels()
  1497. */
  1498. public function getAttributeLabel($attribute)
  1499. {
  1500. $labels = $this->attributeLabels();
  1501. if (isset($labels[$attribute])) {
  1502. return $labels[$attribute];
  1503. } elseif (strpos($attribute, '.')) {
  1504. $attributeParts = explode('.', $attribute);
  1505. $neededAttribute = array_pop($attributeParts);
  1506. $relatedModel = $this;
  1507. foreach ($attributeParts as $relationName) {
  1508. if ($relatedModel->isRelationPopulated($relationName) && $relatedModel->$relationName instanceof self) {
  1509. $relatedModel = $relatedModel->$relationName;
  1510. } else {
  1511. try {
  1512. $relation = $relatedModel->getRelation($relationName);
  1513. } catch (InvalidParamException $e) {
  1514. return $this->generateAttributeLabel($attribute);
  1515. }
  1516. /* @var $modelClass ActiveRecordInterface */
  1517. $modelClass = $relation->modelClass;
  1518. $relatedModel = $modelClass::instance();
  1519. }
  1520. }
  1521. $labels = $relatedModel->attributeLabels();
  1522. if (isset($labels[$neededAttribute])) {
  1523. return $labels[$neededAttribute];
  1524. }
  1525. }
  1526. return $this->generateAttributeLabel($attribute);
  1527. }
  1528. /**
  1529. * Returns the text hint for the specified attribute.
  1530. * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model.
  1531. * @param string $attribute the attribute name
  1532. * @return string the attribute hint
  1533. * @see attributeHints()
  1534. * @since 2.0.4
  1535. */
  1536. public function getAttributeHint($attribute)
  1537. {
  1538. $hints = $this->attributeHints();
  1539. if (isset($hints[$attribute])) {
  1540. return $hints[$attribute];
  1541. } elseif (strpos($attribute, '.')) {
  1542. $attributeParts = explode('.', $attribute);
  1543. $neededAttribute = array_pop($attributeParts);
  1544. $relatedModel = $this;
  1545. foreach ($attributeParts as $relationName) {
  1546. if ($relatedModel->isRelationPopulated($relationName) && $relatedModel->$relationName instanceof self) {
  1547. $relatedModel = $relatedModel->$relationName;
  1548. } else {
  1549. try {
  1550. $relation = $relatedModel->getRelation($relationName);
  1551. } catch (InvalidParamException $e) {
  1552. return '';
  1553. }
  1554. /* @var $modelClass ActiveRecordInterface */
  1555. $modelClass = $relation->modelClass;
  1556. $relatedModel = $modelClass::instance();
  1557. }
  1558. }
  1559. $hints = $relatedModel->attributeHints();
  1560. if (isset($hints[$neededAttribute])) {
  1561. return $hints[$neededAttribute];
  1562. }
  1563. }
  1564. return '';
  1565. }
  1566. /**
  1567. * {@inheritdoc}
  1568. *
  1569. * The default implementation returns the names of the columns whose values have been populated into this record.
  1570. */
  1571. public function fields()
  1572. {
  1573. $fields = array_keys($this->_attributes);
  1574. return array_combine($fields, $fields);
  1575. }
  1576. /**
  1577. * {@inheritdoc}
  1578. *
  1579. * The default implementation returns the names of the relations that have been populated into this record.
  1580. */
  1581. public function extraFields()
  1582. {
  1583. $fields = array_keys($this->getRelatedRecords());
  1584. return array_combine($fields, $fields);
  1585. }
  1586. /**
  1587. * Sets the element value at the specified offset to null.
  1588. * This method is required by the SPL interface [[\ArrayAccess]].
  1589. * It is implicitly called when you use something like `unset($model[$offset])`.
  1590. * @param mixed $offset the offset to unset element
  1591. */
  1592. public function offsetUnset($offset)
  1593. {
  1594. if (property_exists($this, $offset)) {
  1595. $this->$offset = null;
  1596. } else {
  1597. unset($this->$offset);
  1598. }
  1599. }
  1600. /**
  1601. * Resets dependent related models checking if their links contain specific attribute.
  1602. * @param string $attribute The changed attribute name.
  1603. */
  1604. private function resetDependentRelations($attribute)
  1605. {
  1606. foreach ($this->_relationsDependencies[$attribute] as $relation) {
  1607. unset($this->_related[$relation]);
  1608. }
  1609. unset($this->_relationsDependencies[$attribute]);
  1610. }
  1611. /**
  1612. * Sets relation dependencies for a property
  1613. * @param string $name property name
  1614. * @param ActiveQueryInterface $relation relation instance
  1615. */
  1616. private function setRelationDependencies($name, $relation)
  1617. {
  1618. if (empty($relation->via) && $relation->link) {
  1619. foreach ($relation->link as $attribute) {
  1620. $this->_relationsDependencies[$attribute][$name] = $name;
  1621. }
  1622. } elseif ($relation->via instanceof ActiveQueryInterface) {
  1623. $this->setRelationDependencies($name, $relation->via);
  1624. } elseif (is_array($relation->via)) {
  1625. list(, $viaQuery) = $relation->via;
  1626. $this->setRelationDependencies($name, $viaQuery);
  1627. }
  1628. }
  1629. }