Permalink
Browse files

make tree recovery much, much simpler

  • Loading branch information...
1 parent c3222f7 commit 8ea021226537205bba4283218241f6da25b41292 Andy Dawson committed Mar 24, 2013
Showing with 57 additions and 13 deletions.
  1. +57 −13 lib/Cake/Model/Behavior/TreeBehavior.php
@@ -627,19 +627,8 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
$Model->updateAll(array($Model->escapeField($parent) => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
}
}
- $count = 1;
- foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) {
- $lft = $count++;
- $rght = $count++;
- $Model->create(false);
- $Model->id = $array[$Model->alias][$Model->primaryKey];
- $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false, 'validate' => false));
- }
- foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
- $Model->create(false);
- $Model->id = $array[$Model->alias][$Model->primaryKey];
- $this->_setParent($Model, $array[$Model->alias][$parent]);
- }
+
+ $this->_recoverByParentId($Model);
} else {
$db = ConnectionManager::getDataSource($Model->useDbConfig);
foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
@@ -655,6 +644,61 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
}
/**
+ * _recoverByParentId
+ *
+ * Recursive helper function used by recover
+ *
+ * @param Model $Model
+ * @param integer $counter
+ * @param mixed $parentId
+ * @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
+ ),
+ 'fields' => array($Model->primaryKey),
+ 'page' => 1,
+ 'limit' => 100,
+ 'order' => array($Model->primaryKey)
+ );
+
+ $scope = $this->settings[$Model->alias]['scope'];
+ if ($scope && ($scope !== '1 = 1' && $scope !== true)) {
+ $conditions[] = $scope;
@ceeram

ceeram Sep 17, 2013

Member

$conditions is unused, $scope should be added to $params['conditions'] ? see Lighthouse ticket 4062

@dereuromark

dereuromark Sep 17, 2013

Member

There is probably no test for this method with scope yet.

+ }
+
+ while ($rows = $Model->find('all', $params)) {
+ foreach ($rows as $row) {
+ $counter = $this->_recoverByParentId($Model, $counter, $row[$Model->alias][$Model->primaryKey]);
+ }
+ $params['page']++;
+ if (count($rows) !== $params['limit']) {
+ break;
+ }
+ }
+
+ if (!is_null($parentId)) {
+ $Model->updateAll(
+ array($this->settings[$Model->alias]['right'] => $counter),
+ array($Model->escapeField() => $parentId)
+ );
+ $counter++;
+ }
+
+ return $counter;
+ }
+
+/**
* Reorder method.
*
* Reorders the nodes (and child nodes) of the tree according to the field and direction specified in the parameters.

0 comments on commit 8ea0212

Please sign in to comment.