2011-03-15 21:48:04 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2011-03-15 21:48:04 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Internal\Hydration;
|
|
|
|
|
2012-12-13 21:56:25 +04:00
|
|
|
use PDO;
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\DBAL\Types\Type;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\ORM\Query;
|
2011-03-15 21:48:04 +03:00
|
|
|
|
|
|
|
class SimpleObjectHydrator extends AbstractHydrator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ClassMetadata
|
|
|
|
*/
|
|
|
|
private $class;
|
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-15 22:03:05 +03:00
|
|
|
private $declaringClasses = array();
|
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function hydrateAllData()
|
2011-03-15 21:48:04 +03:00
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
$cache = array();
|
|
|
|
|
|
|
|
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
|
2011-11-03 04:08:24 +04:00
|
|
|
$this->hydrateRowData($row, $cache, $result);
|
2011-03-15 21:48:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->getUnitOfWork()->triggerEagerLoads();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function prepare()
|
2011-03-15 21:48:04 +03:00
|
|
|
{
|
2011-11-03 04:08:24 +04:00
|
|
|
if (count($this->_rsm->aliasMap) !== 1) {
|
|
|
|
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.");
|
2011-03-15 21:48:04 +03:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-03-15 21:48:04 +03:00
|
|
|
if ($this->_rsm->scalarMappings) {
|
|
|
|
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.");
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
$this->class = $this->_em->getClassMetadata(reset($this->_rsm->aliasMap));
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
// We only need to add declaring classes if we have inheritance.
|
|
|
|
if ($this->class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($this->_rsm->declaringClasses as $column => $class) {
|
2011-11-03 04:08:24 +04:00
|
|
|
$this->declaringClasses[$column] = $this->_em->getClassMetadata($class);
|
|
|
|
}
|
2011-03-15 21:48:04 +03:00
|
|
|
}
|
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function hydrateRowData(array $sqlResult, array &$cache, array &$result)
|
2011-03-15 21:48:04 +03:00
|
|
|
{
|
2011-11-03 04:08:24 +04:00
|
|
|
$entityName = $this->class->name;
|
|
|
|
$data = array();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
// We need to find the correct entity class name if we have inheritance in resultset
|
|
|
|
if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
|
2011-03-15 21:48:04 +03:00
|
|
|
$discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-03-16 05:38:54 +04:00
|
|
|
if ( ! isset($sqlResult[$discrColumnName])) {
|
2012-03-16 04:29:07 +04:00
|
|
|
throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
|
|
|
|
}
|
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
if ($sqlResult[$discrColumnName] === '') {
|
2011-11-01 02:35:41 +04:00
|
|
|
throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
|
|
|
|
}
|
|
|
|
|
2011-03-15 21:48:04 +03:00
|
|
|
$entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]];
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-03-15 21:48:04 +03:00
|
|
|
unset($sqlResult[$discrColumnName]);
|
2011-11-03 04:08:24 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
foreach ($sqlResult as $column => $value) {
|
|
|
|
// Hydrate column information if not yet present
|
|
|
|
if ( ! isset($cache[$column])) {
|
|
|
|
if (($info = $this->hydrateColumnInfo($entityName, $column)) === null) {
|
|
|
|
continue;
|
2011-03-15 23:34:47 +03:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
$cache[$column] = $info;
|
|
|
|
}
|
2011-03-15 23:34:47 +03:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
// Convert field to a valid PHP value
|
2013-06-11 21:44:08 +04:00
|
|
|
if (isset($cache[$column]['type'])) {
|
|
|
|
$value = Type::getType($cache[$column]['type'])->convertToPHPValue($value, $this->_platform);
|
2011-11-03 04:08:24 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
// 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;
|
2011-03-15 21:48:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-05 01:19:08 +04:00
|
|
|
if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) {
|
|
|
|
$this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
|
2011-03-15 21:48:04 +03:00
|
|
|
}
|
|
|
|
|
2011-11-29 19:36:32 +04:00
|
|
|
$uow = $this->_em->getUnitOfWork();
|
|
|
|
$entity = $uow->createEntity($entityName, $data, $this->_hints);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-29 19:36:32 +04:00
|
|
|
$result[] = $entity;
|
2011-03-15 21:48:04 +03:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
/**
|
|
|
|
* Retrieve column information form ResultSetMapping.
|
2011-12-20 01:56:19 +04:00
|
|
|
*
|
2011-11-03 04:08:24 +04:00
|
|
|
* @param string $entityName
|
|
|
|
* @param string $column
|
2011-12-20 01:56:19 +04:00
|
|
|
*
|
|
|
|
* @return array
|
2011-11-03 04:08:24 +04:00
|
|
|
*/
|
|
|
|
protected function hydrateColumnInfo($entityName, $column)
|
|
|
|
{
|
|
|
|
|
2013-06-11 21:44:08 +04:00
|
|
|
if (isset($this->_rsm->fieldMappings[$column])) {
|
2013-06-12 18:29:17 +04:00
|
|
|
$name = $this->_rsm->fieldMappings[$column];
|
|
|
|
$class = isset($this->declaringClasses[$column])
|
2013-06-11 21:44:08 +04:00
|
|
|
? $this->declaringClasses[$column]
|
|
|
|
: $this->class;
|
2011-11-03 04:08:24 +04:00
|
|
|
|
2013-06-11 21:44:08 +04:00
|
|
|
// If class is not part of the inheritance, ignore
|
|
|
|
if ( ! ($class->name === $entityName || is_subclass_of($entityName, $class->name))) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-11-03 04:08:24 +04:00
|
|
|
|
2013-06-11 21:44:08 +04:00
|
|
|
return array(
|
|
|
|
'name' => $name,
|
|
|
|
'type' => $class->fieldMappings[$name]['type']
|
|
|
|
);
|
|
|
|
}
|
2011-11-03 04:08:24 +04:00
|
|
|
|
2013-06-11 21:44:08 +04:00
|
|
|
if (isset($this->_rsm->metaMappings[$column])) {
|
|
|
|
return array(
|
2013-06-11 22:23:50 +04:00
|
|
|
'name' => $this->_rsm->metaMappings[$column],
|
|
|
|
'type' => (isset($this->_rsm->typeMappings[$column]) ? $this->_rsm->typeMappings[$column] : null)
|
2013-06-11 21:44:08 +04:00
|
|
|
);
|
2011-11-03 04:08:24 +04:00
|
|
|
}
|
2013-06-11 21:44:08 +04:00
|
|
|
|
2013-06-12 18:29:17 +04:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2013-06-11 21:44:08 +04:00
|
|
|
return null;
|
2011-11-03 04:08:24 +04:00
|
|
|
}
|
2012-02-18 02:26:42 +04:00
|
|
|
}
|