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 | 172 | 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) |
||
128 | { |
||
129 | $query = static::find(); |
||
130 | |||
131 | if (!ArrayHelper::isAssociative($condition)) { |
||
132 | // query by primary key |
||
133 | $primaryKey = static::primaryKey(); |
||
134 | if (isset($primaryKey[0])) { |
||
135 | $condition = [$primaryKey[0] => $condition]; |
||
136 | } else { |
||
137 | throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.'); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return $query->andWhere($condition); |
||
142 | } |
||
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 | * @return int the number of rows deleted |
||
196 | * @throws NotSupportedException if not overridden. |
||
197 | */ |
||
198 | public static function deleteAll($condition = null) |
||
199 | { |
||
200 | throw new NotSupportedException(__METHOD__ . ' is not supported.'); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Returns the name of the column that stores the lock version for implementing optimistic locking. |
||
205 | * |
||
206 | * Optimistic locking allows multiple users to access the same record for edits and avoids |
||
207 | * potential conflicts. In case when a user attempts to save the record upon some staled data |
||
208 | * (because another user has modified the data), a [[StaleObjectException]] exception will be thrown, |
||
209 | * and the update or deletion is skipped. |
||
210 | * |
||
211 | * Optimistic locking is only supported by [[update()]] and [[delete()]]. |
||
212 | * |
||
213 | * To use Optimistic locking: |
||
214 | * |
||
215 | * 1. Create a column to store the version number of each row. The column type should be `BIGINT DEFAULT 0`. |
||
216 | * Override this method to return the name of this column. |
||
217 | * 2. Add a `required` validation rule for the version column to ensure the version value is submitted. |
||
218 | * 3. In the Web form that collects the user input, add a hidden field that stores |
||
219 | * the lock version of the recording being updated. |
||
220 | * 4. In the controller action that does the data updating, try to catch the [[StaleObjectException]] |
||
221 | * and implement necessary business logic (e.g. merging the changes, prompting stated data) |
||
222 | * to resolve the conflict. |
||
223 | * |
||
224 | * @return string the column name that stores the lock version of a table row. |
||
225 | * If `null` is returned (default implemented), optimistic locking will not be supported. |
||
226 | */ |
||
227 | 28 | public function optimisticLock() |
|
228 | { |
||
229 | 28 | return null; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 3 | public function canGetProperty($name, $checkVars = true, $checkBehaviors = true) |
|
236 | { |
||
237 | 3 | if (parent::canGetProperty($name, $checkVars, $checkBehaviors)) { |
|
238 | 3 | return true; |
|
239 | } |
||
240 | |||
241 | try { |
||
242 | 3 | return $this->hasAttribute($name); |
|
243 | } catch (\Exception $e) { |
||
244 | // `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used |
||
245 | return false; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @inheritdoc |
||
251 | */ |
||
252 | 9 | public function canSetProperty($name, $checkVars = true, $checkBehaviors = true) |
|
253 | { |
||
254 | 9 | if (parent::canSetProperty($name, $checkVars, $checkBehaviors)) { |
|
255 | 6 | return true; |
|
256 | } |
||
257 | |||
258 | try { |
||
259 | 3 | return $this->hasAttribute($name); |
|
260 | } catch (\Exception $e) { |
||
261 | // `hasAttribute()` may fail on base/abstract classes in case automatic attribute list fetching used |
||
262 | return false; |
||
263 | } |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * PHP getter magic method. |
||
268 | * This method is overridden so that attributes and related objects can be accessed like properties. |
||
269 | * |
||
270 | * @param string $name property name |
||
271 | * @throws \yii\base\InvalidParamException if relation name is wrong |
||
272 | * @return mixed property value |
||
273 | * @see getAttribute() |
||
274 | */ |
||
275 | 310 | public function __get($name) |
|
276 | { |
||
277 | 310 | if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) { |
|
278 | 298 | return $this->_attributes[$name]; |
|
279 | 158 | } elseif ($this->hasAttribute($name)) { |
|
280 | 34 | return null; |
|
281 | } |
||
282 | |||
283 | 136 | if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) { |
|
284 | 78 | return $this->_related[$name]; |
|
285 | } |
||
286 | 91 | $value = parent::__get($name); |
|
287 | 91 | if ($value instanceof ActiveQueryInterface) { |
|
288 | 52 | return $this->_related[$name] = $value->findFor($name, $this); |
|
289 | } |
||
290 | |||
291 | 45 | return $value; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * PHP setter magic method. |
||
296 | * This method is overridden so that AR attributes can be accessed like properties. |
||
297 | * @param string $name property name |
||
298 | * @param mixed $value property value |
||
299 | */ |
||
300 | 152 | public function __set($name, $value) |
|
301 | { |
||
302 | 152 | if ($this->hasAttribute($name)) { |
|
303 | 152 | $this->_attributes[$name] = $value; |
|
304 | } else { |
||
305 | 4 | parent::__set($name, $value); |
|
306 | } |
||
307 | 152 | } |
|
308 | |||
309 | /** |
||
310 | * Checks if a property value is null. |
||
311 | * This method overrides the parent implementation by checking if the named attribute is `null` or not. |
||
312 | * @param string $name the property name or the event name |
||
313 | * @return bool whether the property value is null |
||
314 | */ |
||
315 | 50 | public function __isset($name) |
|
316 | { |
||
317 | try { |
||
318 | 50 | return $this->__get($name) !== null; |
|
319 | } catch (\Exception $e) { |
||
320 | return false; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Sets a component property to be null. |
||
326 | * This method overrides the parent implementation by clearing |
||
327 | * the specified attribute value. |
||
328 | * @param string $name the property name or the event name |
||
329 | */ |
||
330 | 9 | public function __unset($name) |
|
331 | { |
||
332 | 9 | if ($this->hasAttribute($name)) { |
|
333 | 3 | unset($this->_attributes[$name]); |
|
334 | 6 | } elseif (array_key_exists($name, $this->_related)) { |
|
335 | 6 | unset($this->_related[$name]); |
|
336 | } elseif ($this->getRelation($name, false) === null) { |
||
337 | parent::__unset($name); |
||
338 | } |
||
339 | 9 | } |
|
340 | |||
341 | /** |
||
342 | * Declares a `has-one` relation. |
||
343 | * The declaration is returned in terms of a relational [[ActiveQuery]] instance |
||
344 | * through which the related record can be queried and retrieved back. |
||
345 | * |
||
346 | * A `has-one` relation means that there is at most one related record matching |
||
347 | * the criteria set by this relation, e.g., a customer has one country. |
||
348 | * |
||
349 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
350 | * the following code in the `Customer` class: |
||
351 | * |
||
352 | * ```php |
||
353 | * public function getCountry() |
||
354 | * { |
||
355 | * return $this->hasOne(Country::className(), ['id' => 'country_id']); |
||
356 | * } |
||
357 | * ``` |
||
358 | * |
||
359 | * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name |
||
360 | * in the related class `Country`, while the 'country_id' value refers to an attribute name |
||
361 | * in the current AR class. |
||
362 | * |
||
363 | * Call methods declared in [[ActiveQuery]] to further customize the relation. |
||
364 | * |
||
365 | * @param string $class the class name of the related record |
||
366 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
367 | * the attributes of the record associated with the `$class` model, while the values of the |
||
368 | * array refer to the corresponding attributes in **this** AR class. |
||
369 | * @return ActiveQueryInterface the relational query object. |
||
370 | */ |
||
371 | 46 | public function hasOne($class, $link) |
|
372 | { |
||
373 | 46 | return $this->createRelationQuery($class, $link, false); |
|
374 | } |
||
375 | |||
376 | /** |
||
377 | * Declares a `has-many` relation. |
||
378 | * The declaration is returned in terms of a relational [[ActiveQuery]] instance |
||
379 | * through which the related record can be queried and retrieved back. |
||
380 | * |
||
381 | * A `has-many` relation means that there are multiple related records matching |
||
382 | * the criteria set by this relation, e.g., a customer has many orders. |
||
383 | * |
||
384 | * For example, to declare the `orders` relation for `Customer` class, we can write |
||
385 | * the following code in the `Customer` class: |
||
386 | * |
||
387 | * ```php |
||
388 | * public function getOrders() |
||
389 | * { |
||
390 | * return $this->hasMany(Order::className(), ['customer_id' => 'id']); |
||
391 | * } |
||
392 | * ``` |
||
393 | * |
||
394 | * Note that in the above, the 'customer_id' key in the `$link` parameter refers to |
||
395 | * an attribute name in the related class `Order`, while the 'id' value refers to |
||
396 | * an attribute name in the current AR class. |
||
397 | * |
||
398 | * Call methods declared in [[ActiveQuery]] to further customize the relation. |
||
399 | * |
||
400 | * @param string $class the class name of the related record |
||
401 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
402 | * the attributes of the record associated with the `$class` model, while the values of the |
||
403 | * array refer to the corresponding attributes in **this** AR class. |
||
404 | * @return ActiveQueryInterface the relational query object. |
||
405 | */ |
||
406 | 132 | public function hasMany($class, $link) |
|
407 | { |
||
408 | 132 | return $this->createRelationQuery($class, $link, true); |
|
409 | } |
||
410 | |||
411 | /** |
||
412 | * Creates a query instance for `has-one` or `has-many` relation. |
||
413 | * @param string $class the class name of the related record. |
||
414 | * @param array $link the primary-foreign key constraint. |
||
415 | * @param bool $multiple whether this query represents a relation to more than one record. |
||
416 | * @return ActiveQueryInterface the relational query object. |
||
417 | * @since 2.0.12 |
||
418 | * @see hasOne() |
||
419 | * @see hasMany() |
||
420 | */ |
||
421 | 139 | protected function createRelationQuery($class, $link, $multiple) |
|
422 | { |
||
423 | /* @var $class ActiveRecordInterface */ |
||
424 | /* @var $query ActiveQuery */ |
||
425 | 139 | $query = $class::find(); |
|
426 | 139 | $query->primaryModel = $this; |
|
427 | 139 | $query->link = $link; |
|
428 | 139 | $query->multiple = $multiple; |
|
429 | 139 | return $query; |
|
430 | } |
||
431 | |||
432 | /** |
||
433 | * Populates the named relation with the related records. |
||
434 | * Note that this method does not check if the relation exists or not. |
||
435 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
436 | * @param ActiveRecordInterface|array|null $records the related records to be populated into the relation. |
||
437 | * @see getRelation() |
||
438 | */ |
||
439 | 99 | public function populateRelation($name, $records) |
|
443 | |||
444 | /** |
||
445 | * Check whether the named relation has been populated with records. |
||
446 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
447 | * @return bool whether relation has been populated with records. |
||
448 | * @see getRelation() |
||
449 | */ |
||
450 | 45 | public function isRelationPopulated($name) |
|
454 | |||
455 | /** |
||
456 | * Returns all populated related records. |
||
457 | * @return array an array of related records indexed by relation names. |
||
458 | * @see getRelation() |
||
459 | */ |
||
460 | 6 | public function getRelatedRecords() |
|
464 | |||
465 | /** |
||
466 | * Returns a value indicating whether the model has an attribute with the specified name. |
||
467 | * @param string $name the name of the attribute |
||
468 | * @return bool whether the model has an attribute with the specified name. |
||
469 | */ |
||
470 | 249 | public function hasAttribute($name) |
|
474 | |||
475 | /** |
||
476 | * Returns the named attribute value. |
||
477 | * If this record is the result of a query and the attribute is not loaded, |
||
478 | * `null` will be returned. |
||
479 | * @param string $name the attribute name |
||
480 | * @return mixed the attribute value. `null` if the attribute is not set or does not exist. |
||
481 | * @see hasAttribute() |
||
482 | */ |
||
483 | public function getAttribute($name) |
||
487 | |||
488 | /** |
||
489 | * Sets the named attribute value. |
||
490 | * @param string $name the attribute name |
||
491 | * @param mixed $value the attribute value. |
||
492 | * @throws InvalidParamException if the named attribute does not exist. |
||
493 | * @see hasAttribute() |
||
494 | */ |
||
495 | 71 | public function setAttribute($name, $value) |
|
496 | { |
||
497 | 71 | if ($this->hasAttribute($name)) { |
|
498 | 71 | $this->_attributes[$name] = $value; |
|
499 | } else { |
||
500 | throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".'); |
||
501 | } |
||
502 | 71 | } |
|
503 | |||
504 | /** |
||
505 | * Returns the old attribute values. |
||
506 | * @return array the old attribute values (name-value pairs) |
||
507 | */ |
||
508 | public function getOldAttributes() |
||
512 | |||
513 | /** |
||
514 | * Sets the old attribute values. |
||
515 | * All existing old attribute values will be discarded. |
||
516 | * @param array|null $values old attribute values to be set. |
||
517 | * If set to `null` this record is considered to be [[isNewRecord|new]]. |
||
518 | */ |
||
519 | 88 | public function setOldAttributes($values) |
|
523 | |||
524 | /** |
||
525 | * Returns the old value of the named attribute. |
||
526 | * If this record is the result of a query and the attribute is not loaded, |
||
527 | * `null` will be returned. |
||
528 | * @param string $name the attribute name |
||
529 | * @return mixed the old attribute value. `null` if the attribute is not loaded before |
||
530 | * or does not exist. |
||
531 | * @see hasAttribute() |
||
532 | */ |
||
533 | public function getOldAttribute($name) |
||
537 | |||
538 | /** |
||
539 | * Sets the old value of the named attribute. |
||
540 | * @param string $name the attribute name |
||
541 | * @param mixed $value the old attribute value. |
||
542 | * @throws InvalidParamException if the named attribute does not exist. |
||
543 | * @see hasAttribute() |
||
544 | */ |
||
545 | public function setOldAttribute($name, $value) |
||
546 | { |
||
547 | if (isset($this->_oldAttributes[$name]) || $this->hasAttribute($name)) { |
||
548 | $this->_oldAttributes[$name] = $value; |
||
549 | } else { |
||
550 | throw new InvalidParamException(get_class($this) . ' has no attribute named "' . $name . '".'); |
||
551 | } |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Marks an attribute dirty. |
||
556 | * This method may be called to force updating a record when calling [[update()]], |
||
557 | * even if there is no change being made to the record. |
||
558 | * @param string $name the attribute name |
||
559 | */ |
||
560 | public function markAttributeDirty($name) |
||
564 | |||
565 | /** |
||
566 | * Returns a value indicating whether the named attribute has been changed. |
||
567 | * @param string $name the name of the attribute. |
||
568 | * @param bool $identical whether the comparison of new and old value is made for |
||
569 | * identical values using `===`, defaults to `true`. Otherwise `==` is used for comparison. |
||
570 | * This parameter is available since version 2.0.4. |
||
571 | * @return bool whether the attribute has been changed |
||
572 | */ |
||
573 | 2 | public function isAttributeChanged($name, $identical = true) |
|
574 | { |
||
575 | 2 | if (isset($this->_attributes[$name], $this->_oldAttributes[$name])) { |
|
576 | 1 | if ($identical) { |
|
577 | 1 | return $this->_attributes[$name] !== $this->_oldAttributes[$name]; |
|
578 | } |
||
579 | |||
580 | return $this->_attributes[$name] != $this->_oldAttributes[$name]; |
||
581 | } |
||
582 | |||
583 | 1 | return isset($this->_attributes[$name]) || isset($this->_oldAttributes[$name]); |
|
584 | } |
||
585 | |||
586 | /** |
||
587 | * Returns the attribute values that have been modified since they are loaded or saved most recently. |
||
588 | * |
||
589 | * The comparison of new and old values is made for identical values using `===`. |
||
590 | * |
||
591 | * @param string[]|null $names the names of the attributes whose values may be returned if they are |
||
592 | * changed recently. If null, [[attributes()]] will be used. |
||
593 | * @return array the changed attribute values (name-value pairs) |
||
594 | */ |
||
595 | 98 | public function getDirtyAttributes($names = null) |
|
596 | { |
||
597 | 98 | if ($names === null) { |
|
598 | 95 | $names = $this->attributes(); |
|
599 | } |
||
600 | 98 | $names = array_flip($names); |
|
601 | 98 | $attributes = []; |
|
602 | 98 | if ($this->_oldAttributes === null) { |
|
603 | 85 | foreach ($this->_attributes as $name => $value) { |
|
604 | 82 | if (isset($names[$name])) { |
|
605 | 85 | $attributes[$name] = $value; |
|
606 | } |
||
607 | } |
||
608 | } else { |
||
609 | 34 | foreach ($this->_attributes as $name => $value) { |
|
610 | 34 | if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $value !== $this->_oldAttributes[$name])) { |
|
611 | 34 | $attributes[$name] = $value; |
|
612 | } |
||
613 | } |
||
614 | } |
||
615 | 98 | return $attributes; |
|
616 | } |
||
617 | |||
618 | /** |
||
619 | * Saves the current record. |
||
620 | * |
||
621 | * This method will call [[insert()]] when [[isNewRecord]] is `true`, or [[update()]] |
||
622 | * when [[isNewRecord]] is `false`. |
||
623 | * |
||
624 | * For example, to save a customer record: |
||
625 | * |
||
626 | * ```php |
||
627 | * $customer = new Customer; // or $customer = Customer::findOne($id); |
||
628 | * $customer->name = $name; |
||
629 | * $customer->email = $email; |
||
630 | * $customer->save(); |
||
631 | * ``` |
||
632 | * |
||
633 | * @param bool $runValidation whether to perform validation (calling [[validate()]]) |
||
634 | * before saving the record. Defaults to `true`. If the validation fails, the record |
||
635 | * will not be saved to the database and this method will return `false`. |
||
636 | * @param array $attributeNames list of attribute names that need to be saved. Defaults to null, |
||
637 | * meaning all attributes that are loaded from DB will be saved. |
||
638 | * @return bool whether the saving succeeded (i.e. no validation errors occurred). |
||
639 | */ |
||
640 | 92 | public function save($runValidation = true, $attributeNames = null) |
|
641 | { |
||
642 | 92 | if ($this->getIsNewRecord()) { |
|
643 | 79 | return $this->insert($runValidation, $attributeNames); |
|
644 | } |
||
645 | |||
646 | 27 | return $this->update($runValidation, $attributeNames) !== false; |
|
647 | } |
||
648 | |||
649 | /** |
||
650 | * Saves the changes to this active record into the associated database table. |
||
651 | * |
||
652 | * This method performs the following steps in order: |
||
653 | * |
||
654 | * 1. call [[beforeValidate()]] when `$runValidation` is `true`. If [[beforeValidate()]] |
||
655 | * returns `false`, the rest of the steps will be skipped; |
||
656 | * 2. call [[afterValidate()]] when `$runValidation` is `true`. If validation |
||
657 | * failed, the rest of the steps will be skipped; |
||
658 | * 3. call [[beforeSave()]]. If [[beforeSave()]] returns `false`, |
||
659 | * the rest of the steps will be skipped; |
||
660 | * 4. save the record into database. If this fails, it will skip the rest of the steps; |
||
661 | * 5. call [[afterSave()]]; |
||
662 | * |
||
663 | * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]], |
||
664 | * [[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_UPDATE]], and [[EVENT_AFTER_UPDATE]] |
||
665 | * will be raised by the corresponding methods. |
||
666 | * |
||
667 | * Only the [[dirtyAttributes|changed attribute values]] will be saved into database. |
||
668 | * |
||
669 | * For example, to update a customer record: |
||
670 | * |
||
671 | * ```php |
||
672 | * $customer = Customer::findOne($id); |
||
673 | * $customer->name = $name; |
||
674 | * $customer->email = $email; |
||
675 | * $customer->update(); |
||
676 | * ``` |
||
677 | * |
||
678 | * Note that it is possible the update does not affect any row in the table. |
||
679 | * In this case, this method will return 0. For this reason, you should use the following |
||
680 | * code to check if update() is successful or not: |
||
681 | * |
||
682 | * ```php |
||
683 | * if ($customer->update() !== false) { |
||
684 | * // update successful |
||
685 | * } else { |
||
686 | * // update failed |
||
687 | * } |
||
688 | * ``` |
||
689 | * |
||
690 | * @param bool $runValidation whether to perform validation (calling [[validate()]]) |
||
691 | * before saving the record. Defaults to `true`. If the validation fails, the record |
||
692 | * will not be saved to the database and this method will return `false`. |
||
693 | * @param array $attributeNames list of attribute names that need to be saved. Defaults to null, |
||
694 | * meaning all attributes that are loaded from DB will be saved. |
||
695 | * @return int|false the number of rows affected, or `false` if validation fails |
||
696 | * or [[beforeSave()]] stops the updating process. |
||
697 | * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data |
||
698 | * being updated is outdated. |
||
699 | * @throws Exception in case update failed. |
||
700 | */ |
||
701 | public function update($runValidation = true, $attributeNames = null) |
||
708 | |||
709 | /** |
||
710 | * Updates the specified attributes. |
||
711 | * |
||
712 | * This method is a shortcut to [[update()]] when data validation is not needed |
||
713 | * and only a small set attributes need to be updated. |
||
714 | * |
||
715 | * You may specify the attributes to be updated as name list or name-value pairs. |
||
716 | * If the latter, the corresponding attribute values will be modified accordingly. |
||
717 | * The method will then save the specified attributes into database. |
||
718 | * |
||
719 | * Note that this method will **not** perform data validation and will **not** trigger events. |
||
720 | * |
||
721 | * @param array $attributes the attributes (names or name-value pairs) to be updated |
||
722 | * @return int the number of rows affected. |
||
723 | */ |
||
724 | 4 | public function updateAttributes($attributes) |
|
725 | { |
||
726 | 4 | $attrs = []; |
|
727 | 4 | foreach ($attributes as $name => $value) { |
|
728 | 4 | if (is_int($name)) { |
|
729 | $attrs[] = $value; |
||
730 | } else { |
||
731 | 4 | $this->$name = $value; |
|
732 | 4 | $attrs[] = $name; |
|
733 | } |
||
734 | } |
||
735 | |||
736 | 4 | $values = $this->getDirtyAttributes($attrs); |
|
737 | 4 | if (empty($values) || $this->getIsNewRecord()) { |
|
738 | 4 | return 0; |
|
739 | } |
||
740 | |||
741 | 3 | $rows = static::updateAll($values, $this->getOldPrimaryKey(true)); |
|
742 | |||
743 | 3 | foreach ($values as $name => $value) { |
|
744 | 3 | $this->_oldAttributes[$name] = $this->_attributes[$name]; |
|
745 | } |
||
746 | |||
747 | 3 | return $rows; |
|
748 | } |
||
749 | |||
750 | /** |
||
751 | * @see update() |
||
752 | * @param array $attributes attributes to update |
||
753 | * @return int|false the number of rows affected, or false if [[beforeSave()]] stops the updating process. |
||
754 | * @throws StaleObjectException |
||
755 | */ |
||
756 | 30 | protected function updateInternal($attributes = null) |
|
757 | { |
||
758 | 30 | if (!$this->beforeSave(false)) { |
|
759 | return false; |
||
760 | } |
||
761 | 30 | $values = $this->getDirtyAttributes($attributes); |
|
762 | 30 | if (empty($values)) { |
|
763 | 3 | $this->afterSave(false, $values); |
|
764 | 3 | return 0; |
|
765 | } |
||
766 | 28 | $condition = $this->getOldPrimaryKey(true); |
|
767 | 28 | $lock = $this->optimisticLock(); |
|
768 | 28 | if ($lock !== null) { |
|
769 | 3 | $values[$lock] = $this->$lock + 1; |
|
770 | 3 | $condition[$lock] = $this->$lock; |
|
771 | } |
||
772 | // We do not check the return value of updateAll() because it's possible |
||
773 | // that the UPDATE statement doesn't change anything and thus returns 0. |
||
774 | 28 | $rows = static::updateAll($values, $condition); |
|
775 | |||
776 | 28 | if ($lock !== null && !$rows) { |
|
777 | 3 | throw new StaleObjectException('The object being updated is outdated.'); |
|
778 | } |
||
779 | |||
780 | 28 | if (isset($values[$lock])) { |
|
781 | 3 | $this->$lock = $values[$lock]; |
|
782 | } |
||
783 | |||
784 | 28 | $changedAttributes = []; |
|
785 | 28 | foreach ($values as $name => $value) { |
|
786 | 28 | $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null; |
|
787 | 28 | $this->_oldAttributes[$name] = $value; |
|
788 | } |
||
789 | 28 | $this->afterSave(false, $changedAttributes); |
|
790 | |||
791 | 28 | return $rows; |
|
792 | } |
||
793 | |||
794 | /** |
||
795 | * Updates one or several counter columns for the current AR object. |
||
796 | * Note that this method differs from [[updateAllCounters()]] in that it only |
||
797 | * saves counters for the current AR object. |
||
798 | * |
||
799 | * An example usage is as follows: |
||
800 | * |
||
801 | * ```php |
||
802 | * $post = Post::findOne($id); |
||
803 | * $post->updateCounters(['view_count' => 1]); |
||
804 | * ``` |
||
805 | * |
||
806 | * @param array $counters the counters to be updated (attribute name => increment value) |
||
807 | * Use negative values if you want to decrement the counters. |
||
808 | * @return bool whether the saving is successful |
||
809 | * @see updateAllCounters() |
||
810 | */ |
||
811 | 6 | public function updateCounters($counters) |
|
812 | { |
||
813 | 6 | if (static::updateAllCounters($counters, $this->getOldPrimaryKey(true)) > 0) { |
|
814 | 6 | foreach ($counters as $name => $value) { |
|
815 | 6 | if (!isset($this->_attributes[$name])) { |
|
816 | 3 | $this->_attributes[$name] = $value; |
|
817 | } else { |
||
818 | 3 | $this->_attributes[$name] += $value; |
|
819 | } |
||
820 | 6 | $this->_oldAttributes[$name] = $this->_attributes[$name]; |
|
821 | } |
||
822 | |||
823 | 6 | return true; |
|
824 | } |
||
825 | |||
826 | return false; |
||
827 | } |
||
828 | |||
829 | /** |
||
830 | * Deletes the table row corresponding to this active record. |
||
831 | * |
||
832 | * This method performs the following steps in order: |
||
833 | * |
||
834 | * 1. call [[beforeDelete()]]. If the method returns `false`, it will skip the |
||
835 | * rest of the steps; |
||
836 | * 2. delete the record from the database; |
||
837 | * 3. call [[afterDelete()]]. |
||
838 | * |
||
839 | * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]] |
||
840 | * will be raised by the corresponding methods. |
||
841 | * |
||
842 | * @return int|false the number of rows deleted, or `false` if the deletion is unsuccessful for some reason. |
||
843 | * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful. |
||
844 | * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data |
||
845 | * being deleted is outdated. |
||
846 | * @throws Exception in case delete failed. |
||
847 | */ |
||
848 | public function delete() |
||
849 | { |
||
850 | $result = false; |
||
851 | if ($this->beforeDelete()) { |
||
852 | // we do not check the return value of deleteAll() because it's possible |
||
853 | // the record is already deleted in the database and thus the method will return 0 |
||
854 | $condition = $this->getOldPrimaryKey(true); |
||
855 | $lock = $this->optimisticLock(); |
||
856 | if ($lock !== null) { |
||
857 | $condition[$lock] = $this->$lock; |
||
858 | } |
||
859 | $result = static::deleteAll($condition); |
||
860 | if ($lock !== null && !$result) { |
||
861 | throw new StaleObjectException('The object being deleted is outdated.'); |
||
862 | } |
||
863 | $this->_oldAttributes = null; |
||
864 | $this->afterDelete(); |
||
865 | } |
||
866 | |||
867 | return $result; |
||
868 | } |
||
869 | |||
870 | /** |
||
871 | * Returns a value indicating whether the current record is new. |
||
872 | * @return bool whether the record is new and should be inserted when calling [[save()]]. |
||
873 | */ |
||
874 | 129 | public function getIsNewRecord() |
|
878 | |||
879 | /** |
||
880 | * Sets the value indicating whether the record is new. |
||
881 | * @param bool $value whether the record is new and should be inserted when calling [[save()]]. |
||
882 | * @see getIsNewRecord() |
||
883 | */ |
||
884 | public function setIsNewRecord($value) |
||
888 | |||
889 | /** |
||
890 | * Initializes the object. |
||
891 | * This method is called at the end of the constructor. |
||
892 | * The default implementation will trigger an [[EVENT_INIT]] event. |
||
893 | * If you override this method, make sure you call the parent implementation at the end |
||
894 | * to ensure triggering of the event. |
||
895 | */ |
||
896 | 343 | public function init() |
|
901 | |||
902 | /** |
||
903 | * This method is called when the AR object is created and populated with the query result. |
||
904 | * The default implementation will trigger an [[EVENT_AFTER_FIND]] event. |
||
905 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
906 | * event is triggered. |
||
907 | */ |
||
908 | 264 | public function afterFind() |
|
912 | |||
913 | /** |
||
914 | * This method is called at the beginning of inserting or updating a record. |
||
915 | * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is `true`, |
||
916 | * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is `false`. |
||
917 | * When overriding this method, make sure you call the parent implementation like the following: |
||
918 | * |
||
919 | * ```php |
||
920 | * public function beforeSave($insert) |
||
921 | * { |
||
922 | * if (!parent::beforeSave($insert)) { |
||
923 | * return false; |
||
924 | * } |
||
925 | * |
||
926 | * // ...custom code here... |
||
927 | * return true; |
||
928 | * } |
||
929 | * ``` |
||
930 | * |
||
931 | * @param bool $insert whether this method called while inserting a record. |
||
932 | * If `false`, it means the method is called while updating a record. |
||
933 | * @return bool whether the insertion or updating should continue. |
||
934 | * If `false`, the insertion or updating will be cancelled. |
||
935 | */ |
||
936 | 100 | public function beforeSave($insert) |
|
943 | |||
944 | /** |
||
945 | * This method is called at the end of inserting or updating a record. |
||
946 | * The default implementation will trigger an [[EVENT_AFTER_INSERT]] event when `$insert` is `true`, |
||
947 | * or an [[EVENT_AFTER_UPDATE]] event if `$insert` is `false`. The event class used is [[AfterSaveEvent]]. |
||
948 | * When overriding this method, make sure you call the parent implementation so that |
||
949 | * the event is triggered. |
||
950 | * @param bool $insert whether this method called while inserting a record. |
||
951 | * If `false`, it means the method is called while updating a record. |
||
952 | * @param array $changedAttributes The old values of attributes that had changed and were saved. |
||
953 | * You can use this parameter to take action based on the changes made for example send an email |
||
954 | * when the password had changed or implement audit trail that tracks all the changes. |
||
955 | * `$changedAttributes` gives you the old attribute values while the active record (`$this`) has |
||
956 | * already the new, updated values. |
||
957 | * |
||
958 | * Note that no automatic type conversion performed by default. You may use |
||
959 | * [[\yii\behaviors\AttributeTypecastBehavior]] to facilitate attribute typecasting. |
||
960 | * See http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#attributes-typecasting. |
||
961 | */ |
||
962 | 95 | public function afterSave($insert, $changedAttributes) |
|
963 | { |
||
964 | 95 | $this->trigger($insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE, new AfterSaveEvent([ |
|
965 | 95 | 'changedAttributes' => $changedAttributes, |
|
966 | ])); |
||
967 | 95 | } |
|
968 | |||
969 | /** |
||
970 | * This method is invoked before deleting a record. |
||
971 | * The default implementation raises the [[EVENT_BEFORE_DELETE]] event. |
||
972 | * When overriding this method, make sure you call the parent implementation like the following: |
||
973 | * |
||
974 | * ```php |
||
975 | * public function beforeDelete() |
||
976 | * { |
||
977 | * if (!parent::beforeDelete()) { |
||
978 | * return false; |
||
979 | * } |
||
980 | * |
||
981 | * // ...custom code here... |
||
982 | * return true; |
||
983 | * } |
||
984 | * ``` |
||
985 | * |
||
986 | * @return bool whether the record should be deleted. Defaults to `true`. |
||
987 | */ |
||
988 | 6 | public function beforeDelete() |
|
995 | |||
996 | /** |
||
997 | * This method is invoked after deleting a record. |
||
998 | * The default implementation raises the [[EVENT_AFTER_DELETE]] event. |
||
999 | * You may override this method to do postprocessing after the record is deleted. |
||
1000 | * Make sure you call the parent implementation so that the event is raised properly. |
||
1001 | */ |
||
1002 | 6 | public function afterDelete() |
|
1006 | |||
1007 | /** |
||
1008 | * Repopulates this active record with the latest data. |
||
1009 | * |
||
1010 | * If the refresh is successful, an [[EVENT_AFTER_REFRESH]] event will be triggered. |
||
1011 | * This event is available since version 2.0.8. |
||
1012 | * |
||
1013 | * @return bool whether the row still exists in the database. If `true`, the latest data |
||
1014 | * will be populated to this active record. Otherwise, this record will remain unchanged. |
||
1015 | */ |
||
1016 | public function refresh() |
||
1017 | { |
||
1018 | /* @var $record BaseActiveRecord */ |
||
1019 | $record = static::findOne($this->getPrimaryKey(true)); |
||
1020 | return $this->refreshInternal($record); |
||
1021 | } |
||
1022 | |||
1023 | /** |
||
1024 | * Repopulates this active record with the latest data from a newly fetched instance. |
||
1025 | * @param BaseActiveRecord $record the record to take attributes from. |
||
1026 | * @return bool whether refresh was successful. |
||
1027 | * @see refresh() |
||
1028 | * @since 2.0.13 |
||
1029 | */ |
||
1030 | 25 | protected function refreshInternal($record) |
|
1044 | |||
1045 | /** |
||
1046 | * This method is called when the AR object is refreshed. |
||
1047 | * The default implementation will trigger an [[EVENT_AFTER_REFRESH]] event. |
||
1048 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
1049 | * event is triggered. |
||
1050 | * @since 2.0.8 |
||
1051 | */ |
||
1052 | 25 | public function afterRefresh() |
|
1056 | |||
1057 | /** |
||
1058 | * Returns a value indicating whether the given active record is the same as the current one. |
||
1059 | * The comparison is made by comparing the table names and the primary key values of the two active records. |
||
1060 | * If one of the records [[isNewRecord|is new]] they are also considered not equal. |
||
1061 | * @param ActiveRecordInterface $record record to compare to |
||
1062 | * @return bool whether the two active records refer to the same row in the same database table. |
||
1063 | */ |
||
1064 | public function equals($record) |
||
1072 | |||
1073 | /** |
||
1074 | * Returns the primary key value(s). |
||
1075 | * @param bool $asArray whether to return the primary key value as an array. If `true`, |
||
1076 | * the return value will be an array with column names as keys and column values as values. |
||
1077 | * Note that for composite primary keys, an array will always be returned regardless of this parameter value. |
||
1078 | * @property mixed The primary key value. An array (column name => column value) is returned if |
||
1079 | * the primary key is composite. A string is returned otherwise (null will be returned if |
||
1080 | * the key value is null). |
||
1081 | * @return mixed the primary key value. An array (column name => column value) is returned if the primary key |
||
1082 | * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if |
||
1083 | * the key value is null). |
||
1084 | */ |
||
1085 | 41 | public function getPrimaryKey($asArray = false) |
|
1099 | |||
1100 | /** |
||
1101 | * Returns the old primary key value(s). |
||
1102 | * This refers to the primary key value that is populated into the record |
||
1103 | * after executing a find method (e.g. find(), findOne()). |
||
1104 | * The value remains unchanged even if the primary key attribute is manually assigned with a different value. |
||
1105 | * @param bool $asArray whether to return the primary key value as an array. If `true`, |
||
1106 | * the return value will be an array with column name as key and column value as value. |
||
1107 | * If this is `false` (default), a scalar value will be returned for non-composite primary key. |
||
1108 | * @property mixed The old primary key value. An array (column name => column value) is |
||
1109 | * returned if the primary key is composite. A string is returned otherwise (null will be |
||
1110 | * returned if the key value is null). |
||
1111 | * @return mixed the old primary key value. An array (column name => column value) is returned if the primary key |
||
1112 | * is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if |
||
1113 | * the key value is null). |
||
1114 | * @throws Exception if the AR model does not have a primary key |
||
1115 | */ |
||
1116 | 56 | public function getOldPrimaryKey($asArray = false) |
|
1133 | |||
1134 | /** |
||
1135 | * Populates an active record object using a row of data from the database/storage. |
||
1136 | * |
||
1137 | * This is an internal method meant to be called to create active record objects after |
||
1138 | * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate |
||
1139 | * the query results into active records. |
||
1140 | * |
||
1141 | * When calling this method manually you should call [[afterFind()]] on the created |
||
1142 | * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]]. |
||
1143 | * |
||
1144 | * @param BaseActiveRecord $record the record to be populated. In most cases this will be an instance |
||
1145 | * created by [[instantiate()]] beforehand. |
||
1146 | * @param array $row attribute values (name => value) |
||
1147 | */ |
||
1148 | 264 | public static function populateRecord($record, $row) |
|
1160 | |||
1161 | /** |
||
1162 | * Creates an active record instance. |
||
1163 | * |
||
1164 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
1165 | * It is not meant to be used for creating new records directly. |
||
1166 | * |
||
1167 | * You may override this method if the instance being created |
||
1168 | * depends on the row data to be populated into the record. |
||
1169 | * For example, by creating a record based on the value of a column, |
||
1170 | * you may implement the so-called single-table inheritance mapping. |
||
1171 | * @param array $row row data to be populated into the record. |
||
1172 | * @return static the newly created active record |
||
1173 | */ |
||
1174 | 261 | public static function instantiate($row) |
|
1178 | |||
1179 | /** |
||
1180 | * Returns whether there is an element at the specified offset. |
||
1181 | * This method is required by the interface [[\ArrayAccess]]. |
||
1182 | * @param mixed $offset the offset to check on |
||
1183 | * @return bool whether there is an element at the specified offset. |
||
1184 | */ |
||
1185 | 27 | public function offsetExists($offset) |
|
1189 | |||
1190 | /** |
||
1191 | * Returns the relation object with the specified name. |
||
1192 | * A relation is defined by a getter method which returns an [[ActiveQueryInterface]] object. |
||
1193 | * It can be declared in either the Active Record class itself or one of its behaviors. |
||
1194 | * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive). |
||
1195 | * @param bool $throwException whether to throw exception if the relation does not exist. |
||
1196 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
1197 | * and `$throwException` is `false`, `null` will be returned. |
||
1198 | * @throws InvalidParamException if the named relation does not exist. |
||
1199 | */ |
||
1200 | 126 | public function getRelation($name, $throwException = true) |
|
1236 | |||
1237 | /** |
||
1238 | * Establishes the relationship between two models. |
||
1239 | * |
||
1240 | * The relationship is established by setting the foreign key value(s) in one model |
||
1241 | * to be the corresponding primary key value(s) in the other model. |
||
1242 | * The model with the foreign key will be saved into database without performing validation. |
||
1243 | * |
||
1244 | * If the relationship involves a junction table, a new row will be inserted into the |
||
1245 | * junction table which contains the primary key values from both models. |
||
1246 | * |
||
1247 | * Note that this method requires that the primary key value is not null. |
||
1248 | * |
||
1249 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
1250 | * @param ActiveRecordInterface $model the model to be linked with the current one. |
||
1251 | * @param array $extraColumns additional column values to be saved into the junction table. |
||
1252 | * This parameter is only meaningful for a relationship involving a junction table |
||
1253 | * (i.e., a relation set with [[ActiveRelationTrait::via()]] or [[ActiveQuery::viaTable()]].) |
||
1254 | * @throws InvalidCallException if the method is unable to link two models. |
||
1255 | */ |
||
1256 | 9 | public function link($name, $model, $extraColumns = []) |
|
1333 | |||
1334 | /** |
||
1335 | * Destroys the relationship between two models. |
||
1336 | * |
||
1337 | * The model with the foreign key of the relationship will be deleted if `$delete` is `true`. |
||
1338 | * Otherwise, the foreign key will be set `null` and the model will be saved without validation. |
||
1339 | * |
||
1340 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
1341 | * @param ActiveRecordInterface $model the model to be unlinked from the current one. |
||
1342 | * You have to make sure that the model is really related with the current model as this method |
||
1343 | * does not check this. |
||
1344 | * @param bool $delete whether to delete the model that contains the foreign key. |
||
1345 | * If `false`, the model's foreign key will be set `null` and saved. |
||
1346 | * If `true`, the model containing the foreign key will be deleted. |
||
1347 | * @throws InvalidCallException if the models cannot be unlinked |
||
1348 | */ |
||
1349 | 3 | public function unlink($name, $model, $delete = false) |
|
1432 | |||
1433 | /** |
||
1434 | * Destroys the relationship in current model. |
||
1435 | * |
||
1436 | * The model with the foreign key of the relationship will be deleted if `$delete` is `true`. |
||
1437 | * Otherwise, the foreign key will be set `null` and the model will be saved without validation. |
||
1438 | * |
||
1439 | * Note that to destroy the relationship without removing records make sure your keys can be set to null |
||
1440 | * |
||
1441 | * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method. |
||
1442 | * @param bool $delete whether to delete the model that contains the foreign key. |
||
1443 | * |
||
1444 | * Note that the deletion will be performed using [[deleteAll()]], which will not trigger any events on the related models. |
||
1445 | * If you need [[EVENT_BEFORE_DELETE]] or [[EVENT_AFTER_DELETE]] to be triggered, you need to [[find()|find]] the models first |
||
1446 | * and then call [[delete()]] on each of them. |
||
1447 | */ |
||
1448 | 18 | public function unlinkAll($name, $delete = false) |
|
1521 | |||
1522 | /** |
||
1523 | * @param array $link |
||
1524 | * @param ActiveRecordInterface $foreignModel |
||
1525 | * @param ActiveRecordInterface $primaryModel |
||
1526 | * @throws InvalidCallException |
||
1527 | */ |
||
1528 | 9 | private function bindModels($link, $foreignModel, $primaryModel) |
|
1543 | |||
1544 | /** |
||
1545 | * Returns a value indicating whether the given set of attributes represents the primary key for this model |
||
1546 | * @param array $keys the set of attributes to check |
||
1547 | * @return bool whether the given set of attributes represents the primary key for this model |
||
1548 | */ |
||
1549 | 15 | public static function isPrimaryKey($keys) |
|
1558 | |||
1559 | /** |
||
1560 | * Returns the text label for the specified attribute. |
||
1561 | * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. |
||
1562 | * @param string $attribute the attribute name |
||
1563 | * @return string the attribute label |
||
1564 | * @see generateAttributeLabel() |
||
1565 | * @see attributeLabels() |
||
1566 | */ |
||
1567 | 51 | public function getAttributeLabel($attribute) |
|
1598 | |||
1599 | /** |
||
1600 | * Returns the text hint for the specified attribute. |
||
1601 | * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. |
||
1602 | * @param string $attribute the attribute name |
||
1603 | * @return string the attribute hint |
||
1604 | * @see attributeHints() |
||
1605 | * @since 2.0.4 |
||
1606 | */ |
||
1607 | public function getAttributeHint($attribute) |
||
1637 | |||
1638 | /** |
||
1639 | * @inheritdoc |
||
1640 | * |
||
1641 | * The default implementation returns the names of the columns whose values have been populated into this record. |
||
1642 | */ |
||
1643 | public function fields() |
||
1649 | |||
1650 | /** |
||
1651 | * @inheritdoc |
||
1652 | * |
||
1653 | * The default implementation returns the names of the relations that have been populated into this record. |
||
1654 | */ |
||
1655 | public function extraFields() |
||
1661 | |||
1662 | /** |
||
1663 | * Sets the element value at the specified offset to null. |
||
1664 | * This method is required by the SPL interface [[\ArrayAccess]]. |
||
1665 | * It is implicitly called when you use something like `unset($model[$offset])`. |
||
1666 | * @param mixed $offset the offset to unset element |
||
1667 | */ |
||
1668 | 3 | public function offsetUnset($offset) |
|
1676 | } |
||
1677 |