mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Fixes #3065, Ambiguous columns in Tree->children()
Removed notices from TreeBehavior git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5780 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
a1958bf820
commit
5c06492be7
2 changed files with 42 additions and 19 deletions
|
@ -88,6 +88,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
*/
|
||||
function beforeDelete(&$model) {
|
||||
extract($this->settings[$model->name]);
|
||||
|
||||
if (!$enabled) {
|
||||
return true;
|
||||
}
|
||||
|
@ -100,6 +101,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
return true;
|
||||
}
|
||||
$diff = $data[$right] - $data[$left] + 1;
|
||||
|
||||
if ($diff > 2) {
|
||||
$constraint = $scope . ' AND ' . $left . ' BETWEEN ' . ($data[$left] + 1) . ' AND ' . ($data[$right] - 1);
|
||||
$model->deleteAll($constraint);
|
||||
|
@ -120,6 +122,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
*/
|
||||
function beforeSave(&$model) {
|
||||
extract($this->settings[$model->name]);
|
||||
|
||||
if (!$enabled) {
|
||||
return true;
|
||||
}
|
||||
|
@ -140,8 +143,8 @@ class TreeBehavior extends ModelBehavior {
|
|||
} else {
|
||||
$parentNode = $model->find(array($scope, $model->escapeField() => $model->data[$model->name][$parent]),
|
||||
array($model->primaryKey), null, -1);
|
||||
|
||||
if (!$parentNode) {
|
||||
trigger_error(__('Trying to save a node under a none-existant node in TreeBehavior::beforeSave', E_USER_WARNING));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -155,15 +158,12 @@ class TreeBehavior extends ModelBehavior {
|
|||
$parentNode = $model->find(array($scope, $model->escapeField() => $model->data[$model->name][$parent]),
|
||||
array($model->primaryKey, $left, $right), null, -1);
|
||||
if (!$parentNode) {
|
||||
trigger_error(__('Trying to save a node under a none-existant node in TreeBehavior::beforeSave', E_USER_WARNING));
|
||||
return false;
|
||||
} else {
|
||||
list($parentNode) = array_values($parentNode);
|
||||
if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
|
||||
trigger_error(__('Trying to save a node under itself in TreeBehavior::beforeSave', E_USER_WARNING));
|
||||
return false;
|
||||
} elseif ($node[$model->primaryKey] == $parentNode[$model->primaryKey]) {
|
||||
trigger_error(__('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave', E_USER_WARNING));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
extract($this->settings[$model->name]);
|
||||
|
||||
if ($direct) {
|
||||
return $model->findCount(array($scope, $parent => $id));
|
||||
return $model->findCount(array($scope, $model->escapeField($parent) => $id));
|
||||
} else {
|
||||
if ($id === null) {
|
||||
return $model->findCount($scope);
|
||||
|
@ -235,13 +235,13 @@ class TreeBehavior extends ModelBehavior {
|
|||
$order = $model->name . '.' . $left . ' asc';
|
||||
}
|
||||
if ($direct) {
|
||||
return $model->findAll(array($scope, $parent => $id), $fields, $order, $limit, $page, $recursive);
|
||||
return $model->findAll(array($scope, $model->escapeField($parent) => $id), $fields, $order, $limit, $page, $recursive);
|
||||
} else {
|
||||
if (!$id) {
|
||||
$constraint = $scope;
|
||||
} else {
|
||||
@list($item) = array_values($model->find(array($scope,$model->escapeField() => $id), array($left, $right), null, -1));
|
||||
$constraint = array($scope, $right => '< ' . $item[$right], $left => '> ' . $item[$left]);
|
||||
$constraint = array($scope, $model->escapeField($right) => '< ' . $item[$right], $model->escapeField($left) => '> ' . $item[$left]);
|
||||
}
|
||||
return $model->findAll($constraint, $fields, $order, $limit, $page, $recursive);
|
||||
}
|
||||
|
@ -336,7 +336,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
|
||||
if ($parentId) {
|
||||
$parentId = $parentId[$model->name][$parent];
|
||||
$parent = $model->find(array($model->name . '.' . $model->primaryKey => $parentId), $fields, null, $recursive);
|
||||
$parent = $model->find(array($model->escapeField() => $parentId), $fields, null, $recursive);
|
||||
|
||||
return $parent;
|
||||
} else {
|
||||
|
@ -358,7 +358,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
$id = $model->id;
|
||||
}
|
||||
extract($this->settings[$model->name]);
|
||||
@list($item) = array_values($model->find(array($model->name . '.' . $model->primaryKey => $id), array($left, $right), null, -1));
|
||||
@list($item) = array_values($model->find(array($model->escapeField() => $id), array($left, $right), null, -1));
|
||||
|
||||
if (empty ($item)) {
|
||||
return null;
|
||||
|
@ -396,7 +396,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
$nextNode = $model->find(array($scope, $left => ($node[$right] + 1)),
|
||||
$nextNode = $model->find(array($scope, $model->escapeField($left) => ($node[$right] + 1)),
|
||||
array($model->primaryKey, $left, $right), null, -1);
|
||||
if ($nextNode) {
|
||||
list($nextNode)= array_values($nextNode);
|
||||
|
@ -440,8 +440,8 @@ class TreeBehavior extends ModelBehavior {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
$previousNode = $model->find(array($scope, $right => ($node[$left] - 1)),
|
||||
array($model->primaryKey, $left, $right), null, -1);
|
||||
$previousNode = $model->find(array($scope, $model->escapeField($right) => ($node[$left] - 1)),
|
||||
array($model->primaryKey, $left, $right), null, -1);
|
||||
if ($previousNode) {
|
||||
list($previousNode) = array_values($previousNode);
|
||||
} else {
|
||||
|
@ -589,7 +589,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
$errors = array();
|
||||
|
||||
for ($i = $min; $i <= $edge; $i++) {
|
||||
$count = $model->findCount(array($scope, 'OR' => array($left => $i, $right => $i)));
|
||||
$count = $model->findCount(array($scope, 'OR' => array($model->escapeField($left) => $i, $model->escapeField($right) => $i)));
|
||||
if ($count != 1) {
|
||||
if ($count == 0) {
|
||||
$errors[] = array('index', $i, 'missing');
|
||||
|
@ -598,9 +598,9 @@ class TreeBehavior extends ModelBehavior {
|
|||
}
|
||||
}
|
||||
}
|
||||
$count = $model->findCount(array($scope, $right => '< ' . $model->escapeField($left)));
|
||||
$count = $model->findCount(array($scope, $model->escapeField($right) => '< ' . $model->escapeField($left)));
|
||||
if ($count != 0) {
|
||||
$node = $model->find(array($scope, $right => '< ' . $model->escapeField($left)));
|
||||
$node = $model->find(array($scope, $model->escapeField($right) => '< ' . $model->escapeField($left)));
|
||||
$errors[] = array('node', $node[$model->primaryKey], 'left greater than right.');
|
||||
}
|
||||
|
||||
|
@ -661,18 +661,17 @@ class TreeBehavior extends ModelBehavior {
|
|||
} else {
|
||||
list($parentNode)= array_values($model->find(array($scope, $model->escapeField() => $parentId),
|
||||
array($model->primaryKey, $left, $right), null, -1));
|
||||
|
||||
if (empty ($parentNode)) {
|
||||
trigger_error(__('Trying to move a node under a none-existant node in TreeBehavior::_setParent', true), E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
elseif (($model->id == $parentId)) {
|
||||
trigger_error(__('Trying to set a node to be the parent of itself in TreeBehavior::_setParent', E_USER_WARNING));
|
||||
return false;
|
||||
}
|
||||
elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
|
||||
trigger_error(__('Trying to move a node under itself in TreeBehavior::_setParent', E_USER_WARNING));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty ($node[$left]) && empty ($node[$right])) {
|
||||
$this->__sync($model, 2, '+', '>= ' . $parentNode[$right]);
|
||||
$model->save(array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId), false);
|
||||
|
@ -753,4 +752,4 @@ class TreeBehavior extends ModelBehavior {
|
|||
$model->updateAll(array($field => $field . ' ' . $dir . ' ' . $shift), $conditions);
|
||||
}
|
||||
}
|
||||
?>
|
||||
?>
|
||||
|
|
|
@ -722,6 +722,30 @@ class NumberTreeCase extends CakeTestCase {
|
|||
$this->assertIdentical($result, $expects);
|
||||
}
|
||||
|
||||
function testNoAmbiguousColumn() {
|
||||
$this->NumberTree =& new NumberTree();
|
||||
$this->NumberTree->bindModel(array('belongsTo' => array('Dummy' =>
|
||||
array('className' => 'NumberTree', 'foreignKey' => 'parent_id', 'conditions' => array('Dummy.id' => null)))), false);
|
||||
$this->NumberTree->__initialize(2, 2);
|
||||
|
||||
$data = $this->NumberTree->find(array('NumberTree.name' => '1. Root'));
|
||||
$this->NumberTree->id= $data['NumberTree']['id'];
|
||||
|
||||
$direct = $this->NumberTree->children(null, true, array('id', 'name', 'parent_id', 'lft', 'rght'), null, null, null, 1);
|
||||
$expects = array(array('NumberTree' => array('id' => 2, 'name' => '1.1', 'parent_id' => 1, 'lft' => 2, 'rght' => 7)),
|
||||
array('NumberTree' => array('id' => 5, 'name' => '1.2', 'parent_id' => 1, 'lft' => 8, 'rght' => 13)));
|
||||
$this->assertEqual($direct, $expects);
|
||||
|
||||
$total = $this->NumberTree->children(null, null, array('id', 'name', 'parent_id', 'lft', 'rght'), null, null, null, 1);
|
||||
$expects = array(array('NumberTree' => array('id' => 2, 'name' => '1.1', 'parent_id' => 1, 'lft' => 2, 'rght' => 7)),
|
||||
array('NumberTree' => array('id' => 3, 'name' => '1.1.1', 'parent_id' => 2, 'lft' => 3, 'rght' => 4)),
|
||||
array('NumberTree' => array('id' => 4, 'name' => '1.1.2', 'parent_id' => 2, 'lft' => 5, 'rght' => 6)),
|
||||
array('NumberTree' => array('id' => 5, 'name' => '1.2', 'parent_id' => 1, 'lft' => 8, 'rght' => 13)),
|
||||
array('NumberTree' => array( 'id' => 6, 'name' => '1.2.1', 'parent_id' => 5, 'lft' => 9, 'rght' => 10)),
|
||||
array('NumberTree' => array('id' => 7, 'name' => '1.2.2', 'parent_id' => 5, 'lft' => 11, 'rght' => 12)));
|
||||
$this->assertEqual($total, $expects);
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
unset($this->NumberTree);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue