Skip to content

Commit

Permalink
Merge pull request #43 from EtienneBourque/fix-preserve-join-data
Browse files Browse the repository at this point in the history
Fixed a regression with the preserveJoinData option
  • Loading branch information
ADmad authored Dec 7, 2019
2 parents 80313d2 + 5570489 commit 65d38be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Model/Behavior/DuplicatableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ protected function _getContain()
protected function _modifyEntity(EntityInterface $entity, $object)
{
// belongs to many is tricky
if ($object instanceof BelongsToMany && ! $this->getConfig('preserveJoinData')) {
if ($object instanceof BelongsToMany && !$this->getConfig('preserveJoinData')) {
unset($entity->_joinData);
} else {
} elseif (!$object instanceof BelongsToMany) {
// unset primary key
unset($entity->{$object->getPrimaryKey()});

Expand Down
24 changes: 8 additions & 16 deletions tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,28 +213,20 @@ public function testPreserveJoinData()
'contain' => [
'Tags',
],
'preserveJoinData' => false,
'preserveJoinData' => true,
]);

$duplicated = $this->Invoices->duplicateEntity(1);

$this->assertEquals(false, isset($duplicated->tags[0]->_joinData));
$this->assertEquals(false, isset($duplicated->tags[1]->_joinData));

$this->Invoices->removeBehavior('Duplicatable');

$this->Invoices->addBehavior('Duplicatable.Duplicatable', [
$result = $this->Invoices->duplicate(1);
$invoice = $this->Invoices->get($result->id, [
'contain' => [
'Tags',
],
'preserveJoinData' => true,
]
]);

$duplicated = $this->Invoices->duplicateEntity(1);
$this->assertEquals(true, $invoice->tags[0]->_joinData->is_preserved);
$this->assertEquals(true, $invoice->tags[1]->_joinData->is_preserved);

$this->assertEquals(true, isset($duplicated->tags[0]->_joinData));
$this->assertEquals(true, isset($duplicated->tags[1]->_joinData));
$this->assertEquals(true, $duplicated->tags[0]->_joinData->is_preserved);
$this->assertEquals(true, $duplicated->tags[1]->_joinData->is_preserved);
// check that tags are not duplicated
$this->assertEquals(2, $this->Invoices->Tags->find()->count());
}
}

0 comments on commit 65d38be

Please sign in to comment.