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

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\ORM\Internal\Hydration;
  20. use Doctrine\ORM\Mapping\ClassMetadata;
  21. use Doctrine\ORM\Query;
  22. use Exception;
  23. use PDO;
  24. use RuntimeException;
  25. use function array_keys;
  26. use function array_search;
  27. use function count;
  28. use function in_array;
  29. use function key;
  30. use function reset;
  31. use function sprintf;
  32. class SimpleObjectHydrator extends AbstractHydrator
  33. {
  34.     /** @var ClassMetadata */
  35.     private $class;
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     protected function prepare()
  40.     {
  41.         if (count($this->_rsm->aliasMap) !== 1) {
  42.             throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.');
  43.         }
  44.         if ($this->_rsm->scalarMappings) {
  45.             throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.');
  46.         }
  47.         $this->class $this->getClassMetadata(reset($this->_rsm->aliasMap));
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     protected function cleanup()
  53.     {
  54.         parent::cleanup();
  55.         $this->_uow->triggerEagerLoads();
  56.         $this->_uow->hydrationComplete();
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     protected function hydrateAllData()
  62.     {
  63.         $result = [];
  64.         while ($row $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
  65.             $this->hydrateRowData($row$result);
  66.         }
  67.         $this->_em->getUnitOfWork()->triggerEagerLoads();
  68.         return $result;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     protected function hydrateRowData(array $row, array &$result)
  74.     {
  75.         $entityName       $this->class->name;
  76.         $data             = [];
  77.         $discrColumnValue null;
  78.         // We need to find the correct entity class name if we have inheritance in resultset
  79.         if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
  80.             $discrColumnName $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
  81.             // Find mapped discriminator column from the result set.
  82.             if ($metaMappingDiscrColumnName array_search($discrColumnName$this->_rsm->metaMappings)) {
  83.                 $discrColumnName $metaMappingDiscrColumnName;
  84.             }
  85.             if (! isset($row[$discrColumnName])) {
  86.                 throw HydrationException::missingDiscriminatorColumn($entityName$discrColumnNamekey($this->_rsm->aliasMap));
  87.             }
  88.             if ($row[$discrColumnName] === '') {
  89.                 throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
  90.             }
  91.             $discrMap $this->class->discriminatorMap;
  92.             if (! isset($discrMap[$row[$discrColumnName]])) {
  93.                 throw HydrationException::invalidDiscriminatorValue($row[$discrColumnName], array_keys($discrMap));
  94.             }
  95.             $entityName       $discrMap[$row[$discrColumnName]];
  96.             $discrColumnValue $row[$discrColumnName];
  97.             unset($row[$discrColumnName]);
  98.         }
  99.         foreach ($row as $column => $value) {
  100.             // An ObjectHydrator should be used instead of SimpleObjectHydrator
  101.             if (isset($this->_rsm->relationMap[$column])) {
  102.                 throw new Exception(sprintf('Unable to retrieve association information for column "%s"'$column));
  103.             }
  104.             $cacheKeyInfo $this->hydrateColumnInfo($column);
  105.             if (! $cacheKeyInfo) {
  106.                 continue;
  107.             }
  108.             // Check if value is null before conversion (because some types convert null to something else)
  109.             $valueIsNull $value === null;
  110.             // Convert field to a valid PHP value
  111.             if (isset($cacheKeyInfo['type'])) {
  112.                 $type  $cacheKeyInfo['type'];
  113.                 $value $type->convertToPHPValue($value$this->_platform);
  114.             }
  115.             $fieldName $cacheKeyInfo['fieldName'];
  116.             // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
  117.             if (! isset($data[$fieldName]) || ! $valueIsNull) {
  118.                 // If we have inheritance in resultset, make sure the field belongs to the correct class
  119.                 if (isset($cacheKeyInfo['discriminatorValues']) && ! in_array((string) $discrColumnValue$cacheKeyInfo['discriminatorValues'], true)) {
  120.                     continue;
  121.                 }
  122.                 $data[$fieldName] = $value;
  123.             }
  124.         }
  125.         if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
  126.             $this->registerManaged($this->class$this->_hints[Query::HINT_REFRESH_ENTITY], $data);
  127.         }
  128.         $uow    $this->_em->getUnitOfWork();
  129.         $entity $uow->createEntity($entityName$data$this->_hints);
  130.         $result[] = $entity;
  131.         if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) {
  132.             $this->_uow->hydrationComplete();
  133.         }
  134.     }
  135. }