1: <?php
2: namespace TIC\DormBundle\Hydration;
3:
4: use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
5:
6: /**
7: * Récupération des résultats groupés par les valeurs de la 1ère colonne (en index).
8: *
9: * Exemple:
10: * > SELECT a, b, c FROM t
11: * < array(
12: * a1 => array(
13: * array('b' => b1, 'c' => c1),
14: * ),
15: * a2 => array(
16: * array('b' => b2, 'c' => c2),
17: * array('b' => b22, 'c' => c22),
18: * ),
19: * ...
20: * )
21: */
22: class GroupbyHydrator extends AbstractHydrator
23: {
24:
25: /**
26: * Hydrates all rows from the current statement instance at once.
27: *
28: * @return mixed[]
29: */
30: protected function hydrateAllData(): array
31: {
32: # return $this->_stmt->fetchAll(\PDO::FETCH_ASSOC|\PDO::FETCH_GROUP);
33:
34: $result = array();
35:
36: while ($data = $this->statement()->fetchAssociative()) {
37: $key = \array_shift($data);
38: if ($key === null) $key = '[_NULL_]';
39: elseif ($key === '') $key = '[_EMPTY_]';
40:
41: if (! isset($result[ $key ])) $result[ $key ] = array();
42: $result[ $key ][] = $data;
43: }
44:
45: return $result;
46: }
47:
48: }
49: