BaseActiveRecord.php 68 KB

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