diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index d0787b9..544fc0d 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -13,6 +13,7 @@ use tests\models\ProjectLink; use tests\models\ProjectNoTransactions; use tests\models\User; +use tests\models\Tag; use Yii; use yii\base\Model; use yii\db\Migration; @@ -36,6 +37,8 @@ protected function tearDown() $db->createCommand()->dropTable('company')->execute(); $db->createCommand()->dropTable('link_type')->execute(); $db->createCommand()->dropTable('link')->execute(); + $db->createCommand()->dropTable('project_tags')->execute(); + $db->createCommand()->dropTable('tags')->execute(); $db->createCommand()->dropTable('project_link')->execute(); $db->createCommand()->dropTable('dummy')->execute(); parent::tearDown(); @@ -80,6 +83,17 @@ protected function setupDbData() 'PRIMARY KEY(language, name)' ])->execute(); + $db->createCommand()->createTable('tags', [ + 'id' => $migration->primaryKey(), + 'name' => $migration->string()->notNull()->unique() + ])->execute(); + + $db->createCommand()->createTable('project_tags', [ + 'project_id' => $migration->integer()->notNull(), + 'tag_id' => $migration->integer()->notNull(), + 'order' => $migration->integer()->notNull() + ])->execute(); + $db->createCommand()->createTable('link_type', [ 'id' => $migration->primaryKey(), 'name' => $migration->string()->notNull()->unique() @@ -367,6 +381,28 @@ public function testSaveUpdatedHasManyRelationWithCompositeFksAsArrayShouldSucce ); } + public function testSaveNewManyRelationJunctionTableColumnsShouldSucceed() + { + $project = Project::findOne(1); + $firstTag = new Tag(); + $firstTag->name = 'Tag One'; + $firstTag->setOrder(1); + $secondTag = new Tag(); + $secondTag->name = 'Tag Two'; + $secondTag->setOrder(3); + $project->tags = [ + $firstTag, + $secondTag + ]; + $this->assertTrue($project->save(), 'Project could not be saved'); + $this->assertCount(2, $project->tags, 'Project should have 4 tags after assignment'); + + $firstTagJunctionTableColumns = (new \yii\db\Query())->from('project_tags')->where(['tag_id' => $firstTag->id])->one(); + $secondTagJunctionTableColumns = (new \yii\db\Query())->from('project_tags')->where(['tag_id' => $secondTag->id])->one(); + $this->assertEquals($firstTag->getOrder(), $firstTagJunctionTableColumns['order']); + $this->assertEquals($secondTag->getOrder(), $secondTagJunctionTableColumns['order']); + } + public function testSaveMixedRelationsShouldSucceed() { $project = new Project(); diff --git a/tests/models/Project.php b/tests/models/Project.php index 652dfe2..48d686f 100644 --- a/tests/models/Project.php +++ b/tests/models/Project.php @@ -22,7 +22,15 @@ public function behaviors() return [ 'saveRelations' => [ 'class' => SaveRelationsBehavior::className(), - 'relations' => ['company', 'users', 'links'] + 'relations' => ['company', 'users', 'links', 'tags'], + 'junctionTableColumns' => [ + 'tags' => function ($model) { + /** @var $model Tag */ + return [ + 'order' => $model->getOrder() + ]; + } + ] ], ]; } @@ -88,4 +96,12 @@ public function getLinks() return $this->hasMany(Link::className(), ['language' => 'language', 'name' => 'name'])->via('projectLinks'); } + /** + * @return \yii\db\ActiveQuery + */ + public function getTags() + { + return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('project_tags', ['project_id' => 'id']); + } + } \ No newline at end of file diff --git a/tests/models/Tag.php b/tests/models/Tag.php new file mode 100644 index 0000000..5568240 --- /dev/null +++ b/tests/models/Tag.php @@ -0,0 +1,44 @@ +order = $order; + } + + /** + * @return int + */ + public function getOrder() + { + return $this->order; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [['name'], 'required'], + [['name'], 'string'] + ]; + } +}