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

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