vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php line 58

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