Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Even less queries

  • Loading branch information...
commit e92e3df2fbf996e7058787057ac9734a39f50737 1 parent 8ea0212
@AD7six AD7six authored
Showing with 29 additions and 13 deletions.
  1. +29 −13 lib/Cake/Model/Behavior/TreeBehavior.php
View
42 lib/Cake/Model/Behavior/TreeBehavior.php
@@ -654,14 +654,6 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
* @return integer $counter
*/
protected function _recoverByParentId(Model $Model, $counter = 1, $parentId = null) {
- if (!is_null($parentId)) {
- $Model->updateAll(
- array($this->settings[$Model->alias]['left'] => $counter),
- array($Model->escapeField() => $parentId)
- );
- $counter++;
- }
-
$params = array(
'conditions' => array(
$this->settings[$Model->alias]['parent'] => $parentId
@@ -677,17 +669,41 @@ protected function _recoverByParentId(Model $Model, $counter = 1, $parentId = nu
$conditions[] = $scope;
}
- while ($rows = $Model->find('all', $params)) {
- foreach ($rows as $row) {
+ $children = $Model->find('all', $params);
+ $hasChildren = (bool)$children;
+
+ if (!is_null($parentId)) {
+ if($hasChildren) {
+ $Model->updateAll(
+ array($this->settings[$Model->alias]['left'] => $counter),
+ array($Model->escapeField() => $parentId)
+ );
+ $counter++;
+ } else {
+ $Model->updateAll(
+ array(
+ $this->settings[$Model->alias]['left'] => $counter,
+ $this->settings[$Model->alias]['right'] => $counter + 1
+ ),
+ array($Model->escapeField() => $parentId)
+ );
+ $counter += 2;
+ }
+ }
+
+ while ($children) {
+ foreach ($children as $row) {
$counter = $this->_recoverByParentId($Model, $counter, $row[$Model->alias][$Model->primaryKey]);
}
- $params['page']++;
- if (count($rows) !== $params['limit']) {
+
+ if (count($children) !== $params['limit']) {
break;
}
+ $params['page']++;
+ $children = $Model->find('all', $params);
}
- if (!is_null($parentId)) {
+ if (!is_null($parentId) && $hasChildren) {
$Model->updateAll(
array($this->settings[$Model->alias]['right'] => $counter),
array($Model->escapeField() => $parentId)
Please sign in to comment.
Something went wrong with that request. Please try again.