From b7b67ebcb3e8b7c88aebc461281598268312b670 Mon Sep 17 00:00:00 2001 From: leandrogehlen Date: Thu, 30 Aug 2018 14:20:46 -0300 Subject: [PATCH 1/3] Fixes error loading `hasMany` relation without referenced key in data --- src/SaveRelationsBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index 4811a5a..f7bea78 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -241,7 +241,7 @@ class SaveRelationsBehavior extends Behavior $fks[$modelAttribute] = $data[$modelAttribute]; } elseif ($relation->multiple && !$relation->via) { foreach ($link as $relatedAttribute => $relatedModelAttribute) { - if (!isset($data[$relatedAttribute])) { + if (!isset($data[$relatedAttribute]) && in_array($relatedAttribute, $modelClass::primaryKey())) { $fks[$relatedAttribute] = $this->owner->{$relatedModelAttribute}; } } From d2474cd1bdc9c6b8ccfa62a3d33c5a1fc3859582 Mon Sep 17 00:00:00 2001 From: leandrogehlen Date: Thu, 30 Aug 2018 20:08:00 -0300 Subject: [PATCH 2/3] Added tests --- tests/SaveRelationsBehaviorTest.php | 40 +++++++++++++++++++++++++++++++++++-- tests/models/Project.php | 9 +++++++++ tests/models/ProjectImage.php | 29 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/models/ProjectImage.php diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index d62450e..d0f9841 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -41,6 +41,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $db->createCommand()->dropTable('tags')->execute(); $db->createCommand()->dropTable('project_link')->execute(); $db->createCommand()->dropTable('project_contact')->execute(); + $db->createCommand()->dropTable('project_image')->execute(); $db->createCommand()->dropTable('dummy')->execute(); parent::tearDown(); } @@ -124,11 +125,18 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase // Project Contact $db->createCommand()->createTable('project_contact', [ 'project_id' => $migration->integer()->notNull(), - 'email' => $migration->text()->notNull(), - 'phone' => $migration->text(), + 'email' => $migration->string()->notNull(), + 'phone' => $migration->string(), 'PRIMARY KEY(project_id, email)' ])->execute(); + // Project Image + $db->createCommand()->createTable('project_image', [ + 'id' => $migration->primaryKey(), + 'project_id' => $migration->integer()->notNull(), + 'path' => $migration->string()->notNull() + ])->execute(); + // Dummy $db->createCommand()->createTable('dummy', [ 'id' => $migration->primaryKey(), @@ -183,6 +191,12 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase ['admin@apple.com', '(123) 456–7890', 1] ])->execute(); + $db->createCommand()->batchInsert('project_image', ['id', 'project_id', 'path'], [ + [1, 1, '/images/macosx.png'], + [2, 1, '/images/macosx_icon.png'], + [3, 2, '/images/windows.png'] + ])->execute(); + $db->createCommand()->batchInsert('project_user', ['project_id', 'user_id'], [ [1, 1], [1, 4], @@ -538,6 +552,28 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertEquals($project->contacts[1]->phone, '(987) 654–3210'); } + public function testLoadHasManyWithoutReferenceKeyShouldSucceed() + { + $project = Project::findOne(1); + $data = [ + 'ProjectImage' => [ + [ + 'path' => '/images/macosx_new.png' + ], + [ + 'id' => 2, + 'path' => '/images/macosx_updated.png' + ] + ] + ]; + $project->loadRelations($data); + $this->assertTrue($project->save(), 'Project could not be saved'); + $this->assertCount(2, $project->images, "Project should have 2 images"); + $this->assertEquals($project->images[0]->id, 2); + $this->assertEquals($project->images[0]->path, '/images/macosx_updated.png'); + $this->assertEquals($project->images[1]->path, '/images/macosx_new.png'); + } + public function testAssignSingleObjectToHasManyRelationShouldSucceed() { $project = new Project(); diff --git a/tests/models/Project.php b/tests/models/Project.php index d1b6bcd..dc0b32a 100644 --- a/tests/models/Project.php +++ b/tests/models/Project.php @@ -26,6 +26,7 @@ class Project extends \yii\db\ActiveRecord 'company', 'users', 'contacts', + 'images', 'links' => ['scenario' => Link::SCENARIO_FIRST], 'projectLinks' => ['cascadeDelete' => true], 'tags' => [ @@ -107,6 +108,14 @@ class Project extends \yii\db\ActiveRecord /** * @return \yii\db\ActiveQuery */ + public function getImages() + { + return $this->hasMany(ProjectImage::className(), ['project_id' => 'id']); + } + + /** + * @return \yii\db\ActiveQuery + */ public function getLinks() { return $this->hasMany(Link::className(), ['language' => 'language', 'name' => 'name'])->via('projectLinks'); diff --git a/tests/models/ProjectImage.php b/tests/models/ProjectImage.php new file mode 100644 index 0000000..a8fdb5c --- /dev/null +++ b/tests/models/ProjectImage.php @@ -0,0 +1,29 @@ + Date: Thu, 30 Aug 2018 20:12:22 -0300 Subject: [PATCH 3/3] Cleanup --- tests/models/ProjectImage.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/models/ProjectImage.php b/tests/models/ProjectImage.php index a8fdb5c..3305018 100644 --- a/tests/models/ProjectImage.php +++ b/tests/models/ProjectImage.php @@ -4,9 +4,6 @@ namespace tests\models; class ProjectImage extends \yii\db\ActiveRecord { - /** @var int */ - protected $order; - /** * @inheritdoc */