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 |
||
43 | abstract class BaseActiveRecord extends Model implements ActiveRecordInterface |
||
44 | { |
||
45 | /** |
||
46 | * @event Event an event that is triggered when the record is initialized via [[init()]]. |
||
47 | */ |
||
48 | const EVENT_INIT = 'init'; |
||
49 | /** |
||
50 | * @event Event an event that is triggered after the record is created and populated with query result. |
||
51 | */ |
||
52 | const EVENT_AFTER_FIND = 'afterFind'; |
||
53 | /** |
||
54 | * @event ModelEvent an event that is triggered before inserting a record. |
||
55 | * You may set [[ModelEvent::isValid]] to be `false` to stop the insertion. |
||
56 | */ |
||
57 | const EVENT_BEFORE_INSERT = 'beforeInsert'; |
||
58 | /** |
||
59 | * @event AfterSaveEvent an event that is triggered after a record is inserted. |
||
60 | */ |
||
61 | const EVENT_AFTER_INSERT = 'afterInsert'; |
||
62 | /** |
||
63 | * @event ModelEvent an event that is triggered before updating a record. |
||
64 | * You may set [[ModelEvent::isValid]] to be `false` to stop the update. |
||
65 | */ |
||
66 | const EVENT_BEFORE_UPDATE = 'beforeUpdate'; |
||
67 | /** |
||
68 | * @event AfterSaveEvent an event that is triggered after a record is updated. |
||
69 | */ |
||
70 | const EVENT_AFTER_UPDATE = 'afterUpdate'; |
||
71 | /** |
||
72 | * @event ModelEvent an event that is triggered before deleting a record. |
||
73 | * You may set [[ModelEvent::isValid]] to be `false` to stop the deletion. |
||
74 | */ |
||
75 | const EVENT_BEFORE_DELETE = 'beforeDelete'; |
||
76 | /** |
||
77 | * @event Event an event that is triggered after a record is deleted. |
||
78 | */ |
||
79 | const EVENT_AFTER_DELETE = 'afterDelete'; |
||
80 | /** |
||
81 | * @event Event an event that is triggered after a record is refreshed. |
||
82 | * @since 2.0.8 |
||
83 | */ |
||
84 | const EVENT_AFTER_REFRESH = 'afterRefresh'; |
||
85 | |||
86 | /** |
||
87 | * @var array attribute values indexed by attribute names |
||
88 | */ |
||
89 | private $_attributes = []; |
||
90 | /** |
||
91 | * @var array|null old attribute values indexed by attribute names. |
||
92 | * This is `null` if the record [[isNewRecord|is new]]. |
||
93 | */ |
||
94 | private $_oldAttributes; |
||
95 | /** |
||
96 | * @var array related models indexed by the relation names |
||
97 | */ |
||
98 | private $_related = []; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * @inheritdoc |
||
103 | * @return static ActiveRecord instance matching the condition, or `null` if nothing matches. |
||
104 | */ |
||
105 | 161 | public static function findOne($condition) |
|
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | * @return static[] an array of ActiveRecord instances, or an empty array if nothing matches. |
||
113 | */ |
||
114 | public static function findAll($condition) |
||
118 | |||
119 | /** |
||
120 | * Finds ActiveRecord instance(s) by the given condition. |
||
121 | * This method is internally called by [[findOne()]] and [[findAll()]]. |
||
122 | * @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter |
||
123 | * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. |
||
124 | * @throws InvalidConfigException if there is no primary key defined |
||
125 | * @internal |
||
126 | */ |
||
127 | protected static function findByCondition($condition) |
||
143 | |||
144 | /** |
||
145 | * Updates the whole table using the provided attribute values and conditions. |
||
146 | * For example, to change the status to be 1 for all customers whose status is 2: |
||
147 | * |
||
148 | * ```php |
||
149 | * Customer::updateAll(['status' => 1], 'status = 2'); |
||
150 | * ``` |
||
151 | * |
||
152 | * @param array $attributes attribute values (name-value pairs) to be saved into the table |
||
153 | * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL. |
||
154 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
155 | * @return int the number of rows updated |
||
156 | * @throws NotSupportedException if not overridden |
||
157 | */ |
||
158 | public static function updateAll($attributes, $condition = '') |
||
162 | |||
163 | /** |
||
164 | * Updates the whole table using the provided counter changes and conditions. |
||
165 | * For example, to increment all customers' age by 1, |
||
166 | * |
||
167 | * ```php |
||
168 | * Customer::updateAllCounters(['age' => 1]); |
||
169 | * ``` |
||
170 | * |
||
171 | * @param array $counters the counters to be updated (attribute name => increment value). |
||
172 | * Use negative values if you want to decrement the counters. |
||
173 | * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL. |
||
174 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
175 | * @return int the number of rows updated |
||
176 | * @throws NotSupportedException if not overrided |
||
177 | */ |
||
178 | public static function updateAllCounters($counters, $condition = '') |
||
182 | |||
183 | /** |
||
184 | * Deletes rows in the table using the provided conditions. |
||
185 | * WARNING: If you do not specify any condition, this method will delete ALL rows in the table. |
||
186 | * |
||
187 | * For example, to delete all customers whose status is 3: |
||
188 | * |
||
189 | * ```php |
||
190 | * Customer::deleteAll('status = 3'); |
||
191 | * ``` |
||
192 | * |
||
193 | * @param string|array $condition the conditions that will be put in the WHERE part of the DELETE SQL. |
||
194 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
195 | * @param array $params the parameters (name => value) to be bound to the query. |
||
196 | * @return int the number of rows deleted |
||
197 | * @throws NotSupportedException if not overrided |
||
198 | */ |
||
199 | public static function deleteAll($condition = '', $params = []) |
||
203 | |||
204 | /** |
||
205 | * Returns the name of the column that stores the lock version for implementing optimistic locking. |
||
206 | * |
||
207 | * Optimistic locking allows multiple users to access the same record for edits and avoids |
||
208 | * potential conflicts. In case when a user attempts to save the record upon some staled data |
||
209 | * (because another user has modified the data), a [[StaleObjectException]] exception will be thrown, |
||
210 | * and the update or deletion is skipped. |
||
211 | * |
||
212 | * Optimistic locking is only supported by [[update()]] and [[delete()]]. |
||
213 | * |
||
214 | * To use Optimistic locking: |
||
215 | * |
||
216 | * 1. Create a column to store the version number of each row. The column type should be `BIGINT DEFAULT 0`. |
||
217 | * Override this method to return the name of this column. |
||
218 | * 2. Add a `required` validation rule for the version column to ensure the version value is submitted. |
||
219 | * 3. In the Web form that collects the user input, add a hidden field that stores |
||
220 | * the lock version of the recording being updated. |
||
221 | * 4. In the controller action that does the data updating, try to catch the [[StaleObjectException]] |
||
222 | * and implement necessary business logic (e.g. merging the changes, prompting stated data) |
||
223 | * to resolve the conflict. |
||
224 | * |
||
225 | * @return string the column name that stores the lock version of a table row. |
||
226 | * If `null` is returned (default implemented), optimistic locking will not be supported. |
||
227 | */ |
||
228 | 23 | public function optimisticLock() |
|
232 | |||
233 | /** |
||
234 | * @inheritdoc |
||
235 | */ |
||
236 | 3 | public function canGetProperty($name, $checkVars = true, $checkBehaviors = true) |
|
237 | { |
||
238 | 3 | if (parent::canGetProperty($name, $checkVars, $checkBehaviors)) { |
|
239 | 3 | return true; |
|
240 | } |
||
241 | |||
242 | try { |
||
243 | 3 | return $this->hasAttribute($name); |
|
244 | } catch (\Exception $e) { |
||
245 | // `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used |
||
246 | return false; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | 9 | public function canSetProperty($name, $checkVars = true, $checkBehaviors = true) |
|
254 | { |
||
255 | 9 | if (parent::canSetProperty($name, $checkVars, $checkBehaviors)) { |
|
256 | 6 | return true; |
|
257 | } |
||
258 | |||
259 | try { |
||
260 | 3 | return $this->hasAttribute($name); |
|
261 | } catch (\Exception $e) { |
||
262 | // `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used |
||
263 | return false; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * PHP getter magic method. |
||
269 | * This method is overridden so that attributes and related objects can be accessed like properties. |
||
270 | * |
||
271 | * @param string $name property name |
||
272 | * @throws \yii\base\InvalidParamException if relation name is wrong |
||
273 | * @return mixed property value |
||
274 | * @see getAttribute() |
||
275 | */ |
||
276 | 273 | public function __get($name) |
|
294 | |||
295 | /** |
||
296 | * PHP setter magic method. |
||
297 | * This method is overridden so that AR attributes can be accessed like properties. |
||
298 | * @param string $name property name |
||
299 | * @param mixed $value property value |
||
300 | */ |
||
301 | 123 | public function __set($name, $value) |
|
302 | { |
||
303 | 123 | if ($this->hasAttribute($name)) { |
|
304 | 123 | $this->_attributes[$name] = $value; |
|
305 | 123 | } else { |
|
306 | 3 | parent::__set($name, $value); |
|
307 | } |
||
308 | 123 | } |
|
309 | |||
310 | /** |
||
311 | * Checks if a property value is null. |
||
312 | * This method overrides the parent implementation by checking if the named attribute is `null` or not. |
||
313 | * @param string $name the property name or the event name |
||
314 | * @return bool whether the property value is null |
||
315 | */ |
||
316 | 39 | public function __isset($name) |
|
324 | |||
325 | /** |
||
326 | * Sets a component property to be null. |
||
327 | * This method overrides the parent implementation by clearing |
||
328 | * the specified attribute value. |
||
329 | * @param string $name the property name or the event name |
||
330 | */ |
||
331 | 9 | public function __unset($name) |
|
341 | |||
342 | /** |
||
343 | * Declares a `has-one` relation. |
||
344 | * The declaration is returned in terms of a relational [[ActiveQuery]] instance |
||
345 | * through which the related record can be queried and retrieved back. |
||
346 | * |
||
347 | * A `has-one` relation means that there is at most one related record matching |
||
348 | * the criteria set by this relation, e.g., a customer has one country. |
||
349 | * |
||
350 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
351 | * the following code in the `Customer` class: |
||
352 | * |
||
353 | * ```php |
||
354 | * public function getCountry() |
||
355 | * { |
||
356 | * return $this->hasOne(Country::className(), ['id' => 'country_id']); |
||
357 | * } |
||
358 | * ``` |
||
359 | * |
||
360 | * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name |
||
361 | * in the related class `Country`, while the 'country_id' value refers to an attribute name |
||
362 | * in the current AR class. |
||
363 | * |
||
364 | * Call methods declared in [[ActiveQuery]] to further customize the relation. |
||
365 | * |
||
366 | * @param string $class the class name of the related record |
||
367 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
368 | * the attributes of the record associated with the `$class` model, while the values of the |
||
369 | * array refer to the corresponding attributes in **this** AR class. |
||
370 | * @return ActiveQueryInterface the relational query object. |
||
371 | */ |
||
372 | 43 | public function hasOne($class, $link) |
|
382 | |||
383 | /** |
||
384 | * Declares a `has-many` relation. |
||
385 | * The declaration is returned in terms of a relational [[ActiveQuery]] instance |
||
386 | * through which the related record can be queried and retrieved back. |
||
387 | * |
||
388 | * A `has-many` relation means that there are multiple related records matching |
||
389 | * the criteria set by this relation, e.g., a customer has many orders. |
||
390 | * |
||
391 | * For example, to declare the `orders` relation for `Customer` class, we can write |
||
392 | * the following code in the `Customer` class: |
||
393 | * |
||
394 | * ```php |
||
395 | * public function getOrders() |
||
396 | * { |
||
397 | * return $this->hasMany(Order::className(), ['customer_id' => 'id']); |
||
398 | * } |
||
399 | * ``` |
||
400 | * |
||
401 | * Note that in the above, the 'customer_id' key in the `$link` parameter refers to |
||
402 | * an attribute name in the related class `Order`, while the 'id' value refers to |
||
403 | * an attribute name in the current AR class. |
||
404 | * |
||
405 | * Call methods declared in [[ActiveQuery]] to further customize the relation. |
||
406 | * |
||
407 | * @param string $class the class name of the related record |
||
408 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
409 | * the attributes of the record associated with the `$class` model, while the values of the |
||
410 | * array refer to the corresponding attributes in **this** AR class. |
||
411 | * @return ActiveQueryInterface the relational query object. |
||
412 | */ |
||
413 | 129 | public function hasMany($class, $link) |
|
423 | |||
424 | /** |
||
425 | * Populates the named relation with the related records. |
||
426 | * Note that this method does not check if the relation exists or not. |
||
427 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
428 | * @param ActiveRecordInterface|array|null $records the related records to be populated into the relation. |
||
429 | * @see getRelation() |
||
430 | */ |
||
431 | 96 | public function populateRelation($name, $records) |
|
435 | |||
436 | /** |
||
437 | * Check whether the named relation has been populated with records. |
||
438 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
439 | * @return bool whether relation has been populated with records. |
||
440 | * @see getRelation() |
||
441 | */ |
||
442 | 45 | public function isRelationPopulated($name) |
|
446 | |||
447 | /** |
||
448 | * Returns all populated related records. |
||
449 | * @return array an array of related records indexed by relation names. |
||
450 | * @see getRelation() |
||
451 | */ |
||
452 | 6 | public function getRelatedRecords() |
|
456 | |||
457 | /** |
||
458 | * Returns a value indicating whether the model has an attribute with the specified name. |
||
459 | * @param string $name the name of the attribute |
||
460 | * @return bool whether the model has an attribute with the specified name. |
||
461 | */ |
||
462 | 219 | public function hasAttribute($name) |
|
466 | |||
467 | /** |
||
468 | * Returns the named attribute value. |
||
469 | * If this record is the result of a query and the attribute is not loaded, |
||
470 | * `null` will be returned. |
||
471 | * @param string $name the attribute name |
||
472 | * @return mixed the attribute value. `null` if the attribute is not set or does not exist. |
||
473 | * @see hasAttribute() |
||
474 | */ |
||
475 | public function getAttribute($name) |
||
479 | |||
480 | /** |
||
481 | * Sets the named attribute value. |
||
482 | * @param string $name the attribute name |
||
483 | * @param mixed $value the attribute value. |
||
484 | * @throws InvalidParamException if the named attribute does not exist. |
||
485 | * @see hasAttribute() |
||
486 | */ |
||
487 | 60 | public function setAttribute($name, $value) |
|
495 | |||
496 | /** |
||
497 | * Returns the old attribute values. |
||
498 | * @return array the old attribute values (name-value pairs) |
||
499 | */ |
||
500 | public function getOldAttributes() |
||
504 | |||
505 | /** |
||
506 | * Sets the old attribute values. |
||
507 | * All existing old attribute values will be discarded. |
||
508 | * @param array|null $values old attribute values to be set. |
||
509 | * If set to `null` this record is considered to be [[isNewRecord|new]]. |
||
510 | */ |
||
511 | 76 | public function setOldAttributes($values) |
|
515 | |||
516 | /** |
||
517 | * Returns the old value of the named attribute. |
||
518 | * If this record is the result of a query and the attribute is not loaded, |
||
519 | * `null` will be returned. |
||
520 | * @param string $name the attribute name |
||
521 | * @return mixed the old attribute value. `null` if the attribute is not loaded before |
||
522 | * or does not exist. |
||
523 | * @see hasAttribute() |
||
524 | */ |
||
525 | public function getOldAttribute($name) |
||
529 | |||
530 | /** |
||
531 | * Sets the old value of the named attribute. |
||
532 | * @param string $name the attribute name |
||
533 | * @param mixed $value the old attribute value. |
||
534 | * @throws InvalidParamException if the named attribute does not exist. |
||
535 | * @see hasAttribute() |
||
536 | */ |
||
537 | public function setOldAttribute($name, $value) |
||
545 | |||
546 | /** |
||
547 | * Marks an attribute dirty. |
||
548 | * This method may be called to force updating a record when calling [[update()]], |
||
549 | * even if there is no change being made to the record. |
||
550 | * @param string $name the attribute name |
||
551 | */ |
||
552 | public function markAttributeDirty($name) |
||
556 | |||
557 | /** |
||
558 | * Returns a value indicating whether the named attribute has been changed. |
||
559 | * @param string $name the name of the attribute. |
||
560 | * @param bool $identical whether the comparison of new and old value is made for |
||
561 | * identical values using `===`, defaults to `true`. Otherwise `==` is used for comparison. |
||
562 | * This parameter is available since version 2.0.4. |
||
563 | * @return bool whether the attribute has been changed |
||
564 | */ |
||
565 | 1 | public function isAttributeChanged($name, $identical = true) |
|
577 | |||
578 | /** |
||
579 | * Returns the attribute values that have been modified since they are loaded or saved most recently. |
||
580 | * |
||
581 | * The comparison of new and old values is made for identical values using `===`. |
||
582 | * |
||
583 | * @param string[]|null $names the names of the attributes whose values may be returned if they are |
||
584 | * changed recently. If null, [[attributes()]] will be used. |
||
585 | * @return array the changed attribute values (name-value pairs) |
||
586 | */ |
||
587 | 86 | public function getDirtyAttributes($names = null) |
|
609 | |||
610 | /** |
||
611 | * Saves the current record. |
||
612 | * |
||
613 | * This method will call [[insert()]] when [[isNewRecord]] is `true`, or [[update()]] |
||
614 | * when [[isNewRecord]] is `false`. |
||
615 | * |
||
616 | * For example, to save a customer record: |
||
617 | * |
||
618 | * ```php |
||
619 | * $customer = new Customer; // or $customer = Customer::findOne($id); |
||
620 | * $customer->name = $name; |
||
621 | * $customer->email = $email; |
||
622 | * $customer->save(); |
||
623 | * ``` |
||
624 | * |
||
625 | * @param bool $runValidation whether to perform validation (calling [[validate()]]) |
||
626 | * before saving the record. Defaults to `true`. If the validation fails, the record |
||
627 | * will not be saved to the database and this method will return `false`. |
||
628 | * @param array $attributeNames list of attribute names that need to be saved. Defaults to null, |
||
629 | * meaning all attributes that are loaded from DB will be saved. |
||
630 | * @return bool whether the saving succeeded (i.e. no validation errors occurred). |
||
631 | */ |
||
632 | 83 | public function save($runValidation = true, $attributeNames = null) |
|
640 | |||
641 | /** |
||
642 | * Saves the changes to this active record into the associated database table. |
||
643 | * |
||
644 | * This method performs the following steps in order: |
||
645 | * |
||
646 | * 1. call [[beforeValidate()]] when `$runValidation` is `true`. If [[beforeValidate()]] |
||
647 | * returns `false`, the rest of the steps will be skipped; |
||
648 | * 2. call [[afterValidate()]] when `$runValidation` is `true`. If validation |
||
649 | * failed, the rest of the steps will be skipped; |
||
650 | * 3. call [[beforeSave()]]. If [[beforeSave()]] returns `false`, |
||
651 | * the rest of the steps will be skipped; |
||
652 | * 4. save the record into database. If this fails, it will skip the rest of the steps; |
||
653 | * 5. call [[afterSave()]]; |
||
654 | * |
||
655 | * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]], |
||
656 | * [[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_UPDATE]], and [[EVENT_AFTER_UPDATE]] |
||
657 | * will be raised by the corresponding methods. |
||
658 | * |
||
659 | * Only the [[dirtyAttributes|changed attribute values]] will be saved into database. |
||
660 | * |
||
661 | * For example, to update a customer record: |
||
662 | * |
||
663 | * ```php |
||
664 | * $customer = Customer::findOne($id); |
||
665 | * $customer->name = $name; |
||
666 | * $customer->email = $email; |
||
667 | * $customer->update(); |
||
668 | * ``` |
||
669 | * |
||
670 | * Note that it is possible the update does not affect any row in the table. |
||
671 | * In this case, this method will return 0. For this reason, you should use the following |
||
672 | * code to check if update() is successful or not: |
||
673 | * |
||
674 | * ```php |
||
675 | * if ($customer->update() !== false) { |
||
676 | * // update successful |
||
677 | * } else { |
||
678 | * // update failed |
||
679 | * } |
||
680 | * ``` |
||
681 | * |
||
682 | * @param bool $runValidation whether to perform validation (calling [[validate()]]) |
||
683 | * before saving the record. Defaults to `true`. If the validation fails, the record |
||
684 | * will not be saved to the database and this method will return `false`. |
||
685 | * @param array $attributeNames list of attribute names that need to be saved. Defaults to null, |
||
686 | * meaning all attributes that are loaded from DB will be saved. |
||
687 | * @return int|false the number of rows affected, or `false` if validation fails |
||
688 | * or [[beforeSave()]] stops the updating process. |
||
689 | * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data |
||
690 | * being updated is outdated. |
||
691 | * @throws Exception in case update failed. |
||
692 | */ |
||
693 | public function update($runValidation = true, $attributeNames = null) |
||
700 | |||
701 | /** |
||
702 | * Updates the specified attributes. |
||
703 | * |
||
704 | * This method is a shortcut to [[update()]] when data validation is not needed |
||
705 | * and only a small set attributes need to be updated. |
||
706 | * |
||
707 | * You may specify the attributes to be updated as name list or name-value pairs. |
||
708 | * If the latter, the corresponding attribute values will be modified accordingly. |
||
709 | * The method will then save the specified attributes into database. |
||
710 | * |
||
711 | * Note that this method will **not** perform data validation and will **not** trigger events. |
||
712 | * |
||
713 | * @param array $attributes the attributes (names or name-value pairs) to be updated |
||
714 | * @return int the number of rows affected. |
||
715 | */ |
||
716 | 3 | public function updateAttributes($attributes) |
|
741 | |||
742 | /** |
||
743 | * @see update() |
||
744 | * @param array $attributes attributes to update |
||
745 | * @return int|false the number of rows affected, or false if [[beforeSave()]] stops the updating process. |
||
746 | * @throws StaleObjectException |
||
747 | */ |
||
748 | 25 | protected function updateInternal($attributes = null) |
|
785 | |||
786 | /** |
||
787 | * Updates one or several counter columns for the current AR object. |
||
788 | * Note that this method differs from [[updateAllCounters()]] in that it only |
||
789 | * saves counters for the current AR object. |
||
790 | * |
||
791 | * An example usage is as follows: |
||
792 | * |
||
793 | * ```php |
||
794 | * $post = Post::findOne($id); |
||
795 | * $post->updateCounters(['view_count' => 1]); |
||
796 | * ``` |
||
797 | * |
||
798 | * @param array $counters the counters to be updated (attribute name => increment value) |
||
799 | * Use negative values if you want to decrement the counters. |
||
800 | * @return bool whether the saving is successful |
||
801 | * @see updateAllCounters() |
||
802 | */ |
||
803 | 6 | public function updateCounters($counters) |
|
819 | |||
820 | /** |
||
821 | * Deletes the table row corresponding to this active record. |
||
822 | * |
||
823 | * This method performs the following steps in order: |
||
824 | * |
||
825 | * 1. call [[beforeDelete()]]. If the method returns `false`, it will skip the |
||
826 | * rest of the steps; |
||
827 | * 2. delete the record from the database; |
||
828 | * 3. call [[afterDelete()]]. |
||
829 | * |
||
830 | * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]] |
||
831 | * will be raised by the corresponding methods. |
||
832 | * |
||
833 | * @return int|false the number of rows deleted, or `false` if the deletion is unsuccessful for some reason. |
||
834 | * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful. |
||
835 | * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data |
||
836 | * being deleted is outdated. |
||
837 | * @throws Exception in case delete failed. |
||
838 | */ |
||
839 | public function delete() |
||
860 | |||
861 | /** |
||
862 | * Returns a value indicating whether the current record is new. |
||
863 | * @return bool whether the record is new and should be inserted when calling [[save()]]. |
||
864 | */ |
||
865 | 116 | public function getIsNewRecord() |
|
869 | |||
870 | /** |
||
871 | * Sets the value indicating whether the record is new. |
||
872 | * @param bool $value whether the record is new and should be inserted when calling [[save()]]. |
||
873 | * @see getIsNewRecord() |
||
874 | */ |
||
875 | public function setIsNewRecord($value) |
||
879 | |||
880 | /** |
||
881 | * Initializes the object. |
||
882 | * This method is called at the end of the constructor. |
||
883 | * The default implementation will trigger an [[EVENT_INIT]] event. |
||
884 | * If you override this method, make sure you call the parent implementation at the end |
||
885 | * to ensure triggering of the event. |
||
886 | */ |
||
887 | 305 | public function init() |
|
892 | |||
893 | /** |
||
894 | * This method is called when the AR object is created and populated with the query result. |
||
895 | * The default implementation will trigger an [[EVENT_AFTER_FIND]] event. |
||
896 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
897 | * event is triggered. |
||
898 | */ |
||
899 | 253 | public function afterFind() |
|
903 | |||
904 | /** |
||
905 | * This method is called at the beginning of inserting or updating a record. |
||
906 | * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is `true`, |
||
907 | * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is `false`. |
||
908 | * When overriding this method, make sure you call the parent implementation like the following: |
||
909 | * |
||
910 | * ```php |
||
911 | * public function beforeSave($insert) |
||
912 | * { |
||
913 | * if (parent::beforeSave($insert)) { |
||
914 | * // ...custom code here... |
||
915 | * return true; |
||
916 | * } else { |
||
917 | * return false; |
||
918 | * } |
||
919 | * } |
||
920 | * ``` |
||
921 | * |
||
922 | * @param bool $insert whether this method called while inserting a record. |
||
923 | * If `false`, it means the method is called while updating a record. |
||
924 | * @return bool whether the insertion or updating should continue. |
||
925 | * If `false`, the insertion or updating will be cancelled. |
||
926 | */ |
||
927 | 83 | public function beforeSave($insert) |
|
934 | |||
935 | /** |
||
936 | * This method is called at the end of inserting or updating a record. |
||
937 | * The default implementation will trigger an [[EVENT_AFTER_INSERT]] event when `$insert` is `true`, |
||
938 | * or an [[EVENT_AFTER_UPDATE]] event if `$insert` is `false`. The event class used is [[AfterSaveEvent]]. |
||
939 | * When overriding this method, make sure you call the parent implementation so that |
||
940 | * the event is triggered. |
||
941 | * @param bool $insert whether this method called while inserting a record. |
||
942 | * If `false`, it means the method is called while updating a record. |
||
943 | * @param array $changedAttributes The old values of attributes that had changed and were saved. |
||
944 | * You can use this parameter to take action based on the changes made for example send an email |
||
945 | * when the password had changed or implement audit trail that tracks all the changes. |
||
946 | * `$changedAttributes` gives you the old attribute values while the active record (`$this`) has |
||
947 | * already the new, updated values. |
||
948 | * |
||
949 | * Note that no automatic type conversion performed by default. You may use |
||
950 | * [[\yii\behaviors\AttributeTypecastBehavior]] to facilitate attribute typecasting. |
||
951 | * See http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#attributes-typecasting. |
||
952 | */ |
||
953 | 83 | public function afterSave($insert, $changedAttributes) |
|
954 | { |
||
955 | 83 | $this->trigger($insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE, new AfterSaveEvent([ |
|
956 | 83 | 'changedAttributes' => $changedAttributes, |
|
957 | 83 | ])); |
|
958 | 83 | } |
|
959 | |||
960 | /** |
||
961 | * This method is invoked before deleting a record. |
||
962 | * The default implementation raises the [[EVENT_BEFORE_DELETE]] event. |
||
963 | * When overriding this method, make sure you call the parent implementation like the following: |
||
964 | * |
||
965 | * ```php |
||
966 | * public function beforeDelete() |
||
967 | * { |
||
968 | * if (parent::beforeDelete()) { |
||
969 | * // ...custom code here... |
||
970 | * return true; |
||
971 | * } else { |
||
972 | * return false; |
||
973 | * } |
||
974 | * } |
||
975 | * ``` |
||
976 | * |
||
977 | * @return bool whether the record should be deleted. Defaults to `true`. |
||
978 | */ |
||
979 | 6 | public function beforeDelete() |
|
980 | { |
||
981 | 6 | $event = new ModelEvent; |
|
982 | 6 | $this->trigger(self::EVENT_BEFORE_DELETE, $event); |
|
983 | |||
984 | 6 | return $event->isValid; |
|
985 | } |
||
986 | |||
987 | /** |
||
988 | * This method is invoked after deleting a record. |
||
989 | * The default implementation raises the [[EVENT_AFTER_DELETE]] event. |
||
990 | * You may override this method to do postprocessing after the record is deleted. |
||
991 | * Make sure you call the parent implementation so that the event is raised properly. |
||
992 | */ |
||
993 | 6 | public function afterDelete() |
|
994 | { |
||
995 | 6 | $this->trigger(self::EVENT_AFTER_DELETE); |
|
996 | 6 | } |
|
997 | |||
998 | /** |
||
999 | * Repopulates this active record with the latest data. |
||
1000 | * |
||
1001 | * If the refresh is successful, an [[EVENT_AFTER_REFRESH]] event will be triggered. |
||
1002 | * This event is available since version 2.0.8. |
||
1003 | * |
||
1004 | * @return bool whether the row still exists in the database. If `true`, the latest data |
||
1005 | * will be populated to this active record. Otherwise, this record will remain unchanged. |
||
1006 | */ |
||
1007 | 19 | public function refresh() |
|
1008 | { |
||
1009 | /* @var $record BaseActiveRecord */ |
||
1010 | 19 | $record = static::findOne($this->getPrimaryKey(true)); |
|
1011 | 19 | if ($record === null) { |
|
1012 | 3 | return false; |
|
1013 | } |
||
1014 | 19 | foreach ($this->attributes() as $name) { |
|
1015 | 19 | $this->_attributes[$name] = isset($record->_attributes[$name]) ? $record->_attributes[$name] : null; |
|
1016 | 19 | } |
|
1017 | 19 | $this->_oldAttributes = $record->_oldAttributes; |
|
1018 | 19 | $this->_related = []; |
|
1019 | 19 | $this->afterRefresh(); |
|
1020 | |||
1021 | 19 | return true; |
|
1022 | } |
||
1023 | |||
1024 | /** |
||
1025 | * This method is called when the AR object is refreshed. |
||
1026 | * The default implementation will trigger an [[EVENT_AFTER_REFRESH]] event. |
||
1027 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
1028 | * event is triggered. |
||
1029 | * @since 2.0.8 |
||
1030 | */ |
||
1031 | 19 | public function afterRefresh() |
|
1032 | { |
||
1033 | 19 | $this->trigger(self::EVENT_AFTER_REFRESH); |
|
1034 | 19 | } |
|
1035 | |||
1036 | /** |
||
1037 | * Returns a value indicating whether the given active record is the same as the current one. |
||
1038 | * The comparison is made by comparing the table names and the primary key values of the two active records. |
||
1039 | * If one of the records [[isNewRecord|is new]] they are also considered not equal. |
||
1040 | * @param ActiveRecordInterface $record record to compare to |
||
1041 | * @return bool whether the two active records refer to the same row in the same database table. |
||
1042 | */ |
||
1043 | public function equals($record) |
||
1044 | { |
||
1045 | if ($this->getIsNewRecord() || $record->getIsNewRecord()) { |
||
1046 | return false; |
||
1047 | } |
||
1048 | |||
1049 | return get_class($this) === get_class($record) && $this->getPrimaryKey() === $record->getPrimaryKey(); |
||
1050 | } |
||
1051 | |||
1052 | /** |
||
1053 | * Returns the primary key value(s). |
||
1054 | * @param bool $asArray whether to return the primary key value as an array. If `true`, |
||
1055 | * the return value will be an array with column names as keys and column values as values. |
||
1056 | * Note that for composite primary keys, an array will always be returned regardless of this parameter value. |
||
1057 | * @property mixed The primary key value. An array (column name => column value) is returned if |
||
1058 | * the primary key is composite. A string is returned otherwise (null will be returned if |
||
1059 | * the key value is null). |
||
1060 | * @return mixed the primary key value. An array (column name => column value) is returned if the primary key |
||
1061 | * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if |
||
1062 | * the key value is null). |
||
1063 | */ |
||
1064 | 48 | public function getPrimaryKey($asArray = false) |
|
1065 | { |
||
1066 | 48 | $keys = $this->primaryKey(); |
|
1067 | 48 | if (!$asArray && count($keys) === 1) { |
|
1068 | 26 | return isset($this->_attributes[$keys[0]]) ? $this->_attributes[$keys[0]] : null; |
|
1069 | } else { |
||
1070 | 25 | $values = []; |
|
1071 | 25 | foreach ($keys as $name) { |
|
1072 | 25 | $values[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : null; |
|
1073 | 25 | } |
|
1074 | |||
1075 | 25 | return $values; |
|
1076 | } |
||
1077 | } |
||
1078 | |||
1079 | /** |
||
1080 | * Returns the old primary key value(s). |
||
1081 | * This refers to the primary key value that is populated into the record |
||
1082 | * after executing a find method (e.g. find(), findOne()). |
||
1083 | * The value remains unchanged even if the primary key attribute is manually assigned with a different value. |
||
1084 | * @param bool $asArray whether to return the primary key value as an array. If `true`, |
||
1085 | * the return value will be an array with column name as key and column value as value. |
||
1086 | * If this is `false` (default), a scalar value will be returned for non-composite primary key. |
||
1087 | * @property mixed The old primary key value. An array (column name => column value) is |
||
1088 | * returned if the primary key is composite. A string is returned otherwise (null will be |
||
1089 | * returned if the key value is null). |
||
1090 | * @return mixed the old primary key value. An array (column name => column value) is returned if the primary key |
||
1091 | * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if |
||
1092 | * the key value is null). |
||
1093 | * @throws Exception if the AR model does not have a primary key |
||
1094 | */ |
||
1095 | 47 | public function getOldPrimaryKey($asArray = false) |
|
1096 | { |
||
1097 | 47 | $keys = $this->primaryKey(); |
|
1098 | 47 | if (empty($keys)) { |
|
1099 | 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.'); |
||
1100 | } |
||
1101 | 47 | if (!$asArray && count($keys) === 1) { |
|
1102 | 7 | return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null; |
|
1103 | } else { |
||
1104 | 41 | $values = []; |
|
1105 | 41 | foreach ($keys as $name) { |
|
1106 | 41 | $values[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null; |
|
1107 | 41 | } |
|
1108 | |||
1109 | 41 | return $values; |
|
1110 | } |
||
1111 | } |
||
1112 | |||
1113 | /** |
||
1114 | * Populates an active record object using a row of data from the database/storage. |
||
1115 | * |
||
1116 | * This is an internal method meant to be called to create active record objects after |
||
1117 | * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate |
||
1118 | * the query results into active records. |
||
1119 | * |
||
1120 | * When calling this method manually you should call [[afterFind()]] on the created |
||
1121 | * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]]. |
||
1122 | * |
||
1123 | * @param BaseActiveRecord $record the record to be populated. In most cases this will be an instance |
||
1124 | * created by [[instantiate()]] beforehand. |
||
1125 | * @param array $row attribute values (name => value) |
||
1126 | */ |
||
1127 | 253 | public static function populateRecord($record, $row) |
|
1128 | { |
||
1129 | 253 | $columns = array_flip($record->attributes()); |
|
1130 | 253 | foreach ($row as $name => $value) { |
|
1131 | 253 | if (isset($columns[$name])) { |
|
1132 | 253 | $record->_attributes[$name] = $value; |
|
1133 | 253 | } elseif ($record->canSetProperty($name)) { |
|
1134 | 6 | $record->$name = $value; |
|
1135 | 6 | } |
|
1136 | 253 | } |
|
1137 | 253 | $record->_oldAttributes = $record->_attributes; |
|
1138 | 253 | } |
|
1139 | |||
1140 | /** |
||
1141 | * Creates an active record instance. |
||
1142 | * |
||
1143 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
1144 | * It is not meant to be used for creating new records directly. |
||
1145 | * |
||
1146 | * You may override this method if the instance being created |
||
1147 | * depends on the row data to be populated into the record. |
||
1148 | * For example, by creating a record based on the value of a column, |
||
1149 | * you may implement the so-called single-table inheritance mapping. |
||
1150 | * @param array $row row data to be populated into the record. |
||
1151 | * @return static the newly created active record |
||
1152 | */ |
||
1153 | 250 | public static function instantiate($row) |
|
1154 | { |
||
1155 | 250 | return new static; |
|
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Returns whether there is an element at the specified offset. |
||
1160 | * This method is required by the interface [[\ArrayAccess]]. |
||
1161 | * @param mixed $offset the offset to check on |
||
1162 | * @return bool whether there is an element at the specified offset. |
||
1163 | */ |
||
1164 | 24 | public function offsetExists($offset) |
|
1165 | { |
||
1166 | 24 | return $this->__isset($offset); |
|
1167 | } |
||
1168 | |||
1169 | /** |
||
1170 | * Returns the relation object with the specified name. |
||
1171 | * A relation is defined by a getter method which returns an [[ActiveQueryInterface]] object. |
||
1172 | * It can be declared in either the Active Record class itself or one of its behaviors. |
||
1173 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
1174 | * @param bool $throwException whether to throw exception if the relation does not exist. |
||
1175 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
1176 | * and `$throwException` is `false`, `null` will be returned. |
||
1177 | * @throws InvalidParamException if the named relation does not exist. |
||
1178 | */ |
||
1179 | 120 | public function getRelation($name, $throwException = true) |
|
1180 | { |
||
1181 | 120 | $getter = 'get' . $name; |
|
1182 | try { |
||
1183 | // the relation could be defined in a behavior |
||
1184 | 120 | $relation = $this->$getter(); |
|
1185 | 120 | } catch (UnknownMethodException $e) { |
|
1186 | if ($throwException) { |
||
1187 | throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e); |
||
1188 | } else { |
||
1189 | return null; |
||
1190 | } |
||
1191 | } |
||
1192 | 120 | if (!$relation instanceof ActiveQueryInterface) { |
|
1193 | if ($throwException) { |
||
1194 | throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".'); |
||
1195 | } else { |
||
1196 | return null; |
||
1197 | } |
||
1198 | } |
||
1199 | |||
1200 | 120 | if (method_exists($this, $getter)) { |
|
1201 | // relation name is case sensitive, trying to validate it when the relation is defined within this class |
||
1202 | 120 | $method = new \ReflectionMethod($this, $getter); |
|
1203 | 120 | $realName = lcfirst(substr($method->getName(), 3)); |
|
1204 | 120 | if ($realName !== $name) { |
|
1205 | if ($throwException) { |
||
1206 | throw new InvalidParamException('Relation names are case sensitive. ' . get_class($this) . " has a relation named \"$realName\" instead of \"$name\"."); |
||
1207 | } else { |
||
1208 | return null; |
||
1209 | } |
||
1210 | } |
||
1211 | 120 | } |
|
1212 | |||
1213 | 120 | return $relation; |
|
1214 | } |
||
1215 | |||
1216 | /** |
||
1217 | * Establishes the relationship between two models. |
||
1218 | * |
||
1219 | * The relationship is established by setting the foreign key value(s) in one model |
||
1220 | * to be the corresponding primary key value(s) in the other model. |
||
1221 | * The model with the foreign key will be saved into database without performing validation. |
||
1222 | * |
||
1223 | * If the relationship involves a junction table, a new row will be inserted into the |
||
1224 | * junction table which contains the primary key values from both models. |
||
1225 | * |
||
1226 | * Note that this method requires that the primary key value is not null. |
||
1227 | * |
||
1228 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
1229 | * @param ActiveRecordInterface $model the model to be linked with the current one. |
||
1230 | * @param array $extraColumns additional column values to be saved into the junction table. |
||
1231 | * This parameter is only meaningful for a relationship involving a junction table |
||
1232 | * (i.e., a relation set with [[ActiveRelationTrait::via()]] or [[ActiveQuery::viaTable()]].) |
||
1233 | * @throws InvalidCallException if the method is unable to link two models. |
||
1234 | */ |
||
1235 | 9 | public function link($name, $model, $extraColumns = []) |
|
1236 | { |
||
1237 | 9 | $relation = $this->getRelation($name); |
|
1238 | |||
1239 | 9 | if ($relation->via !== null) { |
|
1240 | 3 | if ($this->getIsNewRecord() || $model->getIsNewRecord()) { |
|
1241 | throw new InvalidCallException('Unable to link models: the models being linked cannot be newly created.'); |
||
1242 | } |
||
1243 | 3 | if (is_array($relation->via)) { |
|
1244 | /* @var $viaRelation ActiveQuery */ |
||
1245 | 3 | list($viaName, $viaRelation) = $relation->via; |
|
1246 | 3 | $viaClass = $viaRelation->modelClass; |
|
1247 | // unset $viaName so that it can be reloaded to reflect the change |
||
1248 | 3 | unset($this->_related[$viaName]); |
|
1249 | 3 | } else { |
|
1250 | $viaRelation = $relation->via; |
||
1251 | $viaTable = reset($relation->via->from); |
||
1252 | } |
||
1253 | 3 | $columns = []; |
|
1254 | 3 | foreach ($viaRelation->link as $a => $b) { |
|
1255 | 3 | $columns[$a] = $this->$b; |
|
1256 | 3 | } |
|
1257 | 3 | foreach ($relation->link as $a => $b) { |
|
1258 | 3 | $columns[$b] = $model->$a; |
|
1259 | 3 | } |
|
1260 | 3 | foreach ($extraColumns as $k => $v) { |
|
1261 | 3 | $columns[$k] = $v; |
|
1262 | 3 | } |
|
1263 | 3 | if (is_array($relation->via)) { |
|
1264 | /* @var $viaClass ActiveRecordInterface */ |
||
1265 | /* @var $record ActiveRecordInterface */ |
||
1266 | 3 | $record = new $viaClass(); |
|
1267 | 3 | foreach ($columns as $column => $value) { |
|
1268 | 3 | $record->$column = $value; |
|
1269 | 3 | } |
|
1270 | 3 | $record->insert(false); |
|
1271 | 3 | } else { |
|
1272 | /* @var $viaTable string */ |
||
1273 | static::getDb()->createCommand() |
||
1274 | ->insert($viaTable, $columns)->execute(); |
||
1275 | } |
||
1276 | 3 | } else { |
|
1277 | 9 | $p1 = $model->isPrimaryKey(array_keys($relation->link)); |
|
1278 | 9 | $p2 = static::isPrimaryKey(array_values($relation->link)); |
|
1279 | 9 | if ($p1 && $p2) { |
|
1280 | if ($this->getIsNewRecord() && $model->getIsNewRecord()) { |
||
1281 | throw new InvalidCallException('Unable to link models: at most one model can be newly created.'); |
||
1282 | } elseif ($this->getIsNewRecord()) { |
||
1283 | $this->bindModels(array_flip($relation->link), $this, $model); |
||
1284 | } else { |
||
1285 | $this->bindModels($relation->link, $model, $this); |
||
1286 | } |
||
1287 | 9 | } elseif ($p1) { |
|
1288 | 3 | $this->bindModels(array_flip($relation->link), $this, $model); |
|
1289 | 9 | } elseif ($p2) { |
|
1290 | 9 | $this->bindModels($relation->link, $model, $this); |
|
1291 | 9 | } else { |
|
1292 | throw new InvalidCallException('Unable to link models: the link defining the relation does not involve any primary key.'); |
||
1293 | } |
||
1294 | } |
||
1295 | |||
1296 | // update lazily loaded related objects |
||
1297 | 9 | if (!$relation->multiple) { |
|
1298 | 3 | $this->_related[$name] = $model; |
|
1299 | 9 | } elseif (isset($this->_related[$name])) { |
|
1300 | 9 | if ($relation->indexBy !== null) { |
|
1301 | 6 | if ($relation->indexBy instanceof \Closure) { |
|
1302 | 3 | $index = call_user_func($relation->indexBy, $model); |
|
1303 | 3 | } else { |
|
1304 | 3 | $index = $model->{$relation->indexBy}; |
|
1305 | } |
||
1306 | 6 | $this->_related[$name][$index] = $model; |
|
1307 | 6 | } else { |
|
1308 | 3 | $this->_related[$name][] = $model; |
|
1309 | } |
||
1310 | 9 | } |
|
1311 | 9 | } |
|
1312 | |||
1313 | /** |
||
1314 | * Destroys the relationship between two models. |
||
1315 | * |
||
1316 | * The model with the foreign key of the relationship will be deleted if `$delete` is `true`. |
||
1317 | * Otherwise, the foreign key will be set `null` and the model will be saved without validation. |
||
1318 | * |
||
1319 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
1320 | * @param ActiveRecordInterface $model the model to be unlinked from the current one. |
||
1321 | * You have to make sure that the model is really related with the current model as this method |
||
1322 | * does not check this. |
||
1323 | * @param bool $delete whether to delete the model that contains the foreign key. |
||
1324 | * If `false`, the model's foreign key will be set `null` and saved. |
||
1325 | * If `true`, the model containing the foreign key will be deleted. |
||
1326 | * @throws InvalidCallException if the models cannot be unlinked |
||
1327 | */ |
||
1328 | 3 | public function unlink($name, $model, $delete = false) |
|
1411 | |||
1412 | /** |
||
1413 | * Destroys the relationship in current model. |
||
1414 | * |
||
1415 | * The model with the foreign key of the relationship will be deleted if `$delete` is `true`. |
||
1416 | * Otherwise, the foreign key will be set `null` and the model will be saved without validation. |
||
1417 | * |
||
1418 | * Note that to destroy the relationship without removing records make sure your keys can be set to null |
||
1419 | * |
||
1420 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
1421 | * @param bool $delete whether to delete the model that contains the foreign key. |
||
1422 | */ |
||
1423 | 18 | public function unlinkAll($name, $delete = false) |
|
1424 | { |
||
1496 | |||
1497 | /** |
||
1498 | * @param array $link |
||
1499 | * @param ActiveRecordInterface $foreignModel |
||
1500 | * @param ActiveRecordInterface $primaryModel |
||
1501 | * @throws InvalidCallException |
||
1502 | */ |
||
1503 | 9 | private function bindModels($link, $foreignModel, $primaryModel) |
|
1518 | |||
1519 | /** |
||
1520 | * Returns a value indicating whether the given set of attributes represents the primary key for this model |
||
1521 | * @param array $keys the set of attributes to check |
||
1522 | * @return bool whether the given set of attributes represents the primary key for this model |
||
1523 | */ |
||
1524 | 15 | public static function isPrimaryKey($keys) |
|
1533 | |||
1534 | /** |
||
1535 | * Returns the text label for the specified attribute. |
||
1536 | * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. |
||
1537 | * @param string $attribute the attribute name |
||
1538 | * @return string the attribute label |
||
1539 | * @see generateAttributeLabel() |
||
1540 | * @see attributeLabels() |
||
1541 | */ |
||
1542 | 51 | public function getAttributeLabel($attribute) |
|
1573 | |||
1574 | /** |
||
1575 | * Returns the text hint for the specified attribute. |
||
1576 | * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. |
||
1577 | * @param string $attribute the attribute name |
||
1578 | * @return string the attribute hint |
||
1579 | * @see attributeHints() |
||
1580 | * @since 2.0.4 |
||
1581 | */ |
||
1582 | public function getAttributeHint($attribute) |
||
1612 | |||
1613 | /** |
||
1614 | * @inheritdoc |
||
1615 | * |
||
1616 | * The default implementation returns the names of the columns whose values have been populated into this record. |
||
1617 | */ |
||
1618 | public function fields() |
||
1624 | |||
1625 | /** |
||
1626 | * @inheritdoc |
||
1627 | * |
||
1628 | * The default implementation returns the names of the relations that have been populated into this record. |
||
1629 | */ |
||
1630 | public function extraFields() |
||
1636 | |||
1637 | /** |
||
1638 | * Sets the element value at the specified offset to null. |
||
1639 | * This method is required by the SPL interface [[\ArrayAccess]]. |
||
1640 | * It is implicitly called when you use something like `unset($model[$offset])`. |
||
1641 | * @param mixed $offset the offset to unset element |
||
1642 | */ |
||
1643 | 3 | public function offsetUnset($offset) |
|
1651 | } |
||
1652 |