Skip to content

Commit

Permalink
move the paths used into options
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Jan 9, 2012
1 parent 7b640b8 commit 442d889
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
10 changes: 5 additions & 5 deletions lib/Cake/Model/Model.php
Expand Up @@ -2813,11 +2813,11 @@ protected function _findThreaded($state, $query, $results = array()) {
return $query;
} elseif ($state === 'after') {
return Set::nest($results, array(
'alias' => $this->alias,
'primaryKey' => $this->primaryKey,
'parent' => 'parent_id',
'children' => 'children'
));
'alias' => $this->alias,
'key' => $this->primaryKey,
'parent' => 'parent_id',
'children' => 'children'
));
}
}

Expand Down
27 changes: 18 additions & 9 deletions lib/Cake/Utility/Set.php
Expand Up @@ -1121,9 +1121,11 @@ public static function apply($path, $data, $callback, $options = array()) {
* @param mixed $data
* @param array $options Options are:
* alias - the first array key to look for
* primaryKey - the key to use to identify the rows
* key - the key to use to identify the rows
* parentId - the key to use to identify the parent
* children - the key name to use in the resultset for children
* idPath - the path to a key that identifies each entry
* parentPath - the path to a key that identifies the parent of each entry
* @return array of results, nested
* @link
*/
Expand All @@ -1134,35 +1136,42 @@ public static function nest($data, $options = array()) {

$options = array(
'alias' => key(current($data)),
'primaryKey' => 'id',
'key' => 'id',
'parentId' => 'parent_id',
'children' => 'children'
'children' => 'children',
) + $options;

if (empty($options['idPath'])) {
$options['idPath'] = '/' . $options['alias'] . '/' . $options['key'];
}
if (empty($options['parentPath'])) {
$options['parentPath'] = '/' . $options['alias'] . '/' . $options['parentId'];
}

$return = $idMap = array();
$ids = Set::extract($data, '{n}.' . $options['alias'] . '.' . $options['primaryKey']);
$ids = Set::extract($data, $options['idPath']);

foreach ($data as $result) {
$result[$options['children']] = array();
$id = $result[$options['alias']][$options['primaryKey']];
$id = $result[$options['alias']][$options['key']];
if (isset($result[$options['alias']][$options['parentId']])) {
$parentId = $result[$options['alias']][$options['parentId']];
} else {
$parentId = null;
}
if (isset($idMap[$id]['children'])) {
if (isset($idMap[$id][$options['children']])) {
$idMap[$id] = array_merge($result, (array)$idMap[$id]);
} else {
$idMap[$id] = array_merge($result, array('children' => array()));
$idMap[$id] = array_merge($result, array($options['children'] => array()));
}
if (!$parentId || !in_array($parentId, $ids)) {
$return[] =& $idMap[$id];
} else {
$idMap[$parentId]['children'][] =& $idMap[$id];
$idMap[$parentId][$options['children']][] =& $idMap[$id];
}
}
if (count($return) > 1) {
$ids = array_unique(Set::extract('/' . $options['alias'] . '/' . $options['parentId'], $return));
$ids = array_unique(Set::extract($options['parentPath'], $return));
if (count($ids) > 1) {
$root = $return[0][$options['alias']][$options['parentId']];
foreach ($return as $key => $value) {
Expand Down

0 comments on commit 442d889

Please sign in to comment.