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);