Browse Source

Fixed search bug with missed key of composite fks

tags/1.3.1
eXeCUT 7 years ago
parent
commit
7025ea4bf4
  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) {
if (array_key_exists($modelAttribute, $data) && !empty($data[$modelAttribute])) {
$fks[$modelAttribute] = $data[$modelAttribute];
} else {
$fks = [];
break;
}
}
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()
{
$project = Project::findOne(1);

Loading…
Cancel
Save