From 7025ea4bf4ab673767a77307b7ff8e5a3bcdc9b6 Mon Sep 17 00:00:00 2001 From: eXeCUT Date: Fri, 20 Oct 2017 10:41:35 +0300 Subject: [PATCH] Fixed search bug with missed key of composite fks --- src/SaveRelationsBehavior.php | 3 +++ tests/SaveRelationsBehaviorTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index 37b4676..5e7b1b5 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -182,6 +182,9 @@ class SaveRelationsBehavior extends Behavior foreach ($modelClass::primaryKey() as $modelAttribute) { if (array_key_exists($modelAttribute, $data) && !empty($data[$modelAttribute])) { $fks[$modelAttribute] = $data[$modelAttribute]; + } else { + $fks = []; + break; } } if (empty($fks)) { diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index 5a1e7b7..ee86de2 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -326,6 +326,21 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase ); } + public function testCreateHasManyRelationWithOneOfTheMissingKeyOfCompositeFk() + { + $project = Project::findOne(1); + $project->links = [ + [ + 'language' => 'fr', + ] + ]; + $this->assertCount(1, $project->links, 'Project should have 1 links after assignment'); + $this->assertTrue( + $project->links[0]->isNewRecord, + 'Related link without one of the missed key of composite fk must be is new record' + ); + } + public function testSaveNewHasManyRelationWithCompositeFksAsArrayShouldSucceed() { $project = Project::findOne(1);