您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1515 行
59KB

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