// $project->company = ['id' => 3, 'name' => 'GiHub', 'description' => 'Awesome']; // Will update an existing company record
$project->save();
```
Attributes of the related model will be massively assigned using the `load() method. So remember to declare the according attributes as safe in the rules of the related model.
> **Notes:**
@ -162,6 +171,7 @@ Attributes of the related model will be massively assigned using the `load() met
> See the PHPUnit tests for more examples.
Populate additional junction table columns in a many-to-many relation
In a many-to-many relation involving a junction table additional column values can be saved to the junction table for each model.
@ -171,6 +181,7 @@ See the configuration section for examples.
> 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.
Validation
----------
Every declared related models will be validated prior to be saved. If any validation fails, for each related model attribute in error, an error associated with the named relation will be added to the owner model.
@ -179,6 +190,7 @@ For `hasMany()` relations, the index of the related model will be used to identi
It is possible to specify the validation scenario for each relation by declaring an associative array in which the `scenario` key must contain the needed scenario value.
For instance, in the following configuration, the `links ` related records will be validated using the `Link::SOME_SCENARIO` scenario:
```php
...
public function behaviors()
@ -200,12 +212,37 @@ For instance, in the following configuration, the `links ` related records will
> An error message will be attached to the relation attribute of the owner model.
> In order to be able to handle these cases in a user-friendly way, one will have to catch `yii\db\Exception` exceptions.
Delete related records when the main model is deleted