Skip to content

Commit

Permalink
Adding missing Model::create() when using Model::save() in a loop. Fi…
Browse files Browse the repository at this point in the history
…xing some code formatting. Fixes #848
  • Loading branch information
ADmad committed Dec 17, 2011
1 parent e05d7d1 commit 248a2d3
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/Cake/Model/Behavior/TreeBehavior.php
Expand Up @@ -577,7 +577,6 @@ public function recover($Model, $mode = 'parent', $missingParentAction = null) {
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') {
Expand All @@ -588,13 +587,14 @@ public function recover($Model, $mode = 'parent', $missingParentAction = null) {
}
$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]);
}
Expand Down Expand Up @@ -846,11 +846,10 @@ protected function _setParent($Model, $parentId = null, $created = false) {

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),
Expand Down

0 comments on commit 248a2d3

Please sign in to comment.