Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implementing find('list') and find('threaded') with formatResult, this

helps the table class to keep more focused and with lesss code
  • Loading branch information...
commit f5d8b9585b29139e13e8a1a5d5fe6d3dc27c9eca 1 parent e671a53
@lorenzo lorenzo authored
View
2  src/Collection/CollectionTrait.php
@@ -716,7 +716,7 @@ public function nest($idPath, $parentPath) {
$parents = [];
$idPath = $this->_propertyExtractor($idPath);
$parentPath = $this->_propertyExtractor($parentPath);
- $isObject = !is_array($this->first());
+ $isObject = !is_array((new Collection($this))->first());
$mapper = function($row, $key, $mapReduce) use (&$parents, $idPath, $parentPath) {
$row['children'] = [];
View
2  src/ORM/Query.php
@@ -881,7 +881,7 @@ protected function _decorateResults($result) {
}
foreach ($this->_formatters as $formatter) {
- $result = $formatter($this, $result);
+ $result = $formatter($result, $this);
}
if (!empty($this->_formatters) && !($result instanceof ResultSetDecorator)) {
View
65 src/ORM/Table.php
@@ -741,29 +741,16 @@ public function findList(Query $query, array $options = []) {
$options += [
'idField' => $this->primaryKey(),
'valueField' => $this->displayField(),
- 'groupField' => false
+ 'groupField' => null
];
- $mapper = function($row, $key, $mapReduce) use ($options) {
- $rowKey = $options['idField'];
- $rowVal = $options['valueField'];
- if (!($options['groupField'])) {
- $mapReduce->emit($row[$rowVal], $row[$rowKey]);
- return;
- }
-
- $key = $row[$options['groupField']];
- $mapReduce->emitIntermediate([$row[$rowKey] => $row[$rowVal]], $key);
- };
-
- $reducer = function($values, $key, $mapReduce) {
- $result = [];
- foreach ($values as $value) {
- $result += $value;
- }
- $mapReduce->emit($result, $key);
- };
- return $query->mapReduce($mapper, $reducer);
+ return $query->formatResults(function($results) use ($options) {
+ return $results->combine(
+ $options['idField'],
+ $options['valueField'],
+ $options['groupField']
+ );
+ });
}
/**
@@ -778,36 +765,14 @@ public function findList(Query $query, array $options = []) {
* @return \Cake\ORM\Query
*/
public function findThreaded(Query $query, array $options = []) {
- $parents = [];
- $hydrate = $query->hydrate();
- $mapper = function($row, $key, $mapReduce) use (&$parents) {
- $row['children'] = [];
- $parents[$row['id']] =& $row;
- $mapReduce->emitIntermediate($row['id'], $row['parent_id']);
- };
-
- $reducer = function($values, $key, $mapReduce) use (&$parents, $hydrate) {
- if (empty($key) || !isset($parents[$key])) {
- foreach ($values as $id) {
- $parents[$id] = $hydrate ? $parents[$id] : new \ArrayObject($parents[$id]);
- $mapReduce->emit($parents[$id]);
- }
- return;
- }
-
- foreach ($values as $id) {
- $parents[$key]['children'][] =& $parents[$id];
- }
- };
-
- $query->mapReduce($mapper, $reducer);
- if (!$hydrate) {
- $query->mapReduce(function($row, $key, $mapReduce) {
- $mapReduce->emit($row->getArrayCopy());
- });
- }
+ $options += [
+ 'idField' => $this->primaryKey(),
+ 'parentField' => 'parent_id',
+ ];
- return $query;
+ return $query->formatResults(function($results) use ($options) {
+ return $results->nest($options['idField'], $options['parentField']);
+ });
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.