> Info: The `save()` method will call either `insert()` or `update()`, depending on whether
> Info: The `save()` method will call either `insert()` or `update()`, depending on whether
the Active Record instance is new or not (internally it will check the value of [[yii\db\ActiveRecord::isNewRecord]]).
the Active Record instance is new or not (internally it will check the value of [[yii\db\ActiveRecord::isNewRecord]]).
If an Active Record is instantiated via the `new` operator, calling `save()` will
If an Active Record is instantiated via the `new` operator, calling `save()` will
insert a row in the table; calling `save()` on active record fetched from database will update the corresponding
insert a row in the table; calling `save()` on an active record fetched from the database will update the corresponding
row in the table.
row in the table.
...
@@ -302,7 +305,7 @@ and you may call [[yii\base\Model::validate()|validate()]] to trigger data valid
...
@@ -302,7 +305,7 @@ and you may call [[yii\base\Model::validate()|validate()]] to trigger data valid
When you call `save()`, `insert()` or `update()`, these methods will automatically call [[yii\base\Model::validate()|validate()]].
When you call `save()`, `insert()` or `update()`, these methods will automatically call [[yii\base\Model::validate()|validate()]].
If the validation fails, the corresponding data saving operation will be cancelled.
If the validation fails, the corresponding data saving operation will be cancelled.
The following example shows how to use an Active Record to collect/validate user input and save them into database:
The following example shows how to use an Active Record to collect/validate user input and save them into the database:
```php
```php
// creating a new record
// creating a new record
...
@@ -373,7 +376,7 @@ the following life cycles:
...
@@ -373,7 +376,7 @@ the following life cycles:
4. perform the actual data insertion or updating
4. perform the actual data insertion or updating
5.[[yii\db\ActiveRecord::afterSave()|afterSave()]]: will trigger an [[yii\db\ActiveRecord::EVENT_AFTER_INSERT|EVENT_AFTER_INSERT]] or [[yii\db\ActiveRecord::EVENT_AFTER_UPDATE|EVENT_AFTER_UPDATE]] event
5.[[yii\db\ActiveRecord::afterSave()|afterSave()]]: will trigger an [[yii\db\ActiveRecord::EVENT_AFTER_INSERT|EVENT_AFTER_INSERT]] or [[yii\db\ActiveRecord::EVENT_AFTER_UPDATE|EVENT_AFTER_UPDATE]] event
And Finally when calling [[yii\db\ActiveRecord::delete()|delete()]] to delete an ActiveRecord, we will have
And finally, when calling [[yii\db\ActiveRecord::delete()|delete()]] to delete an ActiveRecord, we will have
the following life cycles:
the following life cycles:
1.[[yii\db\ActiveRecord::beforeDelete()|beforeDelete()]]: will trigger an [[yii\db\ActiveRecord::EVENT_BEFORE_DELETE|EVENT_BEFORE_DELETE]] event
1.[[yii\db\ActiveRecord::beforeDelete()|beforeDelete()]]: will trigger an [[yii\db\ActiveRecord::EVENT_BEFORE_DELETE|EVENT_BEFORE_DELETE]] event
> Note: A relation method returns an instance of [[yii\db\ActiveQuery]]. If you access the relation like
> Note: A relation method returns an instance of [[yii\db\ActiveQuery]]. If you access the relation like
an attribute (i.e. a class property), the return value will be the query result of the relation, which could be an instance of [[yii\db\ActiveRecord]],
an attribute (i.e. a class property), the return value will be the query result of the relation, which could be an instance of [[yii\db\ActiveRecord]],
an array of that, or null, depending the multiplicity of the relation. For example, `$customer->getOrders()` returns
an array of that, or null, depending on the multiplicity of the relation. For example, `$customer->getOrders()` returns
an `ActiveQuery` instance, while `$customer->orders` returns an array of `Order` objects (or an empty array if
an `ActiveQuery` instance, while `$customer->orders` returns an array of `Order` objects (or an empty array if
the query results in nothing).
the query results in nothing).
...
@@ -482,7 +485,7 @@ the query results in nothing).
...
@@ -482,7 +485,7 @@ the query results in nothing).
Relations with Junction Table
Relations with Junction Table
-----------------------------
-----------------------------
Sometimes, two tables are related together via an intermediary table called [junction table][]. To declare such relations,
Sometimes, two tables are related together via an intermediary table called a [junction table][]. To declare such relations,
we can customize the [[yii\db\ActiveQuery]] object by calling its [[yii\db\ActiveQuery::via()|via()]] or
we can customize the [[yii\db\ActiveQuery]] object by calling its [[yii\db\ActiveQuery::via()|via()]] or