Skip to content

Commit

Permalink
Fixed bug in _unlinkAssociated where non necessary null values would …
Browse files Browse the repository at this point in the history
…appear into $conditions variable, causing trouble with replace save strategy and new associated entities to be created. Added a test to cover this case
  • Loading branch information
mylux committed Oct 4, 2015
1 parent 0b3afb8 commit e8f69d2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/ORM/Association/HasMany.php
Expand Up @@ -196,11 +196,19 @@ protected function _unlinkAssociated(array $properties, EntityInterface $entity,
$mustBeDependent = (!$this->_foreignKeyAcceptsNull($target, $properties) || $this->dependent());
$conditions = [
'NOT' => [
'OR' => array_map(
function ($ent) use ($primaryKey) {
return $ent->extract($primaryKey);
},
$remainingEntities
'OR' => array_merge(
array_filter(
array_map(
function ($ent) use ($primaryKey) {
return $ent->extract($primaryKey);
},
$remainingEntities
),
function ($v) {
return !in_array(null, array_values($v), true);
}
),
['1 =' => 0]
)
],
$properties
Expand Down
58 changes: 58 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1938,6 +1938,64 @@ public function testSaveReplaceSaveStrategyNotNullable()
$this->assertFalse($articles->Comments->exists(['id' => $commentId]));
}

/**
* Test that the associated entities are unlinked and deleted when they have a not nullable foreign key
*
* @return void
*/
public function testSaveReplaceSaveStrategyAdding()
{
$articles = $this->getMock(
'Cake\ORM\Table',
['exists'],
[
[
'connection' => $this->connection,
'alias' => 'Articles',
'table' => 'articles',
]
]
);

$articles->hasMany('Comments', ['saveStrategy' => 'replace']);

$article = $articles->newEntity([
'title' => 'Bakeries are sky rocketing',
'body' => 'All because of cake',
'comments' => [
[
'user_id' => 1,
'comment' => 'That is true!'
],
[
'user_id' => 2,
'comment' => 'Of course'
]
]
], ['associated' => ['Comments']]);

$article = $articles->save($article, ['associated' => ['Comments']]);
$commentId = $article->comments[0]->id;
$sizeComments = count($article->comments);
$articleId = $article->id;

$this->assertEquals($sizeComments, $articles->Comments->find('all')->where(['article_id' => $article->id])->count());
$this->assertTrue($articles->Comments->exists(['id' => $commentId]));

unset($article->comments[0]);
$article->comments[] = $articles->comments->newEntity([
'user_id' => 1,
'comment' => 'new comment'
]);

$article->dirty('comments', true);
$article = $articles->save($article, ['associated' => ['Comments']]);

$this->assertEquals($sizeComments, $articles->Comments->find('all')->where(['article_id' => $article->id])->count());
$this->assertFalse($articles->Comments->exists(['id' => $commentId]));
$this->assertTrue($articles->Comments->exists(['comment' => 'new comment', 'article_id' => $articleId]));
}

/**
* Test that saving a new entity with a Primary Key set does not call exists when checkExisting is false.
*
Expand Down

0 comments on commit e8f69d2

Please sign in to comment.