vendor/doctrine/orm/src/Internal/Hydration/SimpleObjectHydrator.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Internal\Hydration;
  4. use Doctrine\ORM\Internal\SQLResultCasing;
  5. use Doctrine\ORM\Mapping\ClassMetadata;
  6. use Doctrine\ORM\Mapping\MappingException;
  7. use Doctrine\ORM\Query;
  8. use Exception;
  9. use RuntimeException;
  10. use ValueError;
  11. use function array_keys;
  12. use function array_search;
  13. use function assert;
  14. use function count;
  15. use function in_array;
  16. use function key;
  17. use function reset;
  18. use function sprintf;
  19. class SimpleObjectHydrator extends AbstractHydrator
  20. {
  21.     use SQLResultCasing;
  22.     private ClassMetadata|null $class null;
  23.     protected function prepare(): void
  24.     {
  25.         if (count($this->resultSetMapping()->aliasMap) !== 1) {
  26.             throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.');
  27.         }
  28.         if ($this->resultSetMapping()->scalarMappings) {
  29.             throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.');
  30.         }
  31.         $this->class $this->getClassMetadata(reset($this->resultSetMapping()->aliasMap));
  32.     }
  33.     protected function cleanup(): void
  34.     {
  35.         parent::cleanup();
  36.         $this->uow->triggerEagerLoads();
  37.         $this->uow->hydrationComplete();
  38.     }
  39.     /**
  40.      * {@inheritDoc}
  41.      */
  42.     protected function hydrateAllData(): array
  43.     {
  44.         $result = [];
  45.         while ($row $this->statement()->fetchAssociative()) {
  46.             $this->hydrateRowData($row$result);
  47.         }
  48.         $this->em->getUnitOfWork()->triggerEagerLoads();
  49.         return $result;
  50.     }
  51.     /**
  52.      * {@inheritDoc}
  53.      */
  54.     protected function hydrateRowData(array $row, array &$result): void
  55.     {
  56.         assert($this->class !== null);
  57.         $entityName       $this->class->name;
  58.         $data             = [];
  59.         $discrColumnValue null;
  60.         // We need to find the correct entity class name if we have inheritance in resultset
  61.         if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
  62.             $discrColumn     $this->class->getDiscriminatorColumn();
  63.             $discrColumnName $this->getSQLResultCasing($this->platform$discrColumn->name);
  64.             // Find mapped discriminator column from the result set.
  65.             $metaMappingDiscrColumnName array_search($discrColumnName$this->resultSetMapping()->metaMappingstrue);
  66.             if ($metaMappingDiscrColumnName) {
  67.                 $discrColumnName $metaMappingDiscrColumnName;
  68.             }
  69.             if (! isset($row[$discrColumnName])) {
  70.                 throw HydrationException::missingDiscriminatorColumn(
  71.                     $entityName,
  72.                     $discrColumnName,
  73.                     key($this->resultSetMapping()->aliasMap),
  74.                 );
  75.             }
  76.             if ($row[$discrColumnName] === '') {
  77.                 throw HydrationException::emptyDiscriminatorValue(key(
  78.                     $this->resultSetMapping()->aliasMap,
  79.                 ));
  80.             }
  81.             $discrMap $this->class->discriminatorMap;
  82.             if (! isset($discrMap[$row[$discrColumnName]])) {
  83.                 throw HydrationException::invalidDiscriminatorValue($row[$discrColumnName], array_keys($discrMap));
  84.             }
  85.             $entityName       $discrMap[$row[$discrColumnName]];
  86.             $discrColumnValue $row[$discrColumnName];
  87.             unset($row[$discrColumnName]);
  88.         }
  89.         foreach ($row as $column => $value) {
  90.             // An ObjectHydrator should be used instead of SimpleObjectHydrator
  91.             if (isset($this->resultSetMapping()->relationMap[$column])) {
  92.                 throw new Exception(sprintf('Unable to retrieve association information for column "%s"'$column));
  93.             }
  94.             $cacheKeyInfo $this->hydrateColumnInfo($column);
  95.             if (! $cacheKeyInfo) {
  96.                 continue;
  97.             }
  98.             // If we have inheritance in resultset, make sure the field belongs to the correct class
  99.             if (isset($cacheKeyInfo['discriminatorValues']) && ! in_array((string) $discrColumnValue$cacheKeyInfo['discriminatorValues'], true)) {
  100.                 continue;
  101.             }
  102.             // Check if value is null before conversion (because some types convert null to something else)
  103.             $valueIsNull $value === null;
  104.             // Convert field to a valid PHP value
  105.             if (isset($cacheKeyInfo['type'])) {
  106.                 $type  $cacheKeyInfo['type'];
  107.                 $value $type->convertToPHPValue($value$this->platform);
  108.             }
  109.             if ($value !== null && isset($cacheKeyInfo['enumType'])) {
  110.                 $originalValue $value;
  111.                 try {
  112.                     $value $this->buildEnum($originalValue$cacheKeyInfo['enumType']);
  113.                 } catch (ValueError $e) {
  114.                     throw MappingException::invalidEnumValue(
  115.                         $entityName,
  116.                         $cacheKeyInfo['fieldName'],
  117.                         (string) $originalValue,
  118.                         $cacheKeyInfo['enumType'],
  119.                         $e,
  120.                     );
  121.                 }
  122.             }
  123.             $fieldName $cacheKeyInfo['fieldName'];
  124.             // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
  125.             if (! isset($data[$fieldName]) || ! $valueIsNull) {
  126.                 $data[$fieldName] = $value;
  127.             }
  128.         }
  129.         if (isset($this->hints[Query::HINT_REFRESH_ENTITY])) {
  130.             $this->registerManaged($this->class$this->hints[Query::HINT_REFRESH_ENTITY], $data);
  131.         }
  132.         $uow    $this->em->getUnitOfWork();
  133.         $entity $uow->createEntity($entityName$data$this->hints);
  134.         $result[] = $entity;
  135.         if (isset($this->hints[Query::HINT_INTERNAL_ITERATION]) && $this->hints[Query::HINT_INTERNAL_ITERATION]) {
  136.             $this->uow->hydrationComplete();
  137.         }
  138.     }
  139. }