Skip to content

Commit

Permalink
Updating find('threaded') to not return ArrayObject anymore and to
Browse files Browse the repository at this point in the history
account for top parents that are not null or zero
  • Loading branch information
lorenzo committed Aug 17, 2013
1 parent ea4cf1f commit b1b9370
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
10 changes: 5 additions & 5 deletions lib/Cake/ORM/Query.php
Expand Up @@ -533,17 +533,17 @@ public function mapReduce(callable $mapper, callable $reducer = null) {
}

protected function _applyFormatters($result) {
foreach ($this->_formatters as $formatter) {
$result = new ResultSetDecorator($result, $formatter);
}

foreach ($this->_mapReduce as $mappers) {
$result = new MapReduce($result, $mappers);
}

if (!empty($this->_mapReduce)) {
$result = new ResultSetDecorator($result);
}
}

foreach ($this->_formatters as $formatter) {
$result = new ResultSetDecorator($result, $formatter);
}
return $result;
}

Expand Down
29 changes: 19 additions & 10 deletions lib/Cake/ORM/Table.php
Expand Up @@ -552,19 +552,28 @@ public function findList(Query $query, array $options = []) {
}

public function findThreaded(Query $query, array $options = []) {
$parents = new \ArrayObject;
$mapper = function($key, $row, $mr) use ($parents) {
$parents[$row['id']] = new \ArrayObject($row);
if (empty($row['parent_id'])) {
return $mr->emit($parents[$row['id']]);
}
$mr->emitIntermediate($row['parent_id'], $parents[$row['id']]);
$parents = [];
$mapper = function($key, $row, $mr) use (&$parents) {
$parents[$row['id']] = &$row;
$mr->emitIntermediate($row['parent_id'], $row['id']);
};

$reducer = function($key, $values, $mr) use ($parents) {
$parents[$key]['children'] = $values;
$reducer = function($key, $values, $mr) use (&$parents) {
if (empty($key) || !isset($parents[$key])) {
foreach ($values as $id) {
$parents[$id] = new \ArrayObject($parents[$id]);
$mr->emit($parents[$id]);
}
return;
}
foreach ($values as $id) {
$parents[$key]['children'][] =& $parents[$id];
}
};
return $query->mapReduce($mapper, $reducer);

return $query->mapReduce($mapper, $reducer)->formatResults(function($row) {
return $row->getArrayCopy();
});
}

/**
Expand Down
48 changes: 24 additions & 24 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -541,54 +541,54 @@ public function testFindList() {

public function testFindThreaded() {
$table = new Table(['table' => 'categories', 'connection' => $this->connection]);
$expected = array(
new \ArrayObject(array(
$expected = [
[
'id' => 1,
'parent_id' => 0,
'name' => 'Category 1',
'children' => array(
new \ArrayObject(array(
'children' => [
[
'id' => 2,
'parent_id' => 1,
'name' => 'Category 1.1',
'children' => array(
new \ArrayObject(array(
'children' => [
[
'id' => 7,
'parent_id' => 2,
'name' => 'Category 1.1.1',
)),
new \ArrayObject(array(
],
[
'id' => 8,
'parent_id' => '2',
'name' => 'Category 1.1.2',
))
),
)),
new \ArrayObject(array(
]
],
],
[
'id' => 3,
'parent_id' => '1',
'name' => 'Category 1.2',
)),
)
)),
new \ArrayObject(array(
],
]
],
[
'id' => 4,
'parent_id' => 0,
'name' => 'Category 2',
)),
new \ArrayObject(array(
],
[
'id' => 5,
'parent_id' => 0,
'name' => 'Category 3',
'children' => array(
new \ArrayObject(array(
'children' => [
[
'id' => '6',
'parent_id' => '5',
'name' => 'Category 3.1',
))
)
))
);
]
]
]
];
$results = $table->find('threaded')
->select(['id', 'parent_id', 'name'])
->toArray();
Expand Down

0 comments on commit b1b9370

Please sign in to comment.