Skip to content

Commit

Permalink
Improving _unlinkAssociated to avoid unecessary queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mylux committed Oct 4, 2015
1 parent e8f69d2 commit 6ba93e9
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/ORM/Association/HasMany.php
Expand Up @@ -194,38 +194,39 @@ protected function _unlinkAssociated(array $properties, EntityInterface $entity,
{
$primaryKey = (array)$target->primaryKey();
$mustBeDependent = (!$this->_foreignKeyAcceptsNull($target, $properties) || $this->dependent());
$conditions = [
'NOT' => [
'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
];
$exclusions = array_filter(
array_map(
function ($ent) use ($primaryKey) {
return $ent->extract($primaryKey);
},
$remainingEntities
),
function ($v) {
return !in_array(null, array_values($v), true);
}
);

if (count($exclusions) > 0) {
$conditions = [
'NOT' => [
'OR' => $exclusions
],
$properties
];

if ($mustBeDependent) {
if ($this->_cascadeCallbacks) {
$query = $this->find('all')->where($conditions);
foreach ($query as $assoc) {
$target->delete($assoc);
if ($mustBeDependent) {
if ($this->_cascadeCallbacks) {
$query = $this->find('all')->where($conditions);
foreach ($query as $assoc) {
$target->delete($assoc);
}
} else {
$target->deleteAll($conditions);
}
} else {
$target->deleteAll($conditions);
$updateFields = array_fill_keys(array_keys($properties), null);
$target->updateAll($updateFields, $conditions);
}
} else {
$updateFields = array_fill_keys(array_keys($properties), null);
$target->updateAll($updateFields, $conditions);
}
}

Expand Down

0 comments on commit 6ba93e9

Please sign in to comment.