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: * Note: colonne 1 en clé et colonne 2 en valeurs uniquement
9: *
10: * Exemple:
11: * > SELECT a, b, c FROM t
12: * < array(
13: * a1 => array( b1 ),
14: * a2 => array( b2, b22 ),
15: * ...
16: * )
17: */
18: class KeylistHydrator extends AbstractHydrator
19: {
20:
21: /**
22: * Hydrates all rows from the current statement instance at once.
23: *
24: * @return mixed[]
25: */
26: protected function hydrateAllData(): array
27: {
28: $result = array();
29:
30: while ($data = $this->statement()->fetchNumeric()) {
31: if (! \is_array($data) || \count($data) < 2) continue;
32:
33: $key = $data[0];
34: if ($key === null) $key = '[_NULL_]';
35: elseif ($key === '') $key = '[_EMPTY_]';
36:
37: $val = (\count($data) > 1) ? $data[1] : $data[0];
38:
39: if (! isset($result[ $key ])) $result[ $key ] = array();
40: $result[ $key ][] = $val;
41: }
42: return $result;
43: }
44:
45: /**
46: * BC layer for a wide-spread use-case of old DBAL APIs
47: *
48: * @deprecated This API is deprecated and will be removed after 2022
49: */
50: # protected function hydrateAllData(): array
51: # {
52: # return $this->_stmt->fetchAll(\PDO::FETCH_COLUMN|\PDO::FETCH_GROUP, null);
53: # }
54:
55: }
56: