Browse Source

Merge branch 'execut-fix-bug-with-many-fk'

tags/1.3.1
Alban Jubert 7 years ago
parent
commit
49b3d103fa
  1. 3
      src/SaveRelationsBehavior.php
  2. 15
      tests/SaveRelationsBehaviorTest.php

3
src/SaveRelationsBehavior.php

@ -182,6 +182,9 @@ class SaveRelationsBehavior extends Behavior
foreach ($modelClass::primaryKey() as $modelAttribute) { foreach ($modelClass::primaryKey() as $modelAttribute) {
if (array_key_exists($modelAttribute, $data) && !empty($data[$modelAttribute])) { if (array_key_exists($modelAttribute, $data) && !empty($data[$modelAttribute])) {
$fks[$modelAttribute] = $data[$modelAttribute]; $fks[$modelAttribute] = $data[$modelAttribute];
} else {
$fks = [];
break;
} }
} }
if (empty($fks)) { if (empty($fks)) {

15
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() public function testSaveNewHasManyRelationWithCompositeFksAsArrayShouldSucceed()
{ {
$project = Project::findOne(1); $project = Project::findOne(1);

Loading…
Cancel
Save