1: <?php
2: namespace TIC\DormBundle\Hydration;
3:
4: use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
5:
6: /**
7: * Récupération d'une liste d'entités en intégrant les résultats de colonnes supplémentaires dans des propriétés.
8: * Note: les entités doivent disposer d'une propriété correspondante accessible (public ou avec setter/mutator)
9: *
10: * Exemple:
11: * > SELECT a, COUNT(a.extras) AS nbExtras FROM t
12: * < array( a1, a2, a3... ) avec a1.nbExtras, a2.nbExtras...
13: */
14: class ExtrasHydrator extends ObjectHydrator
15: {
16:
17: /**
18: * Hydrates all rows from the current statement instance at once.
19: *
20: * @return mixed[]
21: */
22: protected function hydrateAllData(): array
23: {
24: $result = array();
25:
26: foreach (parent::hydrateAllData() as $row) {
27: if (! \is_object($row[0])) continue;
28:
29: foreach ($row as $field => $value) {
30: if (! \is_int($field)) $this->setExtraField($row[0], $field, $value);
31: }
32: $result[] = $row[0];
33: }
34:
35: return $result;
36: }
37:
38: /**
39: * @param Object $object Entity
40: * @param string $property Field name
41: * @param string $value Field value
42: */
43: protected function setExtraField($object, $property, $value)
44: {
45: $mutator = 'set' . \ucfirst($property);
46: if (\method_exists($object, $mutator)) return \call_user_func(array($object, $mutator), $value);
47: if (\property_exists($object, $property)) $object->{$property} = $value;
48: }
49:
50: }
51: