@ -129,6 +139,8 @@ class Project extends \yii\db\ActiveRecord
```
```
> Though not mandatory, it is highly recommended to activate the transactions for the owner model.
> Though not mandatory, it is highly recommended to activate the transactions for the owner model.
> ⚠️ Relations attributes has to be defined as `safe` in owner model validation rules in order to be saved.
Usage
Usage
@ -167,7 +179,6 @@ Attributes of the related model will be massively assigned using the `load() met
> **Note:**
> **Note:**
> Only newly created or changed related models will be saved.
> Only newly created or changed related models will be saved.
> See the PHPUnit tests for more examples.
> See the PHPUnit tests for more examples.
@ -177,8 +188,7 @@ In a many-to-many relation involving a junction table additional column values c
See the configuration section for examples.
See the configuration section for examples.
> **Note:**
> **Note:**
> If junction table properties are configured for a relation the rows associated with the related models in the junction table will be deleted and inserted again on each saving
> If junction table properties are configured for a relation, the rows associated with the related models in the junction table will be deleted and inserted again on each saving to ensure that changes to the junction table properties are saved too.
> to ensure that changes to the junction table properties are saved too.
Validation
Validation
@ -238,7 +248,7 @@ For example, related `projectLinks` records will automatically be deleted when t
...
...
```
```
> **Note:**
> **Note:**.
> Every records related to the main model as they are defined in their `ActiveQuery` statement will be deleted.
> Every records related to the main model as they are defined in their `ActiveQuery` statement will be deleted.
You can even further simplify the process by adding the `SaveRelationsTrait` to your model.
You can even further simplify the process by adding the `SaveRelationsTrait` to your model. In that case, a call to the `load()` method will also automatically trigger a call to the `loadRelations()` method by using the same data, so you basically won't have to change your controllers.
In that case, a call to the `load()` method will also automatically trigger a call to the `loadRelations()` method by using the same data, so you basically won't have to change your controllers.
The `relationKeyName` property can be used to decide how the relations data will be retrieved from the data parameter.
The `relationKeyName` property can be used to decide how the relations data will be retrieved from the data parameter.
Possible constants values are:
Possible constants values are:
* `SaveRelationsBehavior::RELATION_KEY_FORM_NAME` (default): the key name will be computed using the model [`formName()`](https://www.yiiframework.com/doc/api/2.0/yii-base-model#formName()-detail) method
* `SaveRelationsBehavior::RELATION_KEY_FORM_NAME` (default): the key name will be computed using the model [`formName()`](https://www.yiiframework.com/doc/api/2.0/yii-base-model#formName()-detail) method
* `SaveRelationsBehavior::RELATION_KEY_RELATION_NAME`: the relation name as defined in the behavior declarations will be used
* `SaveRelationsBehavior::RELATION_KEY_RELATION_NAME`: the relation name as defined in the behavior declarations will be used
@ -288,10 +298,12 @@ Get old relations values
------------------------
------------------------
To retrieve relations value prior to there most recent modification until the model is saved, the following methods can be used:
To retrieve relations value prior to there most recent modification until the model is saved, the following methods can be used:
* `getOldRelation($name)`: Get a named relation old value.
* `getOldRelation($name)`: Get a named relation old value.
* `getOldRelations()`: Get an array of relations index by there name with there old values.
* `getOldRelations()`: Get an array of relations index by there name with there old values.
> **Notes**
> **Notes:**
>
> * If a relation has not been modified yet, its initial value will be returned
> * If a relation has not been modified yet, its initial value will be returned
> * Only relations defined in the behavior parameters will be returned
> * Only relations defined in the behavior parameters will be returned
@ -299,6 +311,7 @@ To retrieve relations value prior to there most recent modification until the mo
Get dirty relations
Get dirty relations
-------------------
-------------------
To deal with dirty (modified) relations since the model was loaded, the following methods can be used:
To deal with dirty (modified) relations since the model was loaded, the following methods can be used:
* `getDirtyRelations()`: Get the relations that have been modified since they are loaded (name-value pairs)
* `getDirtyRelations()`: Get the relations that have been modified since they are loaded (name-value pairs)
* `markRelationDirty($name)`: Mark a relation as dirty even if it's not been modified.
* `markRelationDirty($name)`: Mark a relation as dirty even if it's not been modified.