|
|
|
@ -133,10 +133,10 @@ $values = $customer->attributes;
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Persisting Data to Database |
|
|
|
|
--------------------------- |
|
|
|
|
Saving Data to Database |
|
|
|
|
----------------------- |
|
|
|
|
|
|
|
|
|
ActiveRecord provides the following methods to insert, update and delete data: |
|
|
|
|
ActiveRecord provides the following methods to insert, update and delete data in the database: |
|
|
|
|
|
|
|
|
|
- [[save()]] |
|
|
|
|
- [[insert()]] |
|
|
|
@ -207,9 +207,9 @@ a one-many relationship. For example, a customer has many orders. And the [[hasO
|
|
|
|
|
method declares a many-one or one-one relationship. For example, an order has one customer. |
|
|
|
|
Both methods take two parameters: |
|
|
|
|
|
|
|
|
|
- `$class`: the name of the class related models should use. If specified without |
|
|
|
|
a namespace, the namespace will be taken from the declaring class. |
|
|
|
|
- `$link`: the association between columns from two tables. This should be given as an array. |
|
|
|
|
- `$class`: the name of the class of the related model(s). If specified without |
|
|
|
|
a namespace, the namespace of the related model class will be taken from the declaring class. |
|
|
|
|
- `$link`: the association between columns from the two tables. This should be given as an array. |
|
|
|
|
The keys of the array are the names of the columns from the table associated with `$class`, |
|
|
|
|
while the values of the array are the names of the columns from the declaring class. |
|
|
|
|
It is a good practice to define relationships based on table foreign keys. |
|
|
|
@ -263,8 +263,8 @@ class Order extends \yii\db\ActiveRecord
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
[[ActiveRelation::via()]] method is similar to [[ActiveRelation::viaTable()]] except that |
|
|
|
|
the first parameter of [[ActiveRelation::via()]] takes a relation name declared in the ActiveRecord class. |
|
|
|
|
For example, the above `items` relation can be equivalently declared as follows: |
|
|
|
|
the first parameter of [[ActiveRelation::via()]] takes a relation name declared in the ActiveRecord class |
|
|
|
|
instead of the pivot table name. For example, the above `items` relation can be equivalently declared as follows: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
class Order extends \yii\db\ActiveRecord |
|
|
|
@ -315,7 +315,7 @@ How many SQL queries will be performed in the above code, assuming there are mor
|
|
|
|
|
the database? 101! The first SQL query brings back 100 customers. Then for each customer, a SQL query |
|
|
|
|
is performed to bring back the customer's orders. |
|
|
|
|
|
|
|
|
|
To solve the above performance problem, you can use the so-called *eager loading* by calling [[ActiveQuery::with()]]: |
|
|
|
|
To solve the above performance problem, you can use the so-called *eager loading* approach by calling [[ActiveQuery::with()]]: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
// SQL executed: SELECT * FROM tbl_customer LIMIT 100 |
|
|
|
@ -473,7 +473,7 @@ TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
|
|
|
|
|
|
|
|
|
|
Imagine situation where you have to save something related to the main model in [[beforeSave()]], |
|
|
|
|
[[afterSave()]], [[beforeDelete()]] and/or [[afterDelete()]] life cycle methods. Developer may come |
|
|
|
|
to solution of overriding ActiveRecord [[save()]] method with database transaction wrapping or |
|
|
|
|
to the solution of overriding ActiveRecord [[save()]] method with database transaction wrapping or |
|
|
|
|
even using transaction in controller action, which is strictly speaking doesn't seems to be a good |
|
|
|
|
practice (recall skinny-controller fat-model fundamental rule). |
|
|
|
|
|
|
|
|
|