Skip to content

Commit

Permalink
Fixing infinitely nesting stack frames when TreeBehavior::reorder() i…
Browse files Browse the repository at this point in the history
…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 0e23fdf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cake/libs/model/behaviors/tree.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand All @@ -649,6 +651,7 @@ function reorder(&$Model, $options = array()) {
}
}
}
$Model->cacheQueries = $cacheQueries;
return true;
}
/**
Expand Down
19 changes: 19 additions & 0 deletions cake/tests/cases/libs/model/behaviors/tree.test.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 0e23fdf

Please sign in to comment.