/** @override */
 protected function _hydrateAll()
 {
     $cache = array();
     $result = $this->_stmt->fetchAll(PDO::FETCH_ASSOC);
     //TODO: Let this exception be raised by Query as QueryException
     if (count($result) > 1 || count($result[0]) > 1) {
         throw HydrationException::nonUniqueResult();
     }
     $result = $this->_gatherScalarRowData($result[0], $cache);
     return array_shift($result);
 }
 /**
  * {@inheritdoc}
  */
 protected function hydrateRowData(array $sqlResult, array &$result)
 {
     $entityName = $this->class->name;
     $data = array();
     // We need to find the correct entity class name if we have inheritance in resultset
     if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
         $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
         // Find mapped discriminator column from the result set.
         if ($metaMappingDiscrColumnName = array_search($discrColumnName, $this->_rsm->metaMappings)) {
             $discrColumnName = $metaMappingDiscrColumnName;
         }
         if (!isset($sqlResult[$discrColumnName])) {
             throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
         }
         if ($sqlResult[$discrColumnName] === '') {
             throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
         }
         $discrMap = $this->class->discriminatorMap;
         if (!isset($discrMap[$sqlResult[$discrColumnName]])) {
             throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap));
         }
         $entityName = $discrMap[$sqlResult[$discrColumnName]];
         unset($sqlResult[$discrColumnName]);
     }
     foreach ($sqlResult as $column => $value) {
         // An ObjectHydrator should be used instead of SimpleObjectHydrator
         if (isset($this->_rsm->relationMap[$column])) {
             throw new \Exception(sprintf('Unable to retrieve association information for column "%s"', $column));
         }
         $cacheKeyInfo = $this->hydrateColumnInfo($column);
         if (!$cacheKeyInfo) {
             continue;
         }
         // Convert field to a valid PHP value
         if (isset($cacheKeyInfo['type'])) {
             $type = $cacheKeyInfo['type'];
             $value = $type->convertToPHPValue($value, $this->_platform);
         }
         $fieldName = $cacheKeyInfo['fieldName'];
         // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
         if (!isset($data[$fieldName]) || $value !== null) {
             $data[$fieldName] = $value;
         }
     }
     if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
         $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
     }
     $uow = $this->_em->getUnitOfWork();
     $entity = $uow->createEntity($entityName, $data, $this->_hints);
     $result[] = $entity;
 }
 /** @override */
 protected function _prepare()
 {
     $this->_identifierMap = $this->_resultPointers = $this->_idTemplate = array();
     $this->_resultCounter = 0;
     if (!isset($this->_hints['deferEagerLoad'])) {
         $this->_hints['deferEagerLoad'] = true;
     }
     foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
         $this->_identifierMap[$dqlAlias] = array();
         $this->_idTemplate[$dqlAlias] = '';
         $class = $this->_em->getClassMetadata($className);
         if (!isset($this->_ce[$className])) {
             $this->_ce[$className] = $class;
         }
         // Remember which associations are "fetch joined", so that we know where to inject
         // collection stubs or proxies and where not.
         if (isset($this->_rsm->relationMap[$dqlAlias])) {
             if (!isset($this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]])) {
                 throw HydrationException::parentObjectOfRelationNotFound($dqlAlias, $this->_rsm->parentAliasMap[$dqlAlias]);
             }
             $sourceClassName = $this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]];
             $sourceClass = $this->_getClassMetadata($sourceClassName);
             $assoc = $sourceClass->associationMappings[$this->_rsm->relationMap[$dqlAlias]];
             $this->_hints['fetched'][$sourceClassName][$assoc['fieldName']] = true;
             if ($sourceClass->subClasses) {
                 foreach ($sourceClass->subClasses as $sourceSubclassName) {
                     $this->_hints['fetched'][$sourceSubclassName][$assoc['fieldName']] = true;
                 }
             }
             if ($assoc['type'] != ClassMetadata::MANY_TO_MANY) {
                 // Mark any non-collection opposite sides as fetched, too.
                 if ($assoc['mappedBy']) {
                     $this->_hints['fetched'][$className][$assoc['mappedBy']] = true;
                 } else {
                     if ($assoc['inversedBy']) {
                         $inverseAssoc = $class->associationMappings[$assoc['inversedBy']];
                         if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) {
                             $this->_hints['fetched'][$className][$inverseAssoc['fieldName']] = true;
                             if ($class->subClasses) {
                                 foreach ($class->subClasses as $targetSubclassName) {
                                     $this->_hints['fetched'][$targetSubclassName][$inverseAssoc['fieldName']] = true;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function hydrateRowData(array $sqlResult, array &$cache, array &$result)
 {
     $entityName = $this->class->name;
     $data = array();
     // We need to find the correct entity class name if we have inheritance in resultset
     if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
         $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
         if ($sqlResult[$discrColumnName] === '') {
             throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
         }
         $entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]];
         unset($sqlResult[$discrColumnName]);
     }
     foreach ($sqlResult as $column => $value) {
         // Hydrate column information if not yet present
         if (!isset($cache[$column])) {
             if (($info = $this->hydrateColumnInfo($entityName, $column)) === null) {
                 continue;
             }
             $cache[$column] = $info;
         }
         // Convert field to a valid PHP value
         if (isset($cache[$column]['field'])) {
             $type = Type::getType($cache[$column]['class']->fieldMappings[$cache[$column]['name']]['type']);
             $value = $type->convertToPHPValue($value, $this->_platform);
         }
         // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
         if (isset($cache[$column]) && (!isset($data[$cache[$column]['name']]) || $value !== null)) {
             $data[$cache[$column]['name']] = $value;
         }
     }
     if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
         $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
     }
     $uow = $this->_em->getUnitOfWork();
     $entity = $uow->createEntity($entityName, $data, $this->_hints);
     //TODO: These should be invoked later, after hydration, because associations may not yet be loaded here.
     if (isset($this->class->lifecycleCallbacks[Events::postLoad])) {
         $this->class->invokeLifecycleCallbacks(Events::postLoad, $entity);
     }
     $evm = $this->_em->getEventManager();
     if ($evm->hasListeners(Events::postLoad)) {
         $evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->_em));
     }
     $result[] = $entity;
 }
 protected function prepare()
 {
     $this->_identifierMap = $this->_resultPointers = $this->_idTemplate = array();
     $this->_resultCounter = 0;
     $this->dtoMap = DtoClassMap::getMapDto();
     foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
         $this->_identifierMap[$dqlAlias] = array();
         $this->_idTemplate[$dqlAlias] = '';
         if (!isset($this->_ce[$className])) {
             $this->_ce[$className] = $this->_em->getClassMetadata($className);
         }
         // Remember which associations are "fetch joined", so that we know where to inject
         // collection stubs or proxies and where not.
         if (!isset($this->_rsm->relationMap[$dqlAlias])) {
             continue;
         }
         if (!isset($this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]])) {
             throw HydrationException::parentObjectOfRelationNotFound($dqlAlias, $this->_rsm->parentAliasMap[$dqlAlias]);
         }
         $sourceClassName = $this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]];
         $sourceClass = $this->_getClassMetadata($sourceClassName);
         $assoc = $sourceClass->associationMappings[$this->_rsm->relationMap[$dqlAlias]];
         $this->_hints['fetched'][$this->_rsm->parentAliasMap[$dqlAlias]][$assoc['fieldName']] = true;
         if ($assoc['type'] === ClassMetadata::MANY_TO_MANY) {
             continue;
         }
         // Mark any non-collection opposite sides as fetched, too.
         if ($assoc['mappedBy']) {
             $this->_hints['fetched'][$dqlAlias][$assoc['mappedBy']] = true;
             continue;
         }
         // handle fetch-joined owning side bi-directional one-to-one associations
         if ($assoc['inversedBy']) {
             $class = $this->_ce[$className];
             $inverseAssoc = $class->associationMappings[$assoc['inversedBy']];
             if (!($inverseAssoc['type'] & ClassMetadata::TO_ONE)) {
                 continue;
             }
             $this->_hints['fetched'][$dqlAlias][$inverseAssoc['fieldName']] = true;
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function hydrateRowData(array $sqlResult, array &$cache, array &$result)
 {
     $entityName = $this->class->name;
     $data = array();
     // We need to find the correct entity class name if we have inheritance in resultset
     if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
         $discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
         if (!isset($sqlResult[$discrColumnName])) {
             throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
         }
         if ($sqlResult[$discrColumnName] === '') {
             throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
         }
         $entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]];
         unset($sqlResult[$discrColumnName]);
     }
     foreach ($sqlResult as $column => $value) {
         // Hydrate column information if not yet present
         if (!isset($cache[$column])) {
             if (($info = $this->hydrateColumnInfo($entityName, $column)) === null) {
                 continue;
             }
             $cache[$column] = $info;
         }
         // Convert field to a valid PHP value
         if (isset($cache[$column]['type'])) {
             $value = Type::getType($cache[$column]['type'])->convertToPHPValue($value, $this->_platform);
         }
         // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
         if (isset($cache[$column]) && (!isset($data[$cache[$column]['name']]) || $value !== null)) {
             $data[$cache[$column]['name']] = $value;
         }
     }
     if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
         $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
     }
     $uow = $this->_em->getUnitOfWork();
     $entity = $uow->createEntity($entityName, $data, $this->_hints);
     $result[] = $entity;
 }
 /**
  * Gets an entity instance.
  *
  * @param array  $data     The instance data.
  * @param string $dqlAlias The DQL alias of the entity's class.
  *
  * @return object The entity.
  *
  * @throws HydrationException
  */
 private function getEntity(array $data, $dqlAlias)
 {
     $className = $this->_rsm->aliasMap[$dqlAlias];
     if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
         $fieldName = $this->_rsm->discriminatorColumns[$dqlAlias];
         if (!isset($this->_rsm->metaMappings[$fieldName])) {
             throw HydrationException::missingDiscriminatorMetaMappingColumn($className, $fieldName, $dqlAlias);
         }
         $discrColumn = $this->_rsm->metaMappings[$fieldName];
         if (!isset($data[$discrColumn])) {
             throw HydrationException::missingDiscriminatorColumn($className, $discrColumn, $dqlAlias);
         }
         if ($data[$discrColumn] === "") {
             throw HydrationException::emptyDiscriminatorValue($dqlAlias);
         }
         $discrMap = $this->_metadataCache[$className]->discriminatorMap;
         if (!isset($discrMap[$data[$discrColumn]])) {
             throw HydrationException::invalidDiscriminatorValue($data[$discrColumn], array_keys($discrMap));
         }
         $className = $discrMap[$data[$discrColumn]];
         unset($data[$discrColumn]);
     }
     if (isset($this->_hints[Query::HINT_REFRESH_ENTITY]) && isset($this->rootAliases[$dqlAlias])) {
         $this->registerManaged($this->_metadataCache[$className], $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
     }
     $this->_hints['fetchAlias'] = $dqlAlias;
     return $this->_uow->createEntity($className, $data, $this->_hints);
 }
Example #8
0
 /**
  * Gets an entity instance.
  *
  * @param $data The instance data.
  * @param $dqlAlias The DQL alias of the entity's class.
  * @return object The entity.
  */
 private function _getEntity(array $data, $dqlAlias)
 {
     $className = $this->_rsm->aliasMap[$dqlAlias];
     if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
         $discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
         if ($data[$discrColumn] === "") {
             throw HydrationException::emptyDiscriminatorValue($dqlAlias);
         }
         $className = $this->_ce[$className]->discriminatorMap[$data[$discrColumn]];
         unset($data[$discrColumn]);
     }
     if (isset($this->_hints[Query::HINT_REFRESH_ENTITY]) && isset($this->_rootAliases[$dqlAlias])) {
         $this->registerManaged($this->_ce[$className], $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
     }
     $this->_hints['fetchAlias'] = $dqlAlias;
     return $this->_uow->createEntity($className, $data, $this->_hints);
 }
Example #9
0
 /**
  * Gets an entity instance.
  *
  * @param $data The instance data.
  * @param $dqlAlias The DQL alias of the entity's class.
  * @return object The entity.
  */
 private function _getEntity(array $data, $dqlAlias)
 {
     $className = $this->_rsm->aliasMap[$dqlAlias];
     if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
         $discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
         if ($data[$discrColumn] === "") {
             throw HydrationException::emptyDiscriminatorValue($dqlAlias);
         }
         $className = $this->_ce[$className]->discriminatorMap[$data[$discrColumn]];
         unset($data[$discrColumn]);
     }
     if (isset($this->_hints[Query::HINT_REFRESH_ENTITY]) && isset($this->_rootAliases[$dqlAlias])) {
         $this->registerManaged($this->_ce[$className], $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
     }
     $this->_hints['fetchAlias'] = $dqlAlias;
     $entity = $this->_uow->createEntity($className, $data, $this->_hints);
     //TODO: These should be invoked later, after hydration, because associations may not yet be loaded here.
     if (isset($this->_ce[$className]->lifecycleCallbacks[Events::postLoad])) {
         $this->_ce[$className]->invokeLifecycleCallbacks(Events::postLoad, $entity);
     }
     $evm = $this->_em->getEventManager();
     if ($evm->hasListeners(Events::postLoad)) {
         $evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->_em));
     }
     return $entity;
 }