Skip to content

Commit

Permalink
Apply patch from 'Carlos Gant'
Browse files Browse the repository at this point in the history
Remove un-necessary loop when deleting without cascade.
Fixes #2050
  • Loading branch information
markstory committed Nov 5, 2011
1 parent 05df15c commit 15753ab
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/Cake/Model/Model.php
Expand Up @@ -2244,26 +2244,28 @@ protected function _deleteDependent($id, $cascade) {
$savedAssociatons = $this->__backAssociation;
$this->__backAssociation = array();
}
foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) {
if ($data['dependent'] === true && $cascade === true) {

$model = $this->{$assoc};
$conditions = array($model->escapeField($data['foreignKey']) => $id);
if ($data['conditions']) {
$conditions = array_merge((array)$data['conditions'], $conditions);
}
$model->recursive = -1;
if ($cascade === true) {
foreach (array_merge($this->hasMany, $this->hasOne) as $assoc => $data) {
if ($data['dependent'] === true) {

$model = $this->{$assoc};
$conditions = array($model->escapeField($data['foreignKey']) => $id);
if ($data['conditions']) {
$conditions = array_merge((array)$data['conditions'], $conditions);
}
$model->recursive = -1;

if (isset($data['exclusive']) && $data['exclusive']) {
$model->deleteAll($conditions);
} else {
$records = $model->find('all', array(
'conditions' => $conditions, 'fields' => $model->primaryKey
));
if (isset($data['exclusive']) && $data['exclusive']) {
$model->deleteAll($conditions);
} else {
$records = $model->find('all', array(
'conditions' => $conditions, 'fields' => $model->primaryKey
));

if (!empty($records)) {
foreach ($records as $record) {
$model->delete($record[$model->alias][$model->primaryKey]);
if (!empty($records)) {
foreach ($records as $record) {
$model->delete($record[$model->alias][$model->primaryKey]);
}
}
}
}
Expand Down

3 comments on commit 15753ab

@fitorec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about I have a question about the condition logic in the Model file in line L678 on __contruct method.

https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.php#L678

In my opinion always gives true, for example:

<?php
function foo($actsAs){
    if ($actsAs !== null || $actsAs !== false) {
                echo "always runs!\n";
    }else
        var_dump($actsAs);
}

foo(false);
foo(null);
foo(
    array('Translate', 'MyBehavior' => array('setting1' => 'value1'))
);
foo('other mensaje');

@fitorec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OUT

always runs!
always runs!
always runs!
always runs!

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps that || should be an &&, or removed.

Please sign in to comment.