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 and updating api doc blocks. Fixes #848
  • Loading branch information
ADmad committed Dec 17, 2011
1 parent 850a33d commit 90bcb75
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions cake/libs/model/behaviors/tree.php
Expand Up @@ -589,7 +589,6 @@ 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 @@ -600,13 +599,14 @@ 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 @@ -827,6 +827,7 @@ function verify(&$Model) {
*
* @param AppModel $Model Model instance
* @param mixed $parentId
* @param boolean $created
* @return boolean true on success, false on failure
* @access protected
*/
Expand Down Expand Up @@ -861,11 +862,10 @@ 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 Expand Up @@ -899,6 +899,8 @@ function _setParent(&$Model, $parentId = null, $created = false) {
* @param AppModel $Model
* @param string $scope
* @param string $right
* @param int $recursive
* @param boolean $created
* @return int
* @access private
*/
Expand Down Expand Up @@ -950,6 +952,7 @@ function __getMin($Model, $scope, $left, $recursive = -1) {
* @param integer $shift
* @param string $direction
* @param array $conditions
* @param boolean $created
* @param string $field
* @access private
*/
Expand Down

0 comments on commit 90bcb75

Please sign in to comment.