Skip to content
Permalink
Browse files

Fixing infinitely nesting stack frames when TreeBehavior::reorder() i…

…s called and the models $cacheQueries = true and there are a sizable number of records being manipulated.

Test cases added.
Fixes #188
  • Loading branch information...
markstory committed Oct 21, 2009
1 parent c6f783e commit 0e23fdfbb3d2889870215cfa459e5bbae87acbdb
Showing with 23 additions and 1 deletion.
  1. +4 −1 cake/libs/model/behaviors/tree.php
  2. +19 −0 cake/tests/cases/libs/model/behaviors/tree.test.php
@@ -460,7 +460,7 @@ 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;
}
@@ -640,6 +640,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];
@@ -649,6 +651,7 @@ function reorder(&$Model, $options = array()) {
}
}
}
$Model->cacheQueries = $cacheQueries;
return true;
}
/**
@@ -1172,6 +1172,25 @@ function testReorderTree() {
$sortedNodes = $this->Tree->find('list', array('order' => $leftField));
$this->assertIdentical($nodes, $sortedNodes);
}
/**
* test reordering large-ish trees with cacheQueries = true.
* This caused infinite loops when moving down elements as stale data is returned
* from the memory cache
*
* @access public
* @return void
*/
function testReorderBigTreeWithQueryCaching() {
extract($this->settings);
$this->Tree =& new $modelClass();
$this->Tree->initialize(2, 10);
$original = $this->Tree->cacheQueries;
$this->Tree->cacheQueries = true;
$this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
$this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
$this->Tree->cacheQueries = $original;
}
/**
* testGenerateTreeListWithSelfJoin method
*

0 comments on commit 0e23fdf

Please sign in to comment.
You can’t perform that action at this time.