From 1be081e86bcc49ace380ca6ebf4e13271442bbe4 Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Sat, 28 Oct 2017 15:08:40 +0200 Subject: [PATCH] Updated README --- README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56bd593..e645ce9 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,18 @@ class Project extends \yii\db\ActiveRecord ... 'saveRelations' => [ 'class' => SaveRelationsBehavior::className(), - 'relations' => ['users', 'company'] + 'relations' => [ + 'company', + 'users', + 'tags' => [ + 'extraColumns' => function ($model) { + /** @var $model Tag */ + return [ + 'order' => $model->order + ]; + } + ] + ], ], ]; } @@ -89,6 +100,13 @@ class Project extends \yii\db\ActiveRecord return $this->hasMany(User::className(), ['id' => 'user_id'])->via('ProjectUsers'); } + /** + * @return ActiveQuery + */ + public function getTags() + { + return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('ProjectTags', ['project_id' => 'id']); + } } ``` > Though not mandatory, it is highly recommended to activate the transactions for the owner model. @@ -129,6 +147,15 @@ 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. + +> **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 +> to ensure that changes to the junction table properties are saved too. +> See the configuration section for examples. + 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.