BaseActiveRecord.php 67 KB

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