allow specifying an explicit root

This commit is contained in:
AD7six 2012-01-18 13:05:44 +01:00
parent 7414d0f77b
commit f1f1ce0db4
2 changed files with 131 additions and 14 deletions

View file

@ -3286,6 +3286,116 @@ class SetTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* test Set nest with a normal model result set, and a nominated root id
*
* @return void
*/
public function testNestModelExplicitRoot() {
$input = array(
array(
'ModelName' => array(
'id' => 1,
'parent_id' => null
),
),
array(
'ModelName' => array(
'id' => 2,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 3,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 4,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 5,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 6,
'parent_id' => null
),
),
array(
'ModelName' => array(
'id' => 7,
'parent_id' => 6
),
),
array(
'ModelName' => array(
'id' => 8,
'parent_id' => 6
),
),
array(
'ModelName' => array(
'id' => 9,
'parent_id' => 6
),
),
array(
'ModelName' => array(
'id' => 10,
'parent_id' => 6
)
)
);
$expected = array(
array(
'ModelName' => array(
'id' => 6,
'parent_id' => null
),
'children' => array(
array(
'ModelName' => array(
'id' => 7,
'parent_id' => 6
),
'children' => array()
),
array(
'ModelName' => array(
'id' => 8,
'parent_id' => 6
),
'children' => array()
),
array(
'ModelName' => array(
'id' => 9,
'parent_id' => 6
),
'children' => array()
),
array(
'ModelName' => array(
'id' => 10,
'parent_id' => 6
),
'children' => array()
)
)
)
);
$result = Set::nest($input, array('root' => 6));
$this->assertEquals($expected, $result);
}
/** /**
* test Set nest with a 1d array - this method should be able to handle any type of array input * test Set nest with a 1d array - this method should be able to handle any type of array input
* *

View file

@ -1123,6 +1123,7 @@ class Set {
* children - the key name to use in the resultset for children * children - the key name to use in the resultset for children
* idPath - the path to a key that identifies each entry * idPath - the path to a key that identifies each entry
* parentPath - the path to a key that identifies the parent of each entry * parentPath - the path to a key that identifies the parent of each entry
* root - the id of the desired top-most result
* @return array of results, nested * @return array of results, nested
* @link * @link
*/ */
@ -1135,7 +1136,8 @@ class Set {
$options += array( $options += array(
'idPath' => "/$alias/id", 'idPath' => "/$alias/id",
'parentPath' => "/$alias/parent_id", 'parentPath' => "/$alias/parent_id",
'children' => 'children' 'children' => 'children',
'root' => null
); );
$return = $idMap = array(); $return = $idMap = array();
@ -1161,16 +1163,21 @@ class Set {
} }
} }
if ($options['root']) {
$root = $options['root'];
} else {
$root = Set::get($return[0], $parentKeys); $root = Set::get($return[0], $parentKeys);
}
foreach ($return as $i => $result) { foreach ($return as $i => $result) {
$id = Set::get($result, $idKeys);
$parentId = Set::get($result, $parentKeys); $parentId = Set::get($result, $parentKeys);
if ($parentId != $root) { if ($id !== $root && $parentId != $root) {
unset($return[$i]); unset($return[$i]);
} }
} }
return $return; return array_values($return);
} }
/** /**