Browse Source

Fix for empty value assignment on HasMany relations

tags/1.1.2
Alban Jubert 8 years ago
parent
commit
abf79e18d8
  1. 4
      src/SaveRelationsBehavior.php
  2. 9
      tests/SaveRelationsBehaviorTest.php

4
src/SaveRelationsBehavior.php

@ -85,7 +85,11 @@ class SaveRelationsBehavior extends Behavior
if ($relation->multiple === true) { if ($relation->multiple === true) {
$newRelations = []; $newRelations = [];
if (!is_array($value)) { if (!is_array($value)) {
if (!empty($value)) {
$value = [$value]; $value = [$value];
} else {
$value = [];
}
} }
foreach ($value as $entry) { foreach ($value as $entry) {
if ($entry instanceof $relation->modelClass) { if ($entry instanceof $relation->modelClass) {

9
tests/SaveRelationsBehaviorTest.php

@ -436,6 +436,14 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($project->users), 'Project should have 1 users after assignment'); $this->assertEquals(1, count($project->users), 'Project should have 1 users after assignment');
} }
public function testAssignSingleEmptyObjectToHasManyRelationShouldSucceed()
{
$project = new Project();
$user = User::findOne(1);
$project->users = null;
$this->assertEquals(0, count($project->users), 'Project should have 0 users after assignment');
}
public function testChangeHasOneRelationWithAnotherObject() public function testChangeHasOneRelationWithAnotherObject()
{ {
$dummy_a = new DummyModel(); $dummy_a = new DummyModel();
@ -447,7 +455,6 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($dummy_a->save(), 'Dummy A could not be saved'); $this->assertTrue($dummy_a->save(), 'Dummy A could not be saved');
$this->assertTrue($dummy_b->save(), 'Dummy B could not be saved'); $this->assertTrue($dummy_b->save(), 'Dummy B could not be saved');
$dummy_c = new DummyModel(); $dummy_c = new DummyModel();
//$this->assertTrue($dummy_c->save(), 'Dummy C could not be saved');
$dummy_a->children = $dummy_c; $dummy_a->children = $dummy_c;
$this->assertTrue($dummy_a->save(), 'Dummy A could not be saved'); $this->assertTrue($dummy_a->save(), 'Dummy A could not be saved');
} }

Loading…
Cancel
Save