BaseActiveRecord.php 72 KB

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