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

View file

@ -3327,7 +3327,21 @@ class ModelTest extends CakeTestCase {
'created' => '2007-03-18 15:30:23', 'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31' '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( array(
'Category' => array( 'Category' => array(
@ -3394,7 +3408,21 @@ class ModelTest extends CakeTestCase {
'created' => '2007-03-18 15:30:23', 'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31' '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( array(
'Category' => array( 'Category' => array(
@ -3426,7 +3454,17 @@ class ModelTest extends CakeTestCase {
'parent_id' => '1', 'parent_id' => '1',
'name' => 'Category 1.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( array(
'Category' => array( 'Category' => array(
@ -3527,7 +3565,21 @@ class ModelTest extends CakeTestCase {
'created' => '2007-03-18 15:30:23', 'created' => '2007-03-18 15:30:23',
'updated' => '2007-03-18 15:32:31' '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); $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 * testFindNeighbours method

View file

@ -35,14 +35,14 @@
class CategoryFixture extends CakeTestFixture { class CategoryFixture extends CakeTestFixture {
/** /**
* name property * name property
* *
* @var string 'Category' * @var string 'Category'
* @access public * @access public
*/ */
var $name = 'Category'; var $name = 'Category';
/** /**
* fields property * fields property
* *
* @var array * @var array
* @access public * @access public
*/ */
@ -55,7 +55,7 @@ class CategoryFixture extends CakeTestFixture {
); );
/** /**
* records property * records property
* *
* @var array * @var array
* @access public * @access public
*/ */
@ -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' => 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 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' => 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'),
); );
} }