Browse Source

Implementing find('list') and find('threaded') with formatResult, this
helps the table class to keep more focused and with lesss code

Jose Lorenzo Rodriguez 12 years ago
parent
commit
f5d8b9585b
3 changed files with 17 additions and 52 deletions
  1. 1 1
      src/Collection/CollectionTrait.php
  2. 1 1
      src/ORM/Query.php
  3. 15 50
      src/ORM/Table.php

+ 1 - 1
src/Collection/CollectionTrait.php

@@ -716,7 +716,7 @@ trait CollectionTrait {
 		$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'] = [];

+ 1 - 1
src/ORM/Query.php

@@ -881,7 +881,7 @@ class Query extends DatabaseQuery {
 		}
 
 		foreach ($this->_formatters as $formatter) {
-			$result = $formatter($this, $result);
+			$result = $formatter($result, $this);
 		}
 
 		if (!empty($this->_formatters) && !($result instanceof ResultSetDecorator)) {

+ 15 - 50
src/ORM/Table.php

@@ -741,29 +741,16 @@ class Table implements EventListener {
 		$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 @@ class Table implements EventListener {
  * @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']);
+		});
 	}
 
 /**