From 248a2d3f261229dcb7da30b82d90071bd8a72ab2 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 18 Dec 2011 03:43:36 +0530 Subject: [PATCH] Adding missing Model::create() when using Model::save() in a loop. Fixing some code formatting. Fixes #848 --- lib/Cake/Model/Behavior/TreeBehavior.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Model/Behavior/TreeBehavior.php b/lib/Cake/Model/Behavior/TreeBehavior.php index ff14fc98d..be6825fb6 100644 --- a/lib/Cake/Model/Behavior/TreeBehavior.php +++ b/lib/Cake/Model/Behavior/TreeBehavior.php @@ -577,7 +577,6 @@ class TreeBehavior extends ModelBehavior { if ($missingParentAction == 'return') { foreach ($missingParents as $id => $display) { $this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')'; - } return false; } elseif ($missingParentAction == 'delete') { @@ -588,13 +587,14 @@ class TreeBehavior extends ModelBehavior { } $count = 1; foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) { - $Model->id = $array[$Model->alias][$Model->primaryKey]; $lft = $count++; $rght = $count++; + $Model->create(false); + $Model->id = $array[$Model->alias][$Model->primaryKey]; $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false)); } foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) { - $Model->create(); + $Model->create(false); $Model->id = $array[$Model->alias][$Model->primaryKey]; $this->_setParent($Model, $array[$Model->alias][$parent]); } @@ -846,11 +846,10 @@ class TreeBehavior extends ModelBehavior { if (($Model->id == $parentId)) { return false; - } elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) { return false; } - if (empty ($node[$left]) && empty ($node[$right])) { + if (empty($node[$left]) && empty($node[$right])) { $this->_sync($Model, 2, '+', '>= ' . $parentNode[$right], $created); $result = $Model->save( array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId),