Permalink
Browse files

Applying patch from 'Carlos Gant'

Removes un-necessary loop when deleting records with cascade = false.
Fixes #2050
  • Loading branch information...
1 parent b75c414 commit 1a2d631130cd9fd3009ddc68942d02fdc3e67742 @markstory markstory committed Nov 5, 2011
Showing with 21 additions and 19 deletions.
  1. +21 −19 cake/libs/model/model.php
View
@@ -1864,26 +1864,28 @@ 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]);
+ }
}
}
}
@@ -2062,7 +2064,7 @@ function hasAny($conditions = null) {
* - If three fields are specified, they are used (in order) for key, value and group.
* - Otherwise, first and second fields are used for key and value.
*
- * Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you
+ * Note: find(list) + database views have issues with MySQL 5.0. Try upgrading to MySQL 5.1 if you
* have issues with database views.
*
* @param array $conditions SQL conditions array, or type of find operation (all / first / count /

0 comments on commit 1a2d631

Please sign in to comment.