From bdc3685e628383770dcca94319b8d8de66ce6284 Mon Sep 17 00:00:00 2001 From: phpnut Date: Sat, 5 Jul 2008 14:10:42 +0000 Subject: [PATCH] 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 --- cake/libs/model/model.php | 8 +- cake/tests/cases/libs/model/model.test.php | 86 +++++++++++++++++++++- cake/tests/fixtures/category_fixture.php | 10 ++- 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 56518f375..033fc249a 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -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; diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index fb074f876..a7117bba3 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -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 diff --git a/cake/tests/fixtures/category_fixture.php b/cake/tests/fixtures/category_fixture.php index 9f826373b..d13bf2b58 100644 --- a/cake/tests/fixtures/category_fixture.php +++ b/cake/tests/fixtures/category_fixture.php @@ -35,14 +35,14 @@ class CategoryFixture extends CakeTestFixture { /** * name property - * + * * @var string 'Category' * @access public */ var $name = 'Category'; /** * fields property - * + * * @var array * @access public */ @@ -55,7 +55,7 @@ class CategoryFixture extends CakeTestFixture { ); /** * records property - * + * * @var array * @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' => 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'), ); }