Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Compatibility with CakePHP 2.4 #70

Merged
merged 1 commit into from

2 participants

@zoghal

No description provided.

@burzum burzum merged commit 6fcce31 into CakeDC:develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 7, 2013
  1. @zoghal
This page is out of date. Refresh to see the latest.
Showing with 317 additions and 344 deletions.
  1. +317 −344 Model/Behavior/BtreeBehavior.php
View
661 Model/Behavior/BtreeBehavior.php
@@ -25,41 +25,43 @@ class BtreeBehavior extends ModelBehavior {
*
* @var array
*/
- var $errors = array();
+ public $errors = array();
/**
* Defaults
- *
+ *
* @var array
*/
- var $_defaults = array(
+ protected $_defaults = array(
'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
- 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
+ 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1,'block_size' => 32768 //block size of 32k allows for up to 64k trees (MAX_INT / 2*block_size)
);
/**
+ * Used to preserve state between delete callbacks.
+ *
+ * @var array
+ */
+ protected $_deletedRow = array();
+
+/**
* Initiate Tree behavior
*
- * @param object $Model instance of model
+ * @param Model $Model instance of model
* @param array $config array of configuration settings.
* @return void
*/
- function setup(&$Model, $config = array()) {
- if (!is_array($config)) {
- $config = array('type' => $config);
- }
-
- // block size of 32k allows for up to 64k trees (MAX_INT / 2*block_size)
- if (!array_key_exists('block_size', $config)) {
- $config['block_size'] = 32768;
+ public function setup(Model $Model, $config = array()) {
+ if (isset($config[0])) {
+ $config['type'] = $config[0];
+ unset($config[0]);
}
-
$settings = array_merge($this->_defaults, $config);
if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) {
$data = $Model->getAssociated($settings['scope']);
- $parent =& $Model->{$settings['scope']};
- $settings['scope'] = $Model->alias . '.' . $data['foreignKey'] . ' = ' . $parent->alias . '.' . $parent->primaryKey;
+ $Parent = $Model->{$settings['scope']};
+ $settings['scope'] = $Model->escapeField($data['foreignKey']) . ' = ' . $Parent->escapeField();
$settings['recursive'] = 0;
}
$this->settings[$Model->alias] = $settings;
@@ -68,37 +70,72 @@ function setup(&$Model, $config = array()) {
/**
* After save method. Called after all saves
*
- * Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
+ * Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
* parameters to be saved.
*
- * @param AppModel $Model Model instance.
+ * @param Model $Model Model instance.
* @param boolean $created indicates whether the node just saved was created or updated
* @return boolean true on success, false on failure
*/
- function afterSave(&$Model, $created) {
+ public function afterSave(Model $Model, $created) {
extract($this->settings[$Model->alias]);
if ($created) {
if ((isset($Model->data[$Model->alias][$parent])) && $Model->data[$Model->alias][$parent]) {
return $this->_setParent($Model, $Model->data[$Model->alias][$parent], $created);
}
- } elseif ($__parentChange) {
+ } elseif ($this->settings[$Model->alias]['__parentChange']) {
$this->settings[$Model->alias]['__parentChange'] = false;
return $this->_setParent($Model, $Model->data[$Model->alias][$parent]);
}
}
/**
- * Before delete method. Called before all deletes
+ * Runs before a find() operation
+ *
+ * @param Model $Model Model using the behavior
+ * @param array $query Query parameters as set by cake
+ * @return array
+ */
+ public function beforeFind(Model $Model, $query) {
+ if ($Model->findQueryType === 'threaded' && !isset($query['parent'])) {
+ $query['parent'] = $this->settings[$Model->alias]['parent'];
+ }
+ return $query;
+ }
+
+/**
+ * Stores the record about to be deleted.
+ *
+ * This is used to delete child nodes in the afterDelete.
+ *
+ * @param Model $Model Model instance
+ * @param boolean $cascade
+ * @return boolean
+ */
+ public function beforeDelete(Model $Model, $cascade = true) {
+ extract($this->settings[$Model->alias]);
+ $data = $Model->find('first', array(
+ 'conditions' => array($Model->escapeField($Model->primaryKey) => $Model->id),
+ 'fields' => array($Model->escapeField($left), $Model->escapeField($right)),
+ 'recursive' => -1));
+ if ($data) {
+ $this->_deletedRow[$Model->alias] = current($data);
+ }
+ return true;
+ }
+
+/**
+ * After delete method.
*
* Will delete the current node and all children using the deleteAll method and sync the table
*
- * @param AppModel $Model Model instance
+ * @param Model $Model Model instance
* @return boolean true to continue, false to abort the delete
*/
- function beforeDelete(&$Model) {
+ public function afterDelete(Model $Model) {
extract($this->settings[$Model->alias]);
- list($name, $data) = array($Model->alias, $Model->read());
- $data = $data[$name];
+ $data = $this->_deletedRow[$Model->alias];
+ $this->_deletedRow[$Model->alias] = null;
if (!$data[$right] || !$data[$left]) {
return true;
@@ -109,38 +146,29 @@ function beforeDelete(&$Model) {
if (is_string($scope)) {
$scope = array($scope);
}
- $scope[]["{$Model->alias}.{$left} BETWEEN ? AND ?"] = array($data[$left] + 1, $data[$right] - 1);
+ $scope[][$Model->escapeField($left) . " BETWEEN ? AND ?"] = array($data[$left] + 1, $data[$right] - 1);
$Model->deleteAll($scope);
}
- $this->__sync($Model, $diff, '-', '> ' . $data[$right]);
+ $this->_sync($Model, $diff, '-', '> ' . $data[$right]);
return true;
}
/**
* Before save method. Called before all saves
*
- * Overriden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
+ * Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
* parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by
* this method bypassing the setParent logic.
*
- * @since 1.2
- * @param AppModel $Model Model instance
+ * @since 1.2
+ * @param Model $Model Model instance
* @return boolean true to continue, false to abort the save
*/
- function beforeSave(&$Model) {
+ public function beforeSave(Model $Model) {
extract($this->settings[$Model->alias]);
- if (isset($Model->data[$Model->alias][$Model->primaryKey])) {
- if ($Model->data[$Model->alias][$Model->primaryKey]) {
- if (!$Model->id) {
- $Model->id = $Model->data[$Model->alias][$Model->primaryKey];
- }
- }
- unset($Model->data[$Model->alias][$Model->primaryKey]);
- }
-
$this->_addToWhitelist($Model, array($left, $right));
- if (!$Model->id) {
+ if (!$Model->id || !$Model->exists()) {
if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
$parentNode = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
@@ -150,14 +178,14 @@ function beforeSave(&$Model) {
return false;
}
list($parentNode) = array_values($parentNode);
- $Model->data[$Model->alias][$left] = 0; //$parentNode[$right];
- $Model->data[$Model->alias][$right] = 0; //$parentNode[$right] + 1;
+ $Model->data[$Model->alias][$left] = 0;
+ $Model->data[$Model->alias][$right] = 0;
} else {
/*
* New Edge/Gap detection finds the first available partition of sufficient size
* instead of always positioning new trees at max(rght+1)
*/
- $edge = $this->__getPartition($Model, $scope, $recursive);
+ $edge = $this->_getPartition($Model, $scope, $recursive);
$Model->data[$Model->alias][$left] = $edge + 1;
$Model->data[$Model->alias][$right] = $edge + 2;
}
@@ -169,11 +197,15 @@ function beforeSave(&$Model) {
$Model->data[$Model->alias][$parent] = null;
$this->_addToWhitelist($Model, $parent);
} else {
- list($node) = array_values($Model->find('first', array(
+ $values = $Model->find('first', array(
'conditions' => array($scope,$Model->escapeField() => $Model->id),
'fields' => array($Model->primaryKey, $parent, $left, $right ), 'recursive' => $recursive)
- ));
+ );
+ if ($values === false) {
+ return false;
+ }
+ list($node) = array_values($values);
$parentNode = $Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
@@ -199,12 +231,13 @@ function beforeSave(&$Model) {
* If the direct parameter is set to true, only the direct children are counted (based upon the parent_id field)
* If false is passed for the id parameter, all top level nodes are counted, or all nodes are counted.
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to read or false to read all top level nodes
+ * @param Model $Model Model instance
+ * @param integer|string|boolean $id The ID of the record to read or false to read all top level nodes
* @param boolean $direct whether to count direct, or all, children
* @return integer number of child nodes
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::childCount
*/
- function childcount(&$Model, $id = null, $direct = false) {
+ public function childCount(Model $Model, $id = null, $direct = false) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
@@ -221,7 +254,7 @@ function childcount(&$Model, $id = null, $direct = false) {
if ($id === null) {
return $Model->find('count', array('conditions' => $scope));
- } elseif (isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
+ } elseif ($Model->id === $id && isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
$data = $Model->data[$Model->alias];
} else {
$data = $Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'recursive' => $recursive));
@@ -239,17 +272,18 @@ function childcount(&$Model, $id = null, $direct = false) {
* If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field)
* If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to read
+ * @param Model $Model Model instance
+ * @param integer|string $id The ID of the record to read
* @param boolean $direct whether to return only the direct, or all, children
- * @param mixed $fields Either a single string of a field name, or an array of field names
+ * @param string|array $fields Either a single string of a field name, or an array of field names
* @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
* @param integer $limit SQL LIMIT clause, for calculating items per page.
* @param integer $page Page number, for accessing paged data
* @param integer $recursive The number of levels deep to fetch associated records
* @return array Array of child nodes
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::children
*/
- function children(&$Model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
+ public function children(Model $Model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
@@ -260,14 +294,13 @@ function children(&$Model, $id = null, $direct = false, $fields = null, $order =
} elseif (!$id) {
$id = null;
}
- $name = $Model->alias;
extract($this->settings[$Model->alias]);
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
if (!$order) {
- $order = $Model->alias . '.' . $left . ' asc';
+ $order = $Model->escapeField($left) . " asc";
}
if ($direct) {
$conditions = array($scope, $Model->escapeField($parent) => $id);
@@ -277,7 +310,7 @@ function children(&$Model, $id = null, $direct = false, $fields = null, $order =
if (!$id) {
$conditions = $scope;
} else {
- $result = array_values($Model->find('first', array(
+ $result = array_values((array)$Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'fields' => array($left, $right),
'recursive' => $recursive
@@ -297,56 +330,56 @@ function children(&$Model, $id = null, $direct = false, $fields = null, $order =
/**
* A convenience method for returning a hierarchical array used for HTML select boxes
*
- * @param AppModel $Model Model instance
- * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...)
+ * @param Model $Model Model instance
+ * @param string|array $conditions SQL conditions as a string or as an array('field' =>'value',...)
* @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
* @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
* @param string $spacer The character or characters which will be repeated
* @param integer $recursive The number of levels deep to fetch associated records
* @return array An associative array of records, where the id is the key, and the display field is the value
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::generateTreeList
*/
- function generatetreelist(&$Model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = null) {
+ public function generateTreeList(Model $Model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = null) {
$overrideRecursive = $recursive;
extract($this->settings[$Model->alias]);
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
- if ($keyPath == null && $valuePath == null && $Model->hasField($Model->displayField)) {
+ $fields = null;
+ if (!$keyPath && !$valuePath && $Model->hasField($Model->displayField)) {
$fields = array($Model->primaryKey, $Model->displayField, $left, $right);
- } else {
- $fields = null;
}
-
- if ($keyPath == null) {
+ if (!$keyPath) {
$keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
}
- if ($valuePath == null) {
- $valuePath = array('{0}{1}', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
+ if (!$valuePath) {
+ $valuePath = array('%s%s', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
} elseif (is_string($valuePath)) {
- $valuePath = array('{0}{1}', '{n}.tree_prefix', $valuePath);
+ $valuePath = array('%s%s', '{n}.tree_prefix', $valuePath);
} else {
- $valuePath[0] = '{' . (count($valuePath) - 1) . '}' . $valuePath[0];
- $valuePath[] = '{n}.tree_prefix';
+ array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix');
}
- $order = $Model->alias . '.' . $left . ' asc';
+ $order = $Model->escapeField($left) . " asc";
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
$stack = array();
foreach ($results as $i => $result) {
- while ($stack && ($stack[count($stack) - 1] < $result[$Model->alias][$right])) {
+ $count = count($stack);
+ while ($stack && ($stack[$count - 1] < $result[$Model->alias][$right])) {
array_pop($stack);
+ $count--;
}
- $results[$i]['tree_prefix'] = str_repeat($spacer,count($stack));
+ $results[$i]['tree_prefix'] = str_repeat($spacer, $count);
$stack[] = $result[$Model->alias][$right];
}
if (empty($results)) {
return array();
}
- return Set::combine($results, $keyPath, $valuePath);
+ return Hash::combine($results, $keyPath, $valuePath);
}
/**
@@ -354,12 +387,14 @@ function generatetreelist(&$Model, $conditions = null, $keyPath = null, $valuePa
*
* reads the parent id and returns this node
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to read
+ * @param Model $Model Model instance
+ * @param integer|string $id The ID of the record to read
+ * @param string|array $fields
* @param integer $recursive The number of levels deep to fetch associated records
- * @return array Array of data for the parent node
+ * @return array|boolean Array of data for the parent node
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getParentNode
*/
- function getparentnode(&$Model, $id = null, $fields = null, $recursive = null) {
+ public function getParentNode(Model $Model, $id = null, $fields = null, $recursive = null) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
@@ -371,7 +406,7 @@ function getparentnode(&$Model, $id = null, $fields = null, $recursive = null) {
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
- $parentId = $Model->read($parent, $id);
+ $parentId = $Model->find('first', array('conditions' => array($Model->primaryKey => $id), 'fields' => array($parent), 'recursive' => -1));
if ($parentId) {
$parentId = $parentId[$Model->alias][$parent];
@@ -385,21 +420,18 @@ function getparentnode(&$Model, $id = null, $fields = null, $recursive = null) {
/**
* Get the path to the given node
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to read
- * @param mixed $fields Either a single string of a field name, or an array of field names
+ * @param Model $Model Model instance
+ * @param integer|string $id The ID of the record to read
+ * @param string|array $fields Either a single string of a field name, or an array of field names
* @param integer $recursive The number of levels deep to fetch associated records
* @return array Array of nodes from top most parent to current node
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getPath
*/
- function getpath(&$Model, $id = null, $fields = null, $recursive = null) {
- $cachequeries = $Model->cacheQueries;
- $Model->cacheQueries = false;
-
+ public function getPath(Model $Model, $id = null, $fields = null, $recursive = null) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
$overrideRecursive = $recursive;
-
if (empty ($id)) {
$id = $Model->id;
}
@@ -407,51 +439,17 @@ function getpath(&$Model, $id = null, $fields = null, $recursive = null) {
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
-
- $result = $Model->find('first', array(
- 'conditions' => array($Model->escapeField() => $id),
- 'fields' => am($fields, array($left, $right, $parent, $Model->primaryKey)),
- 'recursive' => $recursive));
-
+ $result = $Model->find('first', array('conditions' => array($Model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => $recursive));
if ($result) {
- if($result[$Model->alias][$left] == 0 && $result[$Model->alias][$right] == 0) return array($result);
$result = array_values($result);
} else {
return null;
}
$item = $result[0];
-
- $results = array();
- if($item[$left] < $item[$right]) {
- $results = $Model->find('all', array(
- 'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
- 'fields' => am($fields, $left, $right, $Model->primaryKey, $parent), 'order' => array($Model->escapeField($left) => 'asc'), 'recursive' => $recursive
- ));
-
- for($i = 0; $i < count($results); $i++) {
- for($j = $i+1; $j < count($results); $j++) {
- if($results[$i][$Model->alias][$Model->primaryKey] == $results[$j][$Model->alias][$parent]) break;
- }
- $results = array();
- break;
- }
- }
-
- $orig_id = $Model->id;
- if(empty($results)) {
- array_unshift($results, array($Model->alias => $item));
- $cur_id = $item[$Model->primaryKey];
- while($pNode = $this->getparentnode($Model, $cur_id, am($fields, $left, $right, $parent, $Model->primaryKey), $recursive)) {
- array_unshift($results, $pNode);
- if($pNode[$Model->alias][$parent] == null) {
- break;
- }
- $cur_id = $pNode[$Model->alias][$Model->primaryKey];
- }
- }
- $Model->id = $orig_id;
-
- $Model->cacheQueries = $cachequeries;
+ $results = $Model->find('all', array(
+ 'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
+ 'fields' => $fields, 'order' => array($Model->escapeField($left) => 'asc'), 'recursive' => $recursive
+ ));
return $results;
}
@@ -460,12 +458,13 @@ function getpath(&$Model, $id = null, $fields = null, $recursive = null) {
*
* If the node is the last child, or is a top level node with no subsequent node this method will return false
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to move
- * @param mixed $number how many places to move the node or true to move to last position
+ * @param Model $Model Model instance
+ * @param integer|string $id The ID of the record to move
+ * @param integer|boolean $number how many places to move the node or true to move to last position
* @return boolean true on success, false on failure
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveDown
*/
- function movedown(&$Model, $id = null, $number = 1) {
+ public function moveDown(Model $Model, $id = null, $number = 1) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
@@ -494,15 +493,14 @@ function movedown(&$Model, $id = null, $number = 1) {
'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive)
);
if ($nextNode) {
- list($nextNode)= array_values($nextNode);
+ list($nextNode) = array_values($nextNode);
} else {
return false;
}
-
- $edge = $this->__getMax($Model, $scope, $right, $recursive);
- $this->__sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
- $this->__sync($Model, $nextNode[$left] - $node[$left], '-', 'BETWEEN ' . $nextNode[$left] . ' AND ' . $nextNode[$right]);
- $this->__sync($Model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', '> ' . $edge);
+ $edge = $this->_getMax($Model, $scope, $right, $recursive);
+ $this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
+ $this->_sync($Model, $nextNode[$left] - $node[$left], '-', 'BETWEEN ' . $nextNode[$left] . ' AND ' . $nextNode[$right]);
+ $this->_sync($Model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', '> ' . $edge);
if (is_int($number)) {
$number--;
@@ -518,12 +516,13 @@ function movedown(&$Model, $id = null, $number = 1) {
*
* If the node is the first child, or is a top level node with no previous node this method will return false
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to move
- * @param mixed $number how many places to move the node, or true to move to first position
+ * @param Model $Model Model instance
+ * @param integer|string $id The ID of the record to move
+ * @param integer|boolean $number how many places to move the node, or true to move to first position
* @return boolean true on success, false on failure
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveUp
*/
- function moveup(&$Model, $id = null, $number = 1) {
+ public function moveUp(Model $Model, $id = null, $number = 1) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
@@ -558,11 +557,10 @@ function moveup(&$Model, $id = null, $number = 1) {
} else {
return false;
}
-
- $edge = $this->__getMax($Model, $scope, $right, $recursive);
- $this->__sync($Model, $edge - $previousNode[$left] + 1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
- $this->__sync($Model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' .$node[$left] . ' AND ' . $node[$right]);
- $this->__sync($Model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
+ $edge = $this->_getMax($Model, $scope, $right, $recursive);
+ $this->_sync($Model, $edge - $previousNode[$left] + 1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
+ $this->_sync($Model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
+ $this->_sync($Model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
if (is_int($number)) {
$number--;
}
@@ -580,25 +578,22 @@ function moveup(&$Model, $id = null, $number = 1) {
* 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction
* parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present.
*
- * @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
- * @param AppModel $Model Model instance
+ * @param Model $Model Model instance
* @param string $mode parent or tree
- * @param mixed $missingParentAction 'return' to do nothing and return, 'delete' to
+ * @param string|integer $missingParentAction 'return' to do nothing and return, 'delete' to
* delete, or the id of the parent to set as the parent_id
* @return boolean true on success, false on failure
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::recover
*/
- function recover(&$Model, $mode = 'parent', $missingParentAction = null) {
- $cachequeries = $Model->cacheQueries;
- $Model->cacheQueries = false;
-
+ public function recover(Model $Model, $mode = 'parent', $missingParentAction = null) {
if (is_array($mode)) {
extract (array_merge(array('mode' => 'parent'), $mode));
}
extract($this->settings[$Model->alias]);
$Model->recursive = $recursive;
- if ($mode == 'parent') {
+ if ($mode === 'parent') {
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
- 'className' => $Model->alias,
+ 'className' => $Model->name,
'foreignKey' => $parent,
'fields' => array($Model->primaryKey, $left, $right, $parent),
))));
@@ -610,62 +605,48 @@ function recover(&$Model, $mode = 'parent', $missingParentAction = null) {
));
$Model->unbindModel(array('belongsTo' => array('VerifyParent')));
if ($missingParents) {
- if ($missingParentAction == 'return') {
+ 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') {
- $Model->deleteAll(array($Model->primaryKey => array_flip($missingParents)));
+ } elseif ($missingParentAction === 'delete') {
+ $Model->deleteAll(array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)), false);
} else {
- $Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
+ $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, $parent), 'order' => $left)) as $array) {
+ foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) {
$Model->id = $array[$Model->alias][$Model->primaryKey];
// Enforce Partitioning
- if($array[$Model->alias][$parent] == null) {
+ if ($array[$Model->alias][$parent] == null) {
$lft = $this->__getPartition($Model, $scope, $recursive);
$rght = $lft + 1;
} else {
$lft = $count++;
$rght = $count++;
}
- $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false));
+ $Model->create(false);
+ $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false, 'validate' => false));
}
- $nodePage = 0;
- $count = 0;
-
- while ($nodes = $Model->find('all', array(
- 'conditions' => am($scope, array("NOT" => array($parent => NULL))),
- 'fields' => array($Model->primaryKey, $parent),
- 'limit' => 1024,
- 'page' => $nodePage++,
- 'order' => $Model->primaryKey,
- 'recursive' => 0))) {
- foreach($nodes as $node) {
- $Model->create();
- $Model->id = $node[$Model->alias][$Model->primaryKey];
- $this->_setParent($Model, $node[$Model->alias][$parent]);
- }
+ 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]);
}
} else {
- $db =& ConnectionManager::getDataSource($Model->useDbConfig);
+ $db = ConnectionManager::getDataSource($Model->useDbConfig);
foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
- $path = $this->getpath($Model, $array[$Model->alias][$Model->primaryKey]);
- if ($path == null || count($path) < 2) {
+ $path = $this->getPath($Model, $array[$Model->alias][$Model->primaryKey]);
$parentId = null;
- } else {
+ if (count($path) > 1) {
$parentId = $path[count($path) - 2][$Model->alias][$Model->primaryKey];
}
$Model->updateAll(array($parent => $db->value($parentId, $parent)), array($Model->escapeField() => $array[$Model->alias][$Model->primaryKey]));
}
}
-
- $Model->cacheQueries = $cachequeries;
return true;
}
@@ -680,15 +661,16 @@ function recover(&$Model, $mode = 'parent', $missingParentAction = null) {
* Options:
*
* - 'id' id of record to use as top node for reordering
- * - 'field' Which field to use in reordeing defaults to displayField
+ * - 'field' Which field to use in reordering defaults to displayField
* - 'order' Direction to order either DESC or ASC (defaults to ASC)
* - 'verify' Whether or not to verify the tree before reorder. defaults to true.
*
- * @param AppModel $Model Model instance
+ * @param Model $Model Model instance
* @param array $options array of options to use in reordering.
* @return boolean true on success, false on failure
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::reorder
*/
- function reorder(&$Model, $options = array()) {
+ public function reorder(Model $Model, $options = array()) {
$options = array_merge(array('id' => null, 'field' => $Model->displayField, 'order' => 'ASC', 'verify' => true), $options);
extract($options);
if ($verify && !$this->verify($Model)) {
@@ -700,6 +682,8 @@ function reorder(&$Model, $options = array()) {
$sort = $field . ' ' . $order;
$nodes = $this->children($Model, $id, true, $fields, $sort, null, null, $recursive);
+ $cacheQueries = $Model->cacheQueries;
+ $Model->cacheQueries = false;
if ($nodes) {
foreach ($nodes as $node) {
$id = $node[$Model->alias][$Model->primaryKey];
@@ -709,6 +693,7 @@ function reorder(&$Model, $options = array()) {
}
}
}
+ $Model->cacheQueries = $cacheQueries;
return true;
}
@@ -718,12 +703,13 @@ function reorder(&$Model, $options = array()) {
* If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted
* after the children are reparented.
*
- * @param AppModel $Model Model instance
- * @param mixed $id The ID of the record to remove
+ * @param Model $Model Model instance
+ * @param integer|string $id The ID of the record to remove
* @param boolean $delete whether to delete the node after reparenting children (if any)
* @return boolean true on success, false on failure
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::removeFromTree
*/
- function removefromtree(&$Model, $id = null, $delete = false) {
+ public function removeFromTree(Model $Model, $id = null, $delete = false) {
if (is_array($id)) {
extract (array_merge(array('id' => null), $id));
}
@@ -752,27 +738,25 @@ function removefromtree(&$Model, $id = null, $delete = false) {
$parentNode[$right] = $node[$right] + 1;
}
- $db =& ConnectionManager::getDataSource($Model->useDbConfig);
- $Model->updateAll(array($parent => $db->value($node[$parent], $parent)), array($parent => $node[$Model->primaryKey]));
-
+ $db = ConnectionManager::getDataSource($Model->useDbConfig);
+ $Model->updateAll(
+ array($parent => $db->value($node[$parent], $parent)),
+ array($Model->escapeField($parent) => $node[$Model->primaryKey])
+ );
+ $this->_sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
+ $this->_sync($Model, 2, '-', '> ' . ($node[$right]));
$Model->id = $id;
- $this->__sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
- $this->__sync($Model, 2, '-', '> ' . ($node[$right]));
if ($delete) {
- $sub_tree = $Model->find('list', array(
- 'conditions' => array(
- $left . ' >= ' . $node[$left],
- $right .' <= ' . $node[$right],
- ),
- 'fields' => array('id', 'parent_id')
- ));
-
- foreach($sub_tree as $sChild => $sParent) {
- $Model->del($sChild);
- }
-
- return true;
+ $Model->updateAll(
+ array(
+ $Model->escapeField($left) => 0,
+ $Model->escapeField($right) => 0,
+ $Model->escapeField($parent) => null
+ ),
+ array($Model->escapeField() => $id)
+ );
+ return $Model->delete($id);
} else {
/*
* this could feasibly create a lot of new trees.. worst case
@@ -783,10 +767,9 @@ function removefromtree(&$Model, $id = null, $delete = false) {
$edge = $edge - 2;
}
$Model->id = $id;
-
return $Model->save(
array($left => $edge, $right => $edge + 1, $parent => null),
- array('callbacks' => false)
+ array('callbacks' => false, 'validate' => false)
);
}
}
@@ -796,19 +779,17 @@ function removefromtree(&$Model, $id = null, $delete = false) {
*
* Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message)
*
- * @param AppModel $Model Model instance
+ * @param Model $Model Model instance
* @return mixed true if the tree is valid or empty, otherwise an array of (error type [index, node],
* [incorrect left/right index,node id], message)
+ * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::verify
*/
- function verify(&$Model) {
+ public function verify(Model $Model) {
extract($this->settings[$Model->alias]);
- $cachequeries = $Model->cacheQueries;
- $Model->cacheQueries = false;
-
if (!$Model->find('count', array('conditions' => $scope))) {
return true;
}
- $errors = array();
+ $errors = array();
$errors = am($errors, $this->__verifyContinuity($Model));
$errors = am($errors, $this->__verifySanity($Model));
@@ -821,7 +802,7 @@ function verify(&$Model) {
return true;
}
- function __verifyRelations(&$Model) {
+ function __verifyRelations(Model $Model) {
extract($this->settings[$Model->alias]);
$errors = array();
@@ -832,21 +813,21 @@ function __verifyRelations(&$Model) {
))));
$nodePage = 0;
$nodes = $Model->find('all', array(
- 'conditions' => am($scope),
- 'fields' => array(
- $Model->primaryKey,
- $left,
- $right,
- $parent,
- 'VerifyParent.' . $Model->primaryKey,
- 'VerifyParent.' . $left,
- 'VerifyParent.' . $right
- ),
- 'limit' => 1024,
- 'page' => $nodePage++,
- 'order' => $Model->escapeField($Model->primaryKey),
- 'recursive' => 1
- ));
+ 'conditions' => am($scope),
+ 'fields' => array(
+ $Model->primaryKey,
+ $left,
+ $right,
+ $parent,
+ 'VerifyParent.' . $Model->primaryKey,
+ 'VerifyParent.' . $left,
+ 'VerifyParent.' . $right
+ ),
+ 'limit' => 1024,
+ 'page' => $nodePage++,
+ 'order' => $Model->escapeField($Model->primaryKey),
+ 'recursive' => 1
+ ));
while($nodes) {
foreach ($nodes as $instance) {
@@ -872,31 +853,31 @@ function __verifyRelations(&$Model) {
}
}
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
- 'className' => $Model->alias,
- 'foreignKey' => $parent,
- 'fields' => array($Model->primaryKey, $left, $right, $parent)
- ))));
+ 'className' => $Model->alias,
+ 'foreignKey' => $parent,
+ 'fields' => array($Model->primaryKey, $left, $right, $parent)
+ ))));
$nodes = $Model->find('all', array(
- 'conditions' => am($scope),
- 'fields' => array(
- $Model->primaryKey,
- $left,
- $right,
- $parent,
- 'VerifyParent.' . $Model->primaryKey,
- 'VerifyParent.' . $left,
- 'VerifyParent.' . $right
- ),
- 'limit' => 1024,
- 'page' => $nodePage++,
- 'order' => $Model->escapeField($Model->primaryKey),
- 'recursive' => 1
- ));
+ 'conditions' => am($scope),
+ 'fields' => array(
+ $Model->primaryKey,
+ $left,
+ $right,
+ $parent,
+ 'VerifyParent.' . $Model->primaryKey,
+ 'VerifyParent.' . $left,
+ 'VerifyParent.' . $right
+ ),
+ 'limit' => 1024,
+ 'page' => $nodePage++,
+ 'order' => $Model->escapeField($Model->primaryKey),
+ 'recursive' => 1
+ ));
}
return $errors;
}
- function __verifySanity(&$Model) {
+ function __verifySanity(Model $Model) {
extract($this->settings[$Model->alias]);
$errors = array();
$node = $Model->find('first', array('conditions' => array($scope, $Model->escapeField($right) . '< ' . $Model->escapeField($left)), 'recursive' => 0));
@@ -906,7 +887,7 @@ function __verifySanity(&$Model) {
return $errors;
}
- function __verifyContinuity(&$Model) {
+ function __verifyContinuity(Model $Model) {
extract($this->settings[$Model->alias]);
$errors = array();
$nodes = $this->children($Model, null, true, array($left, $right));
@@ -931,6 +912,8 @@ function __verifyContinuity(&$Model) {
return $errors;
}
+
+
/**
* Sets the parent of the given node
*
@@ -938,74 +921,85 @@ function __verifyContinuity(&$Model) {
* of recovering a corrupted table, or creating new nodes. Otherwise it should always be false. In reality this
* method could be private, since calling save with parent_id set also calls setParent
*
- * @param AppModel $Model Model instance
- * @param mixed $parentId
+ * @param Model $Model Model instance
+ * @param integer|string $parentId
+ * @param boolean $created
* @return boolean true on success, false on failure
*/
- function _setParent(&$Model, $parentId = null, $created = false) {
+ protected function _setParent(Model $Model, $parentId = null, $created = false) {
extract($this->settings[$Model->alias]);
- $cachequeries = $Model->cacheQueries;
- $Model->cacheQueries = false;
-
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->id),
'fields' => array($Model->primaryKey, $parent, $left, $right),
'recursive' => $recursive
)));
+ $edge = $this->_getMax($Model, $scope, $right, $recursive, $created);
- if (empty ($parentId)) {
- $edge = $this->__getMax($Model, $scope, $right, $recursive, $created);
- $this->__sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
- $this->__sync($Model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left], $created);
+ if (empty($parentId)) {
+ $this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
+ $this->_sync($Model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left], $created);
} else {
- $parentNode = array_values($Model->find('first', array(
- 'conditions' => array($scope, $Model->escapeField() => $parentId),
- 'fields' => array($Model->primaryKey, $left, $right),
- 'recursive' => $recursive
- )));
+ $values = $Model->find('first', array(
+ 'conditions' => array($scope, $Model->escapeField() => $parentId),
+ 'fields' => array($Model->primaryKey, $left, $right),
+ 'recursive' => $recursive
+ ));
+
+ if ($values === false) {
+ return false;
+ }
+ $parentNode = array_values($values);
if (empty($parentNode) || empty($parentNode[0])) {
return false;
- } $parentNode = $parentNode[0];
-
- $edge = $this->__getMax($Model, "{$Model->alias}.{$left} <= " . $parentNode[$left], $right, $recursive, $created);
+ }
+ $parentNode = $parentNode[0];
if (($Model->id == $parentId)) {
return false;
} elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
return false;
}
-
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),
array('validate' => false, 'callbacks' => false)
);
- $this->__sync($Model, 2, '+', '>= ' . $parentNode[$right], $created);
$Model->data = $result;
} else {
$diff = $node[$right] - $node[$left];
// increment position values of new family of retargeted subtree
- $this->__sync($Model, $diff + 1, '+', '>= ' . $parentNode[$right], $created);
+ $this->_sync($Model, $diff + 1, '+', '>= ' . $parentNode[$right], $created);
// relocate target node under parent
- $this->__sync($Model, $parentNode[$right] - $node[$left], '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
+ $this->_sync($Model, $parentNode[$right] - $node[$left], '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
// reduce values in former position
- $nodeTreeMax = $this->__getMax($Model, "{$Model->alias}.{$left} <= " . $node[$left], $right, $recursive, $created);
- $this->__sync($Model, $diff + 1, '-', 'BETWEEN ' . $node[$right] . ' AND ' . $nodeTreeMax, $created);
+ $nodeTreeMax = $this->_getMax($Model, "{$Model->alias}.{$left} <= " . $node[$left], $right, $recursive, $created);
+ $this->_sync($Model, $diff + 1, '-', 'BETWEEN ' . $node[$right] . ' AND ' . $nodeTreeMax, $created);
}
}
- $Model->cacheQueries = $cachequeries;
return true;
}
+
+
+
+
+
+
+
+
+
+
+
// NEW EDGE/GAP DETECTION
- function __getPartition($Model, $scope, $recursive = -1, $created = false) {
+ protected function _getPartition(Model $Model, $scope, $recursive = -1, $created = false) {
extract($this->settings[$Model->alias]);
- $db =& ConnectionManager::getDataSource($Model->useDbConfig);
+ $db = ConnectionManager::getDataSource($Model->useDbConfig);
if ($created) {
if (is_string($scope)) {
$scope .= " AND {$Model->alias}.{$Model->primaryKey} <> ";
@@ -1021,18 +1015,21 @@ function __getPartition($Model, $scope, $recursive = -1, $created = false) {
while($part_lft < ($count*2)) $part_lft = $part_lft + ($block_size*2);
$edge_pairs = $Model->find('list', array(
- 'conditions' => array($scope, 'parent_id' => null),
- 'fields' => array($left, $right),
- 'order' => "lft ASC",
- 'recursive' => $recursive));
+ 'conditions' => array($scope, 'parent_id' => null),
+ 'fields' => array($left, $right),
+ 'order' => "lft ASC",
+ 'recursive' => $recursive)
+ );
reset($edge_pairs);
while(list($lft, $rght) = each($edge_pairs)) {
- if(!$lft || $lft < $part_lft) continue;
+ if(!$lft || $lft < $part_lft) {
+ continue;
+ }
if(($lft - $part_lft) > $block_size) {
break;
} else {
- $part_lft += $block_size*2;
+ $part_lft += $block_size * 2;
}
}
@@ -1042,49 +1039,51 @@ function __getPartition($Model, $scope, $recursive = -1, $created = false) {
/**
* get the maximum index value in the table.
*
- * @param AppModel $Model
+ * @param Model $Model
* @param string $scope
* @param string $right
- * @return int
+ * @param integer $recursive
+ * @param boolean $created
+ * @return integer
*/
- function __getMax($Model, $scope, $right, $recursive = -1, $created = false) {
- $db =& ConnectionManager::getDataSource($Model->useDbConfig);
+ protected function _getMax(Model $Model, $scope, $right, $recursive = -1, $created = false) {
+ $db = ConnectionManager::getDataSource($Model->useDbConfig);
if ($created) {
if (is_string($scope)) {
- $scope .= " AND {$Model->alias}.{$Model->primaryKey} <> ";
+ $scope .= " AND " . $Model->escapeField() . " <> ";
$scope .= $db->value($Model->id, $Model->getColumnType($Model->primaryKey));
} else {
$scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
}
}
- $edge = $Model->find('first', array(
+ $name = $Model->escapeField($right);
+ list($edge) = array_values($Model->find('first', array(
'conditions' => $scope,
- 'fields' => $right,
- 'order' => array($right . ' DESC'),
- 'recursive' => $recursive
- ));
-
- if(!empty($edge)) list($edge) = array_values($edge);
-
+ 'fields' => $db->calculate($Model, 'max', array($name, $right)),
+ 'recursive' => $recursive,
+ 'callbacks' => false
+ )));
return (empty($edge[$right])) ? 0 : $edge[$right];
}
/**
* get the minimum index value in the table.
*
- * @param AppModel $Model
+ * @param Model $Model
* @param string $scope
- * @param string $right
- * @return int
+ * @param string $left
+ * @param integer $recursive
+ * @return integer
*/
- function __getMin($Model, $scope, $left, $recursive = -1) {
- $db =& ConnectionManager::getDataSource($Model->useDbConfig);
+ protected function _getMin(Model $Model, $scope, $left, $recursive = -1) {
+ $db = ConnectionManager::getDataSource($Model->useDbConfig);
+ $name = $Model->escapeField($left);
list($edge) = array_values($Model->find('first', array(
'conditions' => $scope,
- 'fields' => $db->calculate($Model, 'min', array($left)),
- 'recursive' => $recursive
+ 'fields' => $db->calculate($Model, 'min', array($name, $left)),
+ 'recursive' => $recursive,
+ 'callbacks' => false
)));
-
return (empty($edge[$left])) ? 0 : $edge[$left];
}
@@ -1093,59 +1092,33 @@ function __getMin($Model, $scope, $left, $recursive = -1) {
*
* Handles table sync operations, Taking account of the behavior scope.
*
- * @param AppModel $Model
+ * @param Model $Model
* @param integer $shift
- * @param string $direction
+ * @param string $dir
* @param array $conditions
+ * @param boolean $created
* @param string $field
+ * @return void
*/
- function __sync(&$Model, $shift, $dir = '+', $conditions = array(), $created = false, $field = 'both') {
+ protected function _sync(Model $Model, $shift, $dir = '+', $conditions = array(), $created = false, $field = 'both') {
$ModelRecursive = $Model->recursive;
- $cachequeries = $Model->cacheQueries;
extract($this->settings[$Model->alias]);
$Model->recursive = $recursive;
- $Model->cacheQueries = false;
- if ($field == 'both') {
- $this->__sync($Model, $shift, $dir, $conditions, $created, $left);
+ if ($field === 'both') {
+ $this->_sync($Model, $shift, $dir, $conditions, $created, $left);
$field = $right;
}
if (is_string($conditions)) {
- $conditions = array("{$Model->alias}.{$field} {$conditions}");
+ $conditions = array($Model->escapeField($field) . " {$conditions}");
}
- if (($scope != '1 = 1' && $scope !== true) && $scope) {
+ if (($scope !== '1 = 1' && $scope !== true) && $scope) {
$conditions[] = $scope;
}
-
- $path = $this->getpath($Model, $Model->id);
-
- $max = $min = 0;
- if($path) {
- if(count($path) == 1) {
- if($path[0][$Model->alias][$left] != 0 || $path[0][$Model->alias][$right] != 0) {
- $conditions[] = array($Model->primaryKey => $Model->id);
- } else {
- $min = $path[0][$Model->alias][$left];
- $max = $path[0][$Model->alias][$right];
- }
- } else {
- $min = $this->__getMax($Model, $scope, $right, $recursive);
-
- foreach($path as $node) {
- if($node[$Model->alias][$left] < $min) $min = $node[$Model->alias][$left];
- if($node[$Model->alias][$right] > $max) $max = $node[$Model->alias][$right];
- }
- }
- }
- $conditions[] = $Model->escapeField($left) . ' >= ' . $min;
- $conditions[] = $Model->escapeField($right) . ' <= ' . $max;
-
if ($created) {
- $conditions['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
+ $conditions['NOT'][$Model->escapeField()] = $Model->id;
}
-
- $Model->updateAll(array($Model->alias . '.' . $field => $Model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
+ $Model->updateAll(array($Model->escapeField($field) => $Model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
$Model->recursive = $ModelRecursive;
- $Model->cacheQueries = $cachequeries;
}
-}
+}
Something went wrong with that request. Please try again.