Fixes #5024, find('threaded') returns empty array if root node is not included in the results.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7321 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2008-07-05 14:10:42 +00:00
parent d853520e46
commit bdc3685e62
3 changed files with 93 additions and 11 deletions

View file

@ -1989,6 +1989,8 @@ class Model extends Overloadable {
return $query;
} elseif ($state == 'after') {
$return = $idMap = array();
$ids = Set::extract($results, '{n}.' . $this->alias . '.' . $this->primaryKey);
foreach ($results as $result) {
$result['children'] = array();
$id = $result[$this->alias][$this->primaryKey];
@ -1998,10 +2000,10 @@ class Model extends Overloadable {
} else {
$idMap[$id] = array_merge($result, array('children' => array()));
}
if ($parentId) {
$idMap[$parentId]['children'][] =& $idMap[$id];
} else {
if (!$parentId || !in_array($parentId, $ids)) {
$return[] =& $idMap[$id];
} else {
$idMap[$parentId]['children'][] =& $idMap[$id];
}
}
return $return;

View file

@ -3327,7 +3327,21 @@ class ModelTest extends CakeTestCase {
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'
),
'children' => array()
'children' => array(
array('Category' => array(
'id' => '7',
'parent_id' => '2',
'name' => 'Category 1.1.1',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()),
array('Category' => array(
'id' => '8',
'parent_id' => '2',
'name' => 'Category 1.1.2',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()))
),
array(
'Category' => array(
@ -3394,7 +3408,21 @@ class ModelTest extends CakeTestCase {
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'
),
'children' => array()
'children' => array(
array('Category' => array(
'id' => '7',
'parent_id' => '2',
'name' => 'Category 1.1.1',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()),
array('Category' => array(
'id' => '8',
'parent_id' => '2',
'name' => 'Category 1.1.2',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()))
),
array(
'Category' => array(
@ -3426,7 +3454,17 @@ class ModelTest extends CakeTestCase {
'parent_id' => '1',
'name' => 'Category 1.1'
),
'children' => array()
'children' => array(
array('Category' => array(
'id' => '7',
'parent_id' => '2',
'name' => 'Category 1.1.1'),
'children' => array()),
array('Category' => array(
'id' => '8',
'parent_id' => '2',
'name' => 'Category 1.1.2'),
'children' => array()))
),
array(
'Category' => array(
@ -3527,7 +3565,21 @@ class ModelTest extends CakeTestCase {
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'
),
'children' => array()
'children' => array(
array('Category' => array(
'id' => '8',
'parent_id' => '2',
'name' => 'Category 1.1.2',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()),
array('Category' => array(
'id' => '7',
'parent_id' => '2',
'name' => 'Category 1.1.1',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()))
)
)
)
@ -3559,6 +3611,32 @@ class ModelTest extends CakeTestCase {
)
);
$this->assertEqual($result, $expected);
$result = $TestModel->find('threaded', array('conditions' => array('Category.name LIKE' => 'Category 1.1%')));
$expected = array(
array('Category' =>
array(
'id' => '2',
'parent_id' => '1',
'name' => 'Category 1.1',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array(
array('Category' => array(
'id' => '7',
'parent_id' => '2',
'name' => 'Category 1.1.1',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()),
array('Category' => array(
'id' => '8',
'parent_id' => '2',
'name' => 'Category 1.1.2',
'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31'),
'children' => array()))));
$this->assertEqual($result, $expected);
}
/**
* testFindNeighbours method

View file

@ -65,7 +65,9 @@ class CategoryFixture extends CakeTestFixture {
array('parent_id' => 1, 'name' => 'Category 1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 0, 'name' => 'Category 2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 0, 'name' => 'Category 3', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 5, 'name' => 'Category 3.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31')
array('parent_id' => 5, 'name' => 'Category 3.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 2, 'name' => 'Category 1.1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
array('parent_id' => 2, 'name' => 'Category 1.1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'),
);
}