Skip to content
Permalink
Browse files

HasMany::replace taking into consideration success of delete statemen…

…ts while unlinking
  • Loading branch information...
mylux committed Nov 2, 2015
1 parent c1fa2bf commit 4bb24a91c00c5a0fbb33856ea74c412711cffc34
Showing with 338 additions and 14 deletions.
  1. +24 −14 src/ORM/Association/HasMany.php
  2. +314 −0 tests/TestCase/ORM/TableTest.php
@@ -149,7 +149,11 @@ public function saveAssociated(EntityInterface $entity, array $options = [])
$options['_sourceTable'] = $this->source();
if ($this->_saveStrategy === self::SAVE_REPLACE) {
$this->_unlinkAssociated($properties, $entity, $target, $targetEntities);
$unlinkSuccessful = $this->_unlinkAssociated($properties, $entity, $target, $targetEntities);
}
if (isset($unlinkSuccessful) && !$unlinkSuccessful) {
return false;
}
foreach ($targetEntities as $k => $targetEntity) {
@@ -367,7 +371,7 @@ public function replace(EntityInterface $sourceEntity, array $targetEntities, ar
* @param EntityInterface $entity the entity which should have its associated entities unassigned
* @param Table $target The associated table
* @param array $remainingEntities Entities that should not be deleted
* @return void
* @return bool success
*/
protected function _unlinkAssociated(array $properties, EntityInterface $entity, Table $target, array $remainingEntities = [])
{
@@ -396,7 +400,7 @@ function ($v) {
];
}
$this->_unlink(array_keys($properties), $target, $conditions);
return $this->_unlink(array_keys($properties), $target, $conditions);
}
/**
@@ -406,25 +410,31 @@ function ($v) {
* @param array $foreignKey array of foreign key properties
* @param Table $target The associated table
* @param array $conditions The conditions that specifies what are the objects to be unlinked
* @return void
* @return bool success
*/
protected function _unlink(array $foreignKey, Table $target, array $conditions = [])
{
$ok = true;
$mustBeDependent = (!$this->_foreignKeyAcceptsNull($target, $foreignKey) || $this->dependent());
if ($mustBeDependent) {
if ($this->_cascadeCallbacks) {
$query = $this->find('all')->where($conditions);
foreach ($query as $assoc) {
$target->delete($assoc);
$persistedEntitiesExist = $this->exists($conditions);
if ($persistedEntitiesExist) {
if ($mustBeDependent) {
if ($this->_cascadeCallbacks) {
$query = $this->find('all')->where($conditions);
foreach ($query as $assoc) {
$ok = $ok && $target->delete($assoc);
}
} else {
$ok = ($target->deleteAll($conditions) > 0);
}
} else {
$target->deleteAll($conditions);
}
} else {
$updateFields = array_fill_keys($foreignKey, null);
$target->updateAll($updateFields, $conditions);
$updateFields = array_fill_keys($foreignKey, null);
$ok = $target->updateAll($updateFields, $conditions);
}
}
return $ok;
}
/**
Oops, something went wrong.

0 comments on commit 4bb24a9

Please sign in to comment.
You can’t perform that action at this time.