Complex classes like BaseActiveRecord often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseActiveRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class BaseActiveRecord extends Model implements ActiveRecordInterface |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @event Event an event that is triggered when the record is initialized via [[init()]]. |
||
| 48 | */ |
||
| 49 | const EVENT_INIT = 'init'; |
||
| 50 | /** |
||
| 51 | * @event Event an event that is triggered after the record is created and populated with query result. |
||
| 52 | */ |
||
| 53 | const EVENT_AFTER_FIND = 'afterFind'; |
||
| 54 | /** |
||
| 55 | * @event ModelEvent an event that is triggered before inserting a record. |
||
| 56 | * You may set [[ModelEvent::isValid]] to be `false` to stop the insertion. |
||
| 57 | */ |
||
| 58 | const EVENT_BEFORE_INSERT = 'beforeInsert'; |
||
| 59 | /** |
||
| 60 | * @event AfterSaveEvent an event that is triggered after a record is inserted. |
||
| 61 | */ |
||
| 62 | const EVENT_AFTER_INSERT = 'afterInsert'; |
||
| 63 | /** |
||
| 64 | * @event ModelEvent an event that is triggered before updating a record. |
||
| 65 | * You may set [[ModelEvent::isValid]] to be `false` to stop the update. |
||
| 66 | */ |
||
| 67 | const EVENT_BEFORE_UPDATE = 'beforeUpdate'; |
||
| 68 | /** |
||
| 69 | * @event AfterSaveEvent an event that is triggered after a record is updated. |
||
| 70 | */ |
||
| 71 | const EVENT_AFTER_UPDATE = 'afterUpdate'; |
||
| 72 | /** |
||
| 73 | * @event ModelEvent an event that is triggered before deleting a record. |
||
| 74 | * You may set [[ModelEvent::isValid]] to be `false` to stop the deletion. |
||
| 75 | */ |
||
| 76 | const EVENT_BEFORE_DELETE = 'beforeDelete'; |
||
| 77 | /** |
||
| 78 | * @event Event an event that is triggered after a record is deleted. |
||
| 79 | */ |
||
| 80 | const EVENT_AFTER_DELETE = 'afterDelete'; |
||
| 81 | /** |
||
| 82 | * @event Event an event that is triggered after a record is refreshed. |
||
| 83 | * @since 2.0.8 |
||
| 84 | */ |
||
| 85 | const EVENT_AFTER_REFRESH = 'afterRefresh'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array attribute values indexed by attribute names |
||
| 89 | */ |
||
| 90 | private $_attributes = []; |
||
| 91 | /** |
||
| 92 | * @var array|null old attribute values indexed by attribute names. |
||
| 93 | * This is `null` if the record [[isNewRecord|is new]]. |
||
| 94 | */ |
||
| 95 | private $_oldAttributes; |
||
| 96 | /** |
||
| 97 | * @var array related models indexed by the relation names |
||
| 98 | */ |
||
| 99 | private $_related = []; |
||
| 100 | /** |
||
| 101 | * @var array relation names indexed by their link attributes |
||
| 102 | */ |
||
| 103 | private $_relationsDependencies = []; |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | * @return static|null ActiveRecord instance matching the condition, or `null` if nothing matches. |
||
| 109 | */ |
||
| 110 | 174 | public static function findOne($condition) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | * @return static[] an array of ActiveRecord instances, or an empty array if nothing matches. |
||
| 118 | */ |
||
| 119 | public static function findAll($condition) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Finds ActiveRecord instance(s) by the given condition. |
||
| 126 | * This method is internally called by [[findOne()]] and [[findAll()]]. |
||
| 127 | * @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter |
||
| 128 | * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. |
||
| 129 | * @throws InvalidConfigException if there is no primary key defined |
||
| 130 | * @internal |
||
| 131 | */ |
||
| 132 | protected static function findByCondition($condition) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Updates the whole table using the provided attribute values and conditions. |
||
| 151 | * |
||
| 152 | * For example, to change the status to be 1 for all customers whose status is 2: |
||
| 153 | * |
||
| 154 | * ```php |
||
| 155 | * Customer::updateAll(['status' => 1], 'status = 2'); |
||
| 156 | * ``` |
||
| 157 | * |
||
| 158 | * @param array $attributes attribute values (name-value pairs) to be saved into the table |
||
| 159 | * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL. |
||
| 160 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
| 161 | * @return int the number of rows updated |
||
| 162 | * @throws NotSupportedException if not overridden |
||
| 163 | */ |
||
| 164 | public static function updateAll($attributes, $condition = '') |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Updates the whole table using the provided counter changes and conditions. |
||
| 171 | * |
||
| 172 | * For example, to increment all customers' age by 1, |
||
| 173 | * |
||
| 174 | * ```php |
||
| 175 | * Customer::updateAllCounters(['age' => 1]); |
||
| 176 | * ``` |
||
| 177 | * |
||
| 178 | * @param array $counters the counters to be updated (attribute name => increment value). |
||
| 179 | * Use negative values if you want to decrement the counters. |
||
| 180 | * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL. |
||
| 181 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
| 182 | * @return int the number of rows updated |
||
| 183 | * @throws NotSupportedException if not overrided |
||
| 184 | */ |
||
| 185 | public static function updateAllCounters($counters, $condition = '') |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Deletes rows in the table using the provided conditions. |
||
| 192 | * WARNING: If you do not specify any condition, this method will delete ALL rows in the table. |
||
| 193 | * |
||
| 194 | * For example, to delete all customers whose status is 3: |
||
| 195 | * |
||
| 196 | * ```php |
||
| 197 | * Customer::deleteAll('status = 3'); |
||
| 198 | * ``` |
||
| 199 | * |
||
| 200 | * @param string|array $condition the conditions that will be put in the WHERE part of the DELETE SQL. |
||
| 201 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
| 202 | * @return int the number of rows deleted |
||
| 203 | * @throws NotSupportedException if not overridden. |
||
| 204 | */ |
||
| 205 | public static function deleteAll($condition = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns the name of the column that stores the lock version for implementing optimistic locking. |
||
| 212 | * |
||
| 213 | * Optimistic locking allows multiple users to access the same record for edits and avoids |
||
| 214 | * potential conflicts. In case when a user attempts to save the record upon some staled data |
||
| 215 | * (because another user has modified the data), a [[StaleObjectException]] exception will be thrown, |
||
| 216 | * and the update or deletion is skipped. |
||
| 217 | * |
||
| 218 | * Optimistic locking is only supported by [[update()]] and [[delete()]]. |
||
| 219 | * |
||
| 220 | * To use Optimistic locking: |
||
| 221 | * |
||
| 222 | * 1. Create a column to store the version number of each row. The column type should be `BIGINT DEFAULT 0`. |
||
| 223 | * Override this method to return the name of this column. |
||
| 224 | * 2. Add a `required` validation rule for the version column to ensure the version value is submitted. |
||
| 225 | * 3. In the Web form that collects the user input, add a hidden field that stores |
||
| 226 | * the lock version of the recording being updated. |
||
| 227 | * 4. In the controller action that does the data updating, try to catch the [[StaleObjectException]] |
||
| 228 | * and implement necessary business logic (e.g. merging the changes, prompting stated data) |
||
| 229 | * to resolve the conflict. |
||
| 230 | * |
||
| 231 | * @return string the column name that stores the lock version of a table row. |
||
| 232 | * If `null` is returned (default implemented), optimistic locking will not be supported. |
||
| 233 | */ |
||
| 234 | 28 | public function optimisticLock() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | 3 | public function canGetProperty($name, $checkVars = true, $checkBehaviors = true) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | 9 | public function canSetProperty($name, $checkVars = true, $checkBehaviors = true) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * PHP getter magic method. |
||
| 275 | * This method is overridden so that attributes and related objects can be accessed like properties. |
||
| 276 | * |
||
| 277 | * @param string $name property name |
||
| 278 | * @throws InvalidArgumentException if relation name is wrong |
||
| 279 | * @return mixed property value |
||
| 280 | * @see getAttribute() |
||
| 281 | */ |
||
| 282 | 351 | public function __get($name) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * PHP setter magic method. |
||
| 306 | * This method is overridden so that AR attributes can be accessed like properties. |
||
| 307 | * @param string $name property name |
||
| 308 | * @param mixed $value property value |
||
| 309 | */ |
||
| 310 | 178 | public function __set($name, $value) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Checks if a property value is null. |
||
| 327 | * This method overrides the parent implementation by checking if the named attribute is `null` or not. |
||
| 328 | * @param string $name the property name or the event name |
||
| 329 | * @return bool whether the property value is null |
||
| 330 | */ |
||
| 331 | 56 | public function __isset($name) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Sets a component property to be null. |
||
| 342 | * This method overrides the parent implementation by clearing |
||
| 343 | * the specified attribute value. |
||
| 344 | * @param string $name the property name or the event name |
||
| 345 | */ |
||
| 346 | 15 | public function __unset($name) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Declares a `has-one` relation. |
||
| 362 | * The declaration is returned in terms of a relational [[ActiveQuery]] instance |
||
| 363 | * through which the related record can be queried and retrieved back. |
||
| 364 | * |
||
| 365 | * A `has-one` relation means that there is at most one related record matching |
||
| 366 | * the criteria set by this relation, e.g., a customer has one country. |
||
| 367 | * |
||
| 368 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
| 369 | * the following code in the `Customer` class: |
||
| 370 | * |
||
| 371 | * ```php |
||
| 372 | * public function getCountry() |
||
| 373 | * { |
||
| 374 | * return $this->hasOne(Country::className(), ['id' => 'country_id']); |
||
| 375 | * } |
||
| 376 | * ``` |
||
| 377 | * |
||
| 378 | * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name |
||
| 379 | * in the related class `Country`, while the 'country_id' value refers to an attribute name |
||
| 380 | * in the current AR class. |
||
| 381 | * |
||
| 382 | * Call methods declared in [[ActiveQuery]] to further customize the relation. |
||
| 383 | * |
||
| 384 | * @param string $class the class name of the related record |
||
| 385 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
| 386 | * the attributes of the record associated with the `$class` model, while the values of the |
||
| 387 | * array refer to the corresponding attributes in **this** AR class. |
||
| 388 | * @return ActiveQueryInterface the relational query object. |
||
| 389 | */ |
||
| 390 | 64 | public function hasOne($class, $link) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Declares a `has-many` relation. |
||
| 397 | * The declaration is returned in terms of a relational [[ActiveQuery]] instance |
||
| 398 | * through which the related record can be queried and retrieved back. |
||
| 399 | * |
||
| 400 | * A `has-many` relation means that there are multiple related records matching |
||
| 401 | * the criteria set by this relation, e.g., a customer has many orders. |
||
| 402 | * |
||
| 403 | * For example, to declare the `orders` relation for `Customer` class, we can write |
||
| 404 | * the following code in the `Customer` class: |
||
| 405 | * |
||
| 406 | * ```php |
||
| 407 | * public function getOrders() |
||
| 408 | * { |
||
| 409 | * return $this->hasMany(Order::className(), ['customer_id' => 'id']); |
||
| 410 | * } |
||
| 411 | * ``` |
||
| 412 | * |
||
| 413 | * Note that in the above, the 'customer_id' key in the `$link` parameter refers to |
||
| 414 | * an attribute name in the related class `Order`, while the 'id' value refers to |
||
| 415 | * an attribute name in the current AR class. |
||
| 416 | * |
||
| 417 | * Call methods declared in [[ActiveQuery]] to further customize the relation. |
||
| 418 | * |
||
| 419 | * @param string $class the class name of the related record |
||
| 420 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
| 421 | * the attributes of the record associated with the `$class` model, while the values of the |
||
| 422 | * array refer to the corresponding attributes in **this** AR class. |
||
| 423 | * @return ActiveQueryInterface the relational query object. |
||
| 424 | */ |
||
| 425 | 138 | public function hasMany($class, $link) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Creates a query instance for `has-one` or `has-many` relation. |
||
| 432 | * @param string $class the class name of the related record. |
||
| 433 | * @param array $link the primary-foreign key constraint. |
||
| 434 | * @param bool $multiple whether this query represents a relation to more than one record. |
||
| 435 | * @return ActiveQueryInterface the relational query object. |
||
| 436 | * @since 2.0.12 |
||
| 437 | * @see hasOne() |
||
| 438 | * @see hasMany() |
||
| 439 | */ |
||
| 440 | 160 | protected function createRelationQuery($class, $link, $multiple) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Populates the named relation with the related records. |
||
| 453 | * Note that this method does not check if the relation exists or not. |
||
| 454 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
| 455 | * @param ActiveRecordInterface|array|null $records the related records to be populated into the relation. |
||
| 456 | * @see getRelation() |
||
| 457 | */ |
||
| 458 | 111 | public function populateRelation($name, $records) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Check whether the named relation has been populated with records. |
||
| 465 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
| 466 | * @return bool whether relation has been populated with records. |
||
| 467 | * @see getRelation() |
||
| 468 | */ |
||
| 469 | 45 | public function isRelationPopulated($name) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Returns all populated related records. |
||
| 476 | * @return array an array of related records indexed by relation names. |
||
| 477 | * @see getRelation() |
||
| 478 | */ |
||
| 479 | 6 | public function getRelatedRecords() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Returns a value indicating whether the model has an attribute with the specified name. |
||
| 486 | * @param string $name the name of the attribute |
||
| 487 | * @return bool whether the model has an attribute with the specified name. |
||
| 488 | */ |
||
| 489 | 278 | public function hasAttribute($name) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Returns the named attribute value. |
||
| 496 | * If this record is the result of a query and the attribute is not loaded, |
||
| 497 | * `null` will be returned. |
||
| 498 | * @param string $name the attribute name |
||
| 499 | * @return mixed the attribute value. `null` if the attribute is not set or does not exist. |
||
| 500 | * @see hasAttribute() |
||
| 501 | */ |
||
| 502 | public function getAttribute($name) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Sets the named attribute value. |
||
| 509 | * @param string $name the attribute name |
||
| 510 | * @param mixed $value the attribute value. |
||
| 511 | * @throws InvalidArgumentException if the named attribute does not exist. |
||
| 512 | * @see hasAttribute() |
||
| 513 | */ |
||
| 514 | 77 | public function setAttribute($name, $value) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Returns the old attribute values. |
||
| 531 | * @return array the old attribute values (name-value pairs) |
||
| 532 | */ |
||
| 533 | public function getOldAttributes() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Sets the old attribute values. |
||
| 540 | * All existing old attribute values will be discarded. |
||
| 541 | * @param array|null $values old attribute values to be set. |
||
| 542 | * If set to `null` this record is considered to be [[isNewRecord|new]]. |
||
| 543 | */ |
||
| 544 | 91 | public function setOldAttributes($values) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Returns the old value of the named attribute. |
||
| 551 | * If this record is the result of a query and the attribute is not loaded, |
||
| 552 | * `null` will be returned. |
||
| 553 | * @param string $name the attribute name |
||
| 554 | * @return mixed the old attribute value. `null` if the attribute is not loaded before |
||
| 555 | * or does not exist. |
||
| 556 | * @see hasAttribute() |
||
| 557 | */ |
||
| 558 | public function getOldAttribute($name) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Sets the old value of the named attribute. |
||
| 565 | * @param string $name the attribute name |
||
| 566 | * @param mixed $value the old attribute value. |
||
| 567 | * @throws InvalidArgumentException if the named attribute does not exist. |
||
| 568 | * @see hasAttribute() |
||
| 569 | */ |
||
| 570 | public function setOldAttribute($name, $value) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Marks an attribute dirty. |
||
| 581 | * This method may be called to force updating a record when calling [[update()]], |
||
| 582 | * even if there is no change being made to the record. |
||
| 583 | * @param string $name the attribute name |
||
| 584 | */ |
||
| 585 | public function markAttributeDirty($name) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns a value indicating whether the named attribute has been changed. |
||
| 592 | * @param string $name the name of the attribute. |
||
| 593 | * @param bool $identical whether the comparison of new and old value is made for |
||
| 594 | * identical values using `===`, defaults to `true`. Otherwise `==` is used for comparison. |
||
| 595 | * This parameter is available since version 2.0.4. |
||
| 596 | * @return bool whether the attribute has been changed |
||
| 597 | */ |
||
| 598 | 2 | public function isAttributeChanged($name, $identical = true) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Returns the attribute values that have been modified since they are loaded or saved most recently. |
||
| 613 | * |
||
| 614 | * The comparison of new and old values is made for identical values using `===`. |
||
| 615 | * |
||
| 616 | * @param string[]|null $names the names of the attributes whose values may be returned if they are |
||
| 617 | * changed recently. If null, [[attributes()]] will be used. |
||
| 618 | * @return array the changed attribute values (name-value pairs) |
||
| 619 | */ |
||
| 620 | 101 | public function getDirtyAttributes($names = null) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Saves the current record. |
||
| 646 | * |
||
| 647 | * This method will call [[insert()]] when [[isNewRecord]] is `true`, or [[update()]] |
||
| 648 | * when [[isNewRecord]] is `false`. |
||
| 649 | * |
||
| 650 | * For example, to save a customer record: |
||
| 651 | * |
||
| 652 | * ```php |
||
| 653 | * $customer = new Customer; // or $customer = Customer::findOne($id); |
||
| 654 | * $customer->name = $name; |
||
| 655 | * $customer->email = $email; |
||
| 656 | * $customer->save(); |
||
| 657 | * ``` |
||
| 658 | * |
||
| 659 | * @param bool $runValidation whether to perform validation (calling [[validate()]]) |
||
| 660 | * before saving the record. Defaults to `true`. If the validation fails, the record |
||
| 661 | * will not be saved to the database and this method will return `false`. |
||
| 662 | * @param array $attributeNames list of attribute names that need to be saved. Defaults to null, |
||
| 663 | * meaning all attributes that are loaded from DB will be saved. |
||
| 664 | * @return bool whether the saving succeeded (i.e. no validation errors occurred). |
||
| 665 | */ |
||
| 666 | 95 | public function save($runValidation = true, $attributeNames = null) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Saves the changes to this active record into the associated database table. |
||
| 677 | * |
||
| 678 | * This method performs the following steps in order: |
||
| 679 | * |
||
| 680 | * 1. call [[beforeValidate()]] when `$runValidation` is `true`. If [[beforeValidate()]] |
||
| 681 | * returns `false`, the rest of the steps will be skipped; |
||
| 682 | * 2. call [[afterValidate()]] when `$runValidation` is `true`. If validation |
||
| 683 | * failed, the rest of the steps will be skipped; |
||
| 684 | * 3. call [[beforeSave()]]. If [[beforeSave()]] returns `false`, |
||
| 685 | * the rest of the steps will be skipped; |
||
| 686 | * 4. save the record into database. If this fails, it will skip the rest of the steps; |
||
| 687 | * 5. call [[afterSave()]]; |
||
| 688 | * |
||
| 689 | * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]], |
||
| 690 | * [[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_UPDATE]], and [[EVENT_AFTER_UPDATE]] |
||
| 691 | * will be raised by the corresponding methods. |
||
| 692 | * |
||
| 693 | * Only the [[dirtyAttributes|changed attribute values]] will be saved into database. |
||
| 694 | * |
||
| 695 | * For example, to update a customer record: |
||
| 696 | * |
||
| 697 | * ```php |
||
| 698 | * $customer = Customer::findOne($id); |
||
| 699 | * $customer->name = $name; |
||
| 700 | * $customer->email = $email; |
||
| 701 | * $customer->update(); |
||
| 702 | * ``` |
||
| 703 | * |
||
| 704 | * Note that it is possible the update does not affect any row in the table. |
||
| 705 | * In this case, this method will return 0. For this reason, you should use the following |
||
| 706 | * code to check if update() is successful or not: |
||
| 707 | * |
||
| 708 | * ```php |
||
| 709 | * if ($customer->update() !== false) { |
||
| 710 | * // update successful |
||
| 711 | * } else { |
||
| 712 | * // update failed |
||
| 713 | * } |
||
| 714 | * ``` |
||
| 715 | * |
||
| 716 | * @param bool $runValidation whether to perform validation (calling [[validate()]]) |
||
| 717 | * before saving the record. Defaults to `true`. If the validation fails, the record |
||
| 718 | * will not be saved to the database and this method will return `false`. |
||
| 719 | * @param array $attributeNames list of attribute names that need to be saved. Defaults to null, |
||
| 720 | * meaning all attributes that are loaded from DB will be saved. |
||
| 721 | * @return int|false the number of rows affected, or `false` if validation fails |
||
| 722 | * or [[beforeSave()]] stops the updating process. |
||
| 723 | * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data |
||
| 724 | * being updated is outdated. |
||
| 725 | * @throws Exception in case update failed. |
||
| 726 | */ |
||
| 727 | public function update($runValidation = true, $attributeNames = null) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Updates the specified attributes. |
||
| 738 | * |
||
| 739 | * This method is a shortcut to [[update()]] when data validation is not needed |
||
| 740 | * and only a small set attributes need to be updated. |
||
| 741 | * |
||
| 742 | * You may specify the attributes to be updated as name list or name-value pairs. |
||
| 743 | * If the latter, the corresponding attribute values will be modified accordingly. |
||
| 744 | * The method will then save the specified attributes into database. |
||
| 745 | * |
||
| 746 | * Note that this method will **not** perform data validation and will **not** trigger events. |
||
| 747 | * |
||
| 748 | * @param array $attributes the attributes (names or name-value pairs) to be updated |
||
| 749 | * @return int the number of rows affected. |
||
| 750 | */ |
||
| 751 | 4 | public function updateAttributes($attributes) |
|
| 776 | |||
| 777 | /** |
||
| 778 | * @see update() |
||
| 779 | * @param array $attributes attributes to update |
||
| 780 | * @return int|false the number of rows affected, or false if [[beforeSave()]] stops the updating process. |
||
| 781 | * @throws StaleObjectException |
||
| 782 | */ |
||
| 783 | 30 | protected function updateInternal($attributes = null) |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Updates one or several counter columns for the current AR object. |
||
| 823 | * Note that this method differs from [[updateAllCounters()]] in that it only |
||
| 824 | * saves counters for the current AR object. |
||
| 825 | * |
||
| 826 | * An example usage is as follows: |
||
| 827 | * |
||
| 828 | * ```php |
||
| 829 | * $post = Post::findOne($id); |
||
| 830 | * $post->updateCounters(['view_count' => 1]); |
||
| 831 | * ``` |
||
| 832 | * |
||
| 833 | * @param array $counters the counters to be updated (attribute name => increment value) |
||
| 834 | * Use negative values if you want to decrement the counters. |
||
| 835 | * @return bool whether the saving is successful |
||
| 836 | * @see updateAllCounters() |
||
| 837 | */ |
||
| 838 | 6 | public function updateCounters($counters) |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Deletes the table row corresponding to this active record. |
||
| 858 | * |
||
| 859 | * This method performs the following steps in order: |
||
| 860 | * |
||
| 861 | * 1. call [[beforeDelete()]]. If the method returns `false`, it will skip the |
||
| 862 | * rest of the steps; |
||
| 863 | * 2. delete the record from the database; |
||
| 864 | * 3. call [[afterDelete()]]. |
||
| 865 | * |
||
| 866 | * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]] |
||
| 867 | * will be raised by the corresponding methods. |
||
| 868 | * |
||
| 869 | * @return int|false the number of rows deleted, or `false` if the deletion is unsuccessful for some reason. |
||
| 870 | * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful. |
||
| 871 | * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data |
||
| 872 | * being deleted is outdated. |
||
| 873 | * @throws Exception in case delete failed. |
||
| 874 | */ |
||
| 875 | public function delete() |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Returns a value indicating whether the current record is new. |
||
| 899 | * @return bool whether the record is new and should be inserted when calling [[save()]]. |
||
| 900 | */ |
||
| 901 | 135 | public function getIsNewRecord() |
|
| 905 | |||
| 906 | /** |
||
| 907 | * Sets the value indicating whether the record is new. |
||
| 908 | * @param bool $value whether the record is new and should be inserted when calling [[save()]]. |
||
| 909 | * @see getIsNewRecord() |
||
| 910 | */ |
||
| 911 | public function setIsNewRecord($value) |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Initializes the object. |
||
| 918 | * This method is called at the end of the constructor. |
||
| 919 | * The default implementation will trigger an [[EVENT_INIT]] event. |
||
| 920 | */ |
||
| 921 | 380 | public function init() |
|
| 926 | |||
| 927 | /** |
||
| 928 | * This method is called when the AR object is created and populated with the query result. |
||
| 929 | * The default implementation will trigger an [[EVENT_AFTER_FIND]] event. |
||
| 930 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
| 931 | * event is triggered. |
||
| 932 | */ |
||
| 933 | 297 | public function afterFind() |
|
| 937 | |||
| 938 | /** |
||
| 939 | * This method is called at the beginning of inserting or updating a record. |
||
| 940 | * |
||
| 941 | * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is `true`, |
||
| 942 | * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is `false`. |
||
| 943 | * When overriding this method, make sure you call the parent implementation like the following: |
||
| 944 | * |
||
| 945 | * ```php |
||
| 946 | * public function beforeSave($insert) |
||
| 947 | * { |
||
| 948 | * if (!parent::beforeSave($insert)) { |
||
| 949 | * return false; |
||
| 950 | * } |
||
| 951 | * |
||
| 952 | * // ...custom code here... |
||
| 953 | * return true; |
||
| 954 | * } |
||
| 955 | * ``` |
||
| 956 | * |
||
| 957 | * @param bool $insert whether this method called while inserting a record. |
||
| 958 | * If `false`, it means the method is called while updating a record. |
||
| 959 | * @return bool whether the insertion or updating should continue. |
||
| 960 | * If `false`, the insertion or updating will be cancelled. |
||
| 961 | */ |
||
| 962 | 105 | public function beforeSave($insert) |
|
| 969 | |||
| 970 | /** |
||
| 971 | * This method is called at the end of inserting or updating a record. |
||
| 972 | * The default implementation will trigger an [[EVENT_AFTER_INSERT]] event when `$insert` is `true`, |
||
| 973 | * or an [[EVENT_AFTER_UPDATE]] event if `$insert` is `false`. The event class used is [[AfterSaveEvent]]. |
||
| 974 | * When overriding this method, make sure you call the parent implementation so that |
||
| 975 | * the event is triggered. |
||
| 976 | * @param bool $insert whether this method called while inserting a record. |
||
| 977 | * If `false`, it means the method is called while updating a record. |
||
| 978 | * @param array $changedAttributes The old values of attributes that had changed and were saved. |
||
| 979 | * You can use this parameter to take action based on the changes made for example send an email |
||
| 980 | * when the password had changed or implement audit trail that tracks all the changes. |
||
| 981 | * `$changedAttributes` gives you the old attribute values while the active record (`$this`) has |
||
| 982 | * already the new, updated values. |
||
| 983 | * |
||
| 984 | * Note that no automatic type conversion performed by default. You may use |
||
| 985 | * [[\yii\behaviors\AttributeTypecastBehavior]] to facilitate attribute typecasting. |
||
| 986 | * See http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#attributes-typecasting. |
||
| 987 | */ |
||
| 988 | 98 | public function afterSave($insert, $changedAttributes) |
|
| 994 | |||
| 995 | /** |
||
| 996 | * This method is invoked before deleting a record. |
||
| 997 | * |
||
| 998 | * The default implementation raises the [[EVENT_BEFORE_DELETE]] event. |
||
| 999 | * When overriding this method, make sure you call the parent implementation like the following: |
||
| 1000 | * |
||
| 1001 | * ```php |
||
| 1002 | * public function beforeDelete() |
||
| 1003 | * { |
||
| 1004 | * if (!parent::beforeDelete()) { |
||
| 1005 | * return false; |
||
| 1006 | * } |
||
| 1007 | * |
||
| 1008 | * // ...custom code here... |
||
| 1009 | * return true; |
||
| 1010 | * } |
||
| 1011 | * ``` |
||
| 1012 | * |
||
| 1013 | * @return bool whether the record should be deleted. Defaults to `true`. |
||
| 1014 | */ |
||
| 1015 | 6 | public function beforeDelete() |
|
| 1022 | |||
| 1023 | /** |
||
| 1024 | * This method is invoked after deleting a record. |
||
| 1025 | * The default implementation raises the [[EVENT_AFTER_DELETE]] event. |
||
| 1026 | * You may override this method to do postprocessing after the record is deleted. |
||
| 1027 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 1028 | */ |
||
| 1029 | 6 | public function afterDelete() |
|
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Repopulates this active record with the latest data. |
||
| 1036 | * |
||
| 1037 | * If the refresh is successful, an [[EVENT_AFTER_REFRESH]] event will be triggered. |
||
| 1038 | * This event is available since version 2.0.8. |
||
| 1039 | * |
||
| 1040 | * @return bool whether the row still exists in the database. If `true`, the latest data |
||
| 1041 | * will be populated to this active record. Otherwise, this record will remain unchanged. |
||
| 1042 | */ |
||
| 1043 | public function refresh() |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Repopulates this active record with the latest data from a newly fetched instance. |
||
| 1052 | * @param BaseActiveRecord $record the record to take attributes from. |
||
| 1053 | * @return bool whether refresh was successful. |
||
| 1054 | * @see refresh() |
||
| 1055 | * @since 2.0.13 |
||
| 1056 | */ |
||
| 1057 | 28 | protected function refreshInternal($record) |
|
| 1072 | |||
| 1073 | /** |
||
| 1074 | * This method is called when the AR object is refreshed. |
||
| 1075 | * The default implementation will trigger an [[EVENT_AFTER_REFRESH]] event. |
||
| 1076 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
| 1077 | * event is triggered. |
||
| 1078 | * @since 2.0.8 |
||
| 1079 | */ |
||
| 1080 | 28 | public function afterRefresh() |
|
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Returns a value indicating whether the given active record is the same as the current one. |
||
| 1087 | * The comparison is made by comparing the table names and the primary key values of the two active records. |
||
| 1088 | * If one of the records [[isNewRecord|is new]] they are also considered not equal. |
||
| 1089 | * @param ActiveRecordInterface $record record to compare to |
||
| 1090 | * @return bool whether the two active records refer to the same row in the same database table. |
||
| 1091 | */ |
||
| 1092 | public function equals($record) |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Returns the primary key value(s). |
||
| 1103 | * @param bool $asArray whether to return the primary key value as an array. If `true`, |
||
| 1104 | * the return value will be an array with column names as keys and column values as values. |
||
| 1105 | * Note that for composite primary keys, an array will always be returned regardless of this parameter value. |
||
| 1106 | * @property mixed The primary key value. An array (column name => column value) is returned if |
||
| 1107 | * the primary key is composite. A string is returned otherwise (null will be returned if |
||
| 1108 | * the key value is null). |
||
| 1109 | * @return mixed the primary key value. An array (column name => column value) is returned if the primary key |
||
| 1110 | * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if |
||
| 1111 | * the key value is null). |
||
| 1112 | */ |
||
| 1113 | 44 | public function getPrimaryKey($asArray = false) |
|
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Returns the old primary key value(s). |
||
| 1130 | * This refers to the primary key value that is populated into the record |
||
| 1131 | * after executing a find method (e.g. find(), findOne()). |
||
| 1132 | * The value remains unchanged even if the primary key attribute is manually assigned with a different value. |
||
| 1133 | * @param bool $asArray whether to return the primary key value as an array. If `true`, |
||
| 1134 | * the return value will be an array with column name as key and column value as value. |
||
| 1135 | * If this is `false` (default), a scalar value will be returned for non-composite primary key. |
||
| 1136 | * @property mixed The old primary key value. An array (column name => column value) is |
||
| 1137 | * returned if the primary key is composite. A string is returned otherwise (null will be |
||
| 1138 | * returned if the key value is null). |
||
| 1139 | * @return mixed the old primary key value. An array (column name => column value) is returned if the primary key |
||
| 1140 | * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if |
||
| 1141 | * the key value is null). |
||
| 1142 | * @throws Exception if the AR model does not have a primary key |
||
| 1143 | */ |
||
| 1144 | 59 | public function getOldPrimaryKey($asArray = false) |
|
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Populates an active record object using a row of data from the database/storage. |
||
| 1164 | * |
||
| 1165 | * This is an internal method meant to be called to create active record objects after |
||
| 1166 | * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate |
||
| 1167 | * the query results into active records. |
||
| 1168 | * |
||
| 1169 | * When calling this method manually you should call [[afterFind()]] on the created |
||
| 1170 | * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]]. |
||
| 1171 | * |
||
| 1172 | * @param BaseActiveRecord $record the record to be populated. In most cases this will be an instance |
||
| 1173 | * created by [[instantiate()]] beforehand. |
||
| 1174 | * @param array $row attribute values (name => value) |
||
| 1175 | */ |
||
| 1176 | 297 | public static function populateRecord($record, $row) |
|
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Creates an active record instance. |
||
| 1193 | * |
||
| 1194 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
| 1195 | * It is not meant to be used for creating new records directly. |
||
| 1196 | * |
||
| 1197 | * You may override this method if the instance being created |
||
| 1198 | * depends on the row data to be populated into the record. |
||
| 1199 | * For example, by creating a record based on the value of a column, |
||
| 1200 | * you may implement the so-called single-table inheritance mapping. |
||
| 1201 | * @param array $row row data to be populated into the record. |
||
| 1202 | * @return static the newly created active record |
||
| 1203 | */ |
||
| 1204 | 291 | public static function instantiate($row) |
|
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Returns whether there is an element at the specified offset. |
||
| 1211 | * This method is required by the interface [[\ArrayAccess]]. |
||
| 1212 | * @param mixed $offset the offset to check on |
||
| 1213 | * @return bool whether there is an element at the specified offset. |
||
| 1214 | */ |
||
| 1215 | 30 | public function offsetExists($offset) |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Returns the relation object with the specified name. |
||
| 1222 | * A relation is defined by a getter method which returns an [[ActiveQueryInterface]] object. |
||
| 1223 | * It can be declared in either the Active Record class itself or one of its behaviors. |
||
| 1224 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
| 1225 | * @param bool $throwException whether to throw exception if the relation does not exist. |
||
| 1226 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
| 1227 | * and `$throwException` is `false`, `null` will be returned. |
||
| 1228 | * @throws InvalidArgumentException if the named relation does not exist. |
||
| 1229 | */ |
||
| 1230 | 141 | public function getRelation($name, $throwException = true) |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Establishes the relationship between two models. |
||
| 1269 | * |
||
| 1270 | * The relationship is established by setting the foreign key value(s) in one model |
||
| 1271 | * to be the corresponding primary key value(s) in the other model. |
||
| 1272 | * The model with the foreign key will be saved into database without performing validation. |
||
| 1273 | * |
||
| 1274 | * If the relationship involves a junction table, a new row will be inserted into the |
||
| 1275 | * junction table which contains the primary key values from both models. |
||
| 1276 | * |
||
| 1277 | * Note that this method requires that the primary key value is not null. |
||
| 1278 | * |
||
| 1279 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
| 1280 | * @param ActiveRecordInterface $model the model to be linked with the current one. |
||
| 1281 | * @param array $extraColumns additional column values to be saved into the junction table. |
||
| 1282 | * This parameter is only meaningful for a relationship involving a junction table |
||
| 1283 | * (i.e., a relation set with [[ActiveRelationTrait::via()]] or [[ActiveQuery::viaTable()]].) |
||
| 1284 | * @throws InvalidCallException if the method is unable to link two models. |
||
| 1285 | */ |
||
| 1286 | 9 | public function link($name, $model, $extraColumns = []) |
|
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Destroys the relationship between two models. |
||
| 1366 | * |
||
| 1367 | * The model with the foreign key of the relationship will be deleted if `$delete` is `true`. |
||
| 1368 | * Otherwise, the foreign key will be set `null` and the model will be saved without validation. |
||
| 1369 | * |
||
| 1370 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
| 1371 | * @param ActiveRecordInterface $model the model to be unlinked from the current one. |
||
| 1372 | * You have to make sure that the model is really related with the current model as this method |
||
| 1373 | * does not check this. |
||
| 1374 | * @param bool $delete whether to delete the model that contains the foreign key. |
||
| 1375 | * If `false`, the model's foreign key will be set `null` and saved. |
||
| 1376 | * If `true`, the model containing the foreign key will be deleted. |
||
| 1377 | * @throws InvalidCallException if the models cannot be unlinked |
||
| 1378 | */ |
||
| 1379 | 3 | public function unlink($name, $model, $delete = false) |
|
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Destroys the relationship in current model. |
||
| 1465 | * |
||
| 1466 | * The model with the foreign key of the relationship will be deleted if `$delete` is `true`. |
||
| 1467 | * Otherwise, the foreign key will be set `null` and the model will be saved without validation. |
||
| 1468 | * |
||
| 1469 | * Note that to destroy the relationship without removing records make sure your keys can be set to null |
||
| 1470 | * |
||
| 1471 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
| 1472 | * @param bool $delete whether to delete the model that contains the foreign key. |
||
| 1473 | * |
||
| 1474 | * Note that the deletion will be performed using [[deleteAll()]], which will not trigger any events on the related models. |
||
| 1475 | * If you need [[EVENT_BEFORE_DELETE]] or [[EVENT_AFTER_DELETE]] to be triggered, you need to [[find()|find]] the models first |
||
| 1476 | * and then call [[delete()]] on each of them. |
||
| 1477 | */ |
||
| 1478 | 18 | public function unlinkAll($name, $delete = false) |
|
| 1551 | |||
| 1552 | /** |
||
| 1553 | * @param array $link |
||
| 1554 | * @param ActiveRecordInterface $foreignModel |
||
| 1555 | * @param ActiveRecordInterface $primaryModel |
||
| 1556 | * @throws InvalidCallException |
||
| 1557 | */ |
||
| 1558 | 9 | private function bindModels($link, $foreignModel, $primaryModel) |
|
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Returns a value indicating whether the given set of attributes represents the primary key for this model. |
||
| 1576 | * @param array $keys the set of attributes to check |
||
| 1577 | * @return bool whether the given set of attributes represents the primary key for this model |
||
| 1578 | */ |
||
| 1579 | 15 | public static function isPrimaryKey($keys) |
|
| 1588 | |||
| 1589 | /** |
||
| 1590 | * Returns the text label for the specified attribute. |
||
| 1591 | * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. |
||
| 1592 | * @param string $attribute the attribute name |
||
| 1593 | * @return string the attribute label |
||
| 1594 | * @see generateAttributeLabel() |
||
| 1595 | * @see attributeLabels() |
||
| 1596 | */ |
||
| 1597 | 51 | public function getAttributeLabel($attribute) |
|
| 1630 | |||
| 1631 | /** |
||
| 1632 | * Returns the text hint for the specified attribute. |
||
| 1633 | * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. |
||
| 1634 | * @param string $attribute the attribute name |
||
| 1635 | * @return string the attribute hint |
||
| 1636 | * @see attributeHints() |
||
| 1637 | * @since 2.0.4 |
||
| 1638 | */ |
||
| 1639 | public function getAttributeHint($attribute) |
||
| 1672 | |||
| 1673 | /** |
||
| 1674 | * {@inheritdoc} |
||
| 1675 | * |
||
| 1676 | * The default implementation returns the names of the columns whose values have been populated into this record. |
||
| 1677 | */ |
||
| 1678 | public function fields() |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * {@inheritdoc} |
||
| 1687 | * |
||
| 1688 | * The default implementation returns the names of the relations that have been populated into this record. |
||
| 1689 | */ |
||
| 1690 | public function extraFields() |
||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * Sets the element value at the specified offset to null. |
||
| 1699 | * This method is required by the SPL interface [[\ArrayAccess]]. |
||
| 1700 | * It is implicitly called when you use something like `unset($model[$offset])`. |
||
| 1701 | * @param mixed $offset the offset to unset element |
||
| 1702 | */ |
||
| 1703 | 3 | public function offsetUnset($offset) |
|
| 1711 | |||
| 1712 | /** |
||
| 1713 | * Resets dependent related models checking if their links contain specific attribute. |
||
| 1714 | * @param string $attribute The changed attribute name. |
||
| 1715 | */ |
||
| 1716 | 15 | private function resetDependentRelations($attribute) |
|
| 1723 | |||
| 1724 | /** |
||
| 1725 | * Sets relation dependencies for a property |
||
| 1726 | * @param string $name property name |
||
| 1727 | * @param ActiveQueryInterface $relation relation instance |
||
| 1728 | */ |
||
| 1729 | 67 | private function setRelationDependencies($name, $relation) |
|
| 1742 | } |
||
| 1743 |