2009-01-14 00:56:43 +03:00
|
|
|
<?php
|
2009-02-07 20:02:13 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2009-01-15 16:30:44 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Internal\Hydration;
|
|
|
|
|
2009-05-29 19:38:46 +04:00
|
|
|
use Doctrine\DBAL\Connection;
|
2009-02-17 13:54:18 +03:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
2009-07-21 13:25:14 +04:00
|
|
|
use Doctrine\ORM\Query;
|
2009-04-09 22:19:14 +04:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2009-01-14 00:56:43 +03:00
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* The ObjectHydrator constructs an object graph out of an SQL result set.
|
2009-01-14 00:56:43 +03:00
|
|
|
*
|
2009-05-13 19:19:27 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-02-07 20:02:13 +03:00
|
|
|
* @since 2.0
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class ObjectHydrator extends AbstractHydrator
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
/*
|
|
|
|
* These two properties maintain their values between hydration runs.
|
|
|
|
*/
|
2009-05-17 23:27:12 +04:00
|
|
|
/* Class entries */
|
|
|
|
private $_ce = array();
|
2009-05-14 22:34:12 +04:00
|
|
|
private $_discriminatorMap = array();
|
|
|
|
/*
|
|
|
|
* The following parts are reinitialized on every hydration run.
|
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_isSimpleQuery = false;
|
2009-05-13 19:19:27 +04:00
|
|
|
private $_allowPartialObjects = false;
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_identifierMap = array();
|
|
|
|
private $_resultPointers = array();
|
|
|
|
private $_idTemplate = array();
|
|
|
|
private $_resultCounter = 0;
|
2009-05-14 22:34:12 +04:00
|
|
|
private $_rootAliases = array();
|
2009-05-13 19:19:27 +04:00
|
|
|
private $_fetchedAssociations = array();
|
2009-05-14 22:34:12 +04:00
|
|
|
/* TODO: Consider unifying _collections and _initializedRelations */
|
|
|
|
/** Collections initialized by the hydrator */
|
|
|
|
private $_collections = array();
|
|
|
|
/** Memory for initialized relations */
|
|
|
|
private $_initializedRelations = array();
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/** @override */
|
2009-04-12 23:02:12 +04:00
|
|
|
protected function _prepare()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->_isSimpleQuery = count($this->_rsm->aliasMap) <= 1;
|
2009-07-21 13:25:14 +04:00
|
|
|
$this->_allowPartialObjects = $this->_em->getConfiguration()->getAllowPartialObjects()
|
|
|
|
|| isset($this->_hints[Query::HINT_FORCE_PARTIAL_LOAD]);
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_identifierMap = array();
|
|
|
|
$this->_resultPointers = array();
|
|
|
|
$this->_idTemplate = array();
|
|
|
|
$this->_resultCounter = 0;
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->_fetchedAssociations = array();
|
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_identifierMap[$dqlAlias] = array();
|
|
|
|
$this->_resultPointers[$dqlAlias] = array();
|
|
|
|
$this->_idTemplate[$dqlAlias] = '';
|
2009-05-21 23:18:14 +04:00
|
|
|
$class = $this->_em->getClassMetadata($className);
|
2009-05-14 22:34:12 +04:00
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
if ( ! isset($this->_ce[$class->name])) {
|
|
|
|
$this->_ce[$class->name] = $class;
|
2009-05-14 22:34:12 +04:00
|
|
|
// Gather class descriptors and discriminator values of subclasses, if necessary
|
|
|
|
if ($class->isInheritanceTypeSingleTable() || $class->isInheritanceTypeJoined()) {
|
|
|
|
$this->_discriminatorMap[$class->name][$class->discriminatorValue] = $class->name;
|
|
|
|
foreach (array_merge($class->parentClasses, $class->subClasses) as $className) {
|
|
|
|
$otherClass = $this->_em->getClassMetadata($className);
|
|
|
|
$value = $otherClass->discriminatorValue;
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_ce[$className] = $otherClass;
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->_discriminatorMap[$class->name][$value] = $className;
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_discriminatorMap[$className][$value] = $className;
|
2009-05-14 22:34:12 +04:00
|
|
|
}
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
// Remember which associations are "fetch joined"
|
2009-05-14 22:34:12 +04:00
|
|
|
if (isset($this->_rsm->relationMap[$dqlAlias])) {
|
|
|
|
$assoc = $this->_rsm->relationMap[$dqlAlias];
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_fetchedAssociations[$assoc->sourceEntityName][$assoc->sourceFieldName] = true;
|
|
|
|
if ($assoc->mappedByFieldName) {
|
|
|
|
$this->_fetchedAssociations[$assoc->targetEntityName][$assoc->mappedByFieldName] = true;
|
2009-05-26 15:30:07 +04:00
|
|
|
} else {
|
2009-05-26 15:31:29 +04:00
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
2009-05-26 15:30:07 +04:00
|
|
|
if (isset($targetClass->inverseMappings[$assoc->sourceFieldName])) {
|
2009-05-26 15:31:29 +04:00
|
|
|
$inverseAssoc = $targetClass->inverseMappings[$assoc->sourceFieldName];
|
|
|
|
$this->_fetchedAssociations[$assoc->targetEntityName][$inverseAssoc->sourceFieldName] = true;
|
2009-05-26 15:30:07 +04:00
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:30:44 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _hydrateAll()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-05-17 23:27:12 +04:00
|
|
|
$result = $this->_rsm->isMixed ? array() : new Collection;
|
2009-01-14 00:56:43 +03:00
|
|
|
|
|
|
|
$cache = array();
|
2009-05-29 19:38:46 +04:00
|
|
|
while ($data = $this->_stmt->fetch(Connection::FETCH_ASSOC)) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_hydrateRow($data, $cache, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take snapshots from all initialized collections
|
|
|
|
foreach ($this->_collections as $coll) {
|
2009-02-07 20:02:13 +03:00
|
|
|
$coll->takeSnapshot();
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
$this->_collections = array();
|
|
|
|
$this->_initializedRelations = array();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-13 19:19:27 +04:00
|
|
|
* Updates the result pointer for an entity. The result pointers point to the
|
|
|
|
* last seen instance of each entity type. This is used for graph construction.
|
2009-01-14 00:56:43 +03:00
|
|
|
*
|
|
|
|
* @param Collection $coll The element.
|
|
|
|
* @param boolean|integer $index Index of the element in the collection.
|
|
|
|
* @param string $dqlAlias
|
2009-05-17 23:27:12 +04:00
|
|
|
* @todo May be worth to try to inline this method (through first reducing the
|
|
|
|
* calls of this method to 1).
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
2009-05-13 19:19:27 +04:00
|
|
|
private function updateResultPointer(&$coll, $index, $dqlAlias)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
|
|
|
if ($coll === null) {
|
|
|
|
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($index !== false) {
|
|
|
|
$this->_resultPointers[$dqlAlias] = $coll[$index];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_object($coll)) {
|
|
|
|
end($coll);
|
|
|
|
$this->_resultPointers[$dqlAlias] =& $coll[key($coll)];
|
2009-04-09 22:19:14 +04:00
|
|
|
} else if ($coll instanceof Collection) {
|
2009-01-14 00:56:43 +03:00
|
|
|
if (count($coll) > 0) {
|
|
|
|
$this->_resultPointers[$dqlAlias] = $coll->last();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->_resultPointers[$dqlAlias] = $coll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
/**
|
2009-07-20 14:52:07 +04:00
|
|
|
* Initializes a related collection.
|
2009-04-12 23:02:12 +04:00
|
|
|
*
|
2009-07-20 14:52:07 +04:00
|
|
|
* @param object $entity The entity to which the collection belongs.
|
|
|
|
* @param string $name The name of the field on the entity that holds the collection.
|
2009-07-28 15:43:42 +04:00
|
|
|
* @return PersistentCollection
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private function initRelatedCollection($entity, $name)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
2009-05-17 23:27:12 +04:00
|
|
|
$classMetadata = $this->_ce[get_class($entity)];
|
2009-05-07 20:36:27 +04:00
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
$relation = $classMetadata->associationMappings[$name];
|
2009-07-20 14:52:07 +04:00
|
|
|
if ( ! isset($this->_ce[$relation->targetEntityName])) {
|
|
|
|
$this->_ce[$relation->targetEntityName] = $this->_em->getClassMetadata($relation->targetEntityName);
|
|
|
|
}
|
|
|
|
$coll = new PersistentCollection($this->_em, $this->_ce[$relation->targetEntityName]);
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_collections[] = $coll;
|
2009-05-07 20:36:27 +04:00
|
|
|
$coll->setOwner($entity, $relation);
|
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
$classMetadata->reflFields[$name]->setValue($entity, $coll);
|
2009-05-07 20:36:27 +04:00
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $name, $coll);
|
|
|
|
$this->_initializedRelations[$oid][$name] = true;
|
2009-07-28 15:43:42 +04:00
|
|
|
return $coll;
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
2009-05-13 19:19:27 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param <type> $entity
|
|
|
|
* @param <type> $assocField
|
|
|
|
* @param <type> $indexField
|
|
|
|
* @return <type>
|
2009-07-20 14:52:07 +04:00
|
|
|
* @todo Inline this method.
|
2009-05-13 19:19:27 +04:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private function isIndexKeyInUse($entity, $assocField, $indexField)
|
|
|
|
{
|
2009-05-17 23:27:12 +04:00
|
|
|
return $this->_ce[get_class($entity)]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$assocField]
|
2009-04-09 22:12:48 +04:00
|
|
|
->getValue($entity)
|
|
|
|
->containsKey($indexField);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param <type> $coll
|
|
|
|
* @return <type>
|
|
|
|
* @todo Consider inlining this method, introducing $coll->lastKey().
|
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private function getLastKey($coll)
|
|
|
|
{
|
2009-01-15 16:30:44 +03:00
|
|
|
// Check needed because of mixed results.
|
2009-01-14 00:56:43 +03:00
|
|
|
// is_object instead of is_array because is_array is slow on large arrays.
|
|
|
|
if (is_object($coll)) {
|
|
|
|
$coll->last();
|
|
|
|
return $coll->key();
|
|
|
|
} else {
|
|
|
|
end($coll);
|
|
|
|
return key($coll);
|
|
|
|
}
|
|
|
|
}
|
2009-07-21 13:25:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an entity instance.
|
|
|
|
*
|
|
|
|
* @param $data The instance data.
|
|
|
|
* @param $dqlAlias The DQL alias of the entity's class.
|
|
|
|
* @return object The entity.
|
|
|
|
*/
|
2009-05-26 15:30:07 +04:00
|
|
|
private function getEntity(array $data, $dqlAlias)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-05-26 15:30:07 +04:00
|
|
|
$className = $this->_rsm->aliasMap[$dqlAlias];
|
|
|
|
if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
|
2009-07-20 19:30:54 +04:00
|
|
|
$discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
|
2009-04-12 23:02:12 +04:00
|
|
|
$className = $this->_discriminatorMap[$className][$data[$discrColumn]];
|
|
|
|
unset($data[$discrColumn]);
|
|
|
|
}
|
2009-07-21 13:25:14 +04:00
|
|
|
$entity = $this->_uow->createEntity($className, $data, $this->_hints);
|
2009-05-13 19:19:27 +04:00
|
|
|
|
|
|
|
// Properly initialize any unfetched associations, if partial objects are not allowed.
|
|
|
|
if ( ! $this->_allowPartialObjects) {
|
2009-05-17 23:27:12 +04:00
|
|
|
foreach ($this->_ce[$className]->associationMappings as $field => $assoc) {
|
2009-05-13 19:19:27 +04:00
|
|
|
if ( ! isset($this->_fetchedAssociations[$className][$field])) {
|
|
|
|
if ($assoc->isOneToOne()) {
|
2009-07-21 13:25:14 +04:00
|
|
|
$joinColumns = array();
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
|
|
|
|
$joinColumns[$srcColumn] = $data[$assoc->joinColumnFieldNames[$srcColumn]];
|
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
if ($assoc->isLazilyFetched()) {
|
|
|
|
// Inject proxy
|
2009-07-21 13:25:14 +04:00
|
|
|
$this->_ce[$className]->reflFields[$field]->setValue($entity,
|
|
|
|
$this->_em->getProxyFactory()->getAssociationProxy($entity, $assoc, $joinColumns));
|
2009-05-13 19:19:27 +04:00
|
|
|
} else {
|
2009-07-21 13:25:14 +04:00
|
|
|
// Eager load
|
|
|
|
//TODO: Allow more efficient and configurable batching of these loads
|
2009-07-21 19:53:58 +04:00
|
|
|
$assoc->load($entity, new $assoc->targetEntityName, $this->_em, $joinColumns);
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Inject collection
|
2009-07-28 15:43:42 +04:00
|
|
|
$collection = $this->initRelatedCollection($entity, $field);
|
|
|
|
if ($assoc->isLazilyFetched()) {
|
|
|
|
$collection->setLazyInitialization();
|
|
|
|
} else {
|
|
|
|
//TODO: Allow more efficient and configurable batching of these loads
|
|
|
|
$assoc->load($entity, $collection, $this->_em);
|
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a related element.
|
|
|
|
*
|
2009-05-07 20:36:27 +04:00
|
|
|
* @param object $entity1
|
|
|
|
* @param string $property
|
|
|
|
* @param object $entity2
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
|
|
|
private function setRelatedElement($entity1, $property, $entity2)
|
|
|
|
{
|
2009-05-28 15:13:12 +04:00
|
|
|
$class = $this->_ce[get_class($entity1)];
|
|
|
|
$class->reflFields[$property]->setValue($entity1, $entity2);
|
2009-05-13 19:19:27 +04:00
|
|
|
$this->_uow->setOriginalEntityProperty(spl_object_hash($entity1), $property, $entity2);
|
2009-05-28 15:13:12 +04:00
|
|
|
$relation = $class->associationMappings[$property];
|
2009-07-21 13:25:14 +04:00
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
if ($relation->isOneToOne()) {
|
2009-05-17 23:27:12 +04:00
|
|
|
$targetClass = $this->_ce[$relation->targetEntityName];
|
|
|
|
if ($relation->isOwningSide) {
|
2009-01-14 00:56:43 +03:00
|
|
|
// If there is an inverse mapping on the target class its bidirectional
|
2009-05-14 22:34:12 +04:00
|
|
|
if (isset($targetClass->inverseMappings[$property])) {
|
2009-05-26 19:42:54 +04:00
|
|
|
$sourceProp = $targetClass->inverseMappings[$property]->sourceFieldName;
|
2009-05-14 14:03:09 +04:00
|
|
|
$targetClass->reflFields[$sourceProp]->setValue($entity2, $entity1);
|
2009-07-18 15:41:37 +04:00
|
|
|
} else if ($class === $targetClass && $relation->mappedByFieldName) {
|
|
|
|
// Special case: bi-directional self-referencing one-one on the same class
|
2009-05-26 19:42:54 +04:00
|
|
|
$targetClass->reflFields[$property]->setValue($entity2, $entity1);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
} else {
|
2009-05-26 19:42:54 +04:00
|
|
|
// For sure bidirectional, as there is no inverse side in unidirectional mappings
|
2009-05-14 22:34:12 +04:00
|
|
|
$targetClass->reflFields[$relation->mappedByFieldName]->setValue($entity2, $entity1);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-15 16:30:44 +03:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2009-01-14 00:56:43 +03:00
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _hydrateRow(array &$data, array &$cache, &$result)
|
|
|
|
{
|
|
|
|
// 1) Initialize
|
|
|
|
$id = $this->_idTemplate; // initialize the id-memory
|
|
|
|
$nonemptyComponents = array();
|
2009-01-15 16:30:44 +03:00
|
|
|
$rowData = $this->_gatherRowData($data, $cache, $id, $nonemptyComponents);
|
2009-01-14 00:56:43 +03:00
|
|
|
|
|
|
|
// Extract scalar values. They're appended at the end.
|
|
|
|
if (isset($rowData['scalars'])) {
|
|
|
|
$scalars = $rowData['scalars'];
|
|
|
|
unset($rowData['scalars']);
|
|
|
|
}
|
|
|
|
|
2009-05-07 20:36:27 +04:00
|
|
|
// Hydrate the entity data found in the current row.
|
2009-01-14 00:56:43 +03:00
|
|
|
foreach ($rowData as $dqlAlias => $data) {
|
|
|
|
$index = false;
|
2009-05-21 23:18:14 +04:00
|
|
|
$entityName = $this->_rsm->aliasMap[$dqlAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-05-14 22:34:12 +04:00
|
|
|
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) {
|
2009-04-09 22:12:48 +04:00
|
|
|
// It's a joined result
|
|
|
|
|
2009-05-14 22:34:12 +04:00
|
|
|
$parent = $this->_rsm->parentAliasMap[$dqlAlias];
|
|
|
|
$relation = $this->_rsm->relationMap[$dqlAlias];
|
|
|
|
$relationAlias = $relation->sourceFieldName;
|
2009-04-09 22:12:48 +04:00
|
|
|
|
|
|
|
// Get a reference to the right element in the result tree.
|
|
|
|
// This element will get the associated element attached.
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parent])) {
|
2009-05-21 23:18:14 +04:00
|
|
|
$first = reset($this->_resultPointers);
|
2009-05-24 14:38:37 +04:00
|
|
|
// TODO: Exception if key($first) === null ?
|
2009-05-21 23:18:14 +04:00
|
|
|
$baseElement = $this->_resultPointers[$parent][key($first)];
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if (isset($this->_resultPointers[$parent])) {
|
2009-05-17 23:27:12 +04:00
|
|
|
$baseElement = $this->_resultPointers[$parent];
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
|
|
|
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
$parentClass = get_class($baseElement);
|
2009-04-09 22:12:48 +04:00
|
|
|
|
|
|
|
// Check the type of the relation (many or single-valued)
|
|
|
|
if ( ! $relation->isOneToOne()) {
|
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
2009-05-07 20:36:27 +04:00
|
|
|
if ( ! isset($this->_initializedRelations[spl_object_hash($baseElement)][$relationAlias])) {
|
2009-07-02 15:48:44 +04:00
|
|
|
$this->initRelatedCollection($baseElement, $relationAlias);
|
2009-05-07 20:36:27 +04:00
|
|
|
}
|
|
|
|
|
2009-05-13 19:19:27 +04:00
|
|
|
$path = $parent . '.' . $dqlAlias;
|
2009-04-09 22:12:48 +04:00
|
|
|
$indexExists = isset($this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]]);
|
|
|
|
$index = $indexExists ? $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] : false;
|
|
|
|
$indexIsValid = $index !== false ? $this->isIndexKeyInUse($baseElement, $relationAlias, $index) : false;
|
2009-07-15 10:46:43 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! $indexExists || ! $indexIsValid) {
|
2009-05-26 15:30:07 +04:00
|
|
|
$element = $this->getEntity($data, $dqlAlias);
|
2009-05-07 20:36:27 +04:00
|
|
|
|
|
|
|
// If it's a bi-directional many-to-many, also initialize the reverse collection.
|
|
|
|
if ($relation->isManyToMany()) {
|
2009-05-17 23:27:12 +04:00
|
|
|
if ($relation->isOwningSide && isset($this->_ce[$entityName]->inverseMappings[$relationAlias])) {
|
2009-07-02 15:48:44 +04:00
|
|
|
$inverseFieldName = $this->_ce[$entityName]->inverseMappings[$relationAlias]->sourceFieldName;
|
|
|
|
// Only initialize reverse collection if it is not yet initialized.
|
|
|
|
if ( ! isset($this->_initializedRelations[spl_object_hash($element)][$inverseFieldName])) {
|
|
|
|
$this->initRelatedCollection($element, $this->_ce[$entityName]
|
|
|
|
->inverseMappings[$relationAlias]->sourceFieldName);
|
|
|
|
}
|
2009-05-17 23:27:12 +04:00
|
|
|
} else if ($relation->mappedByFieldName) {
|
2009-07-02 15:48:44 +04:00
|
|
|
// Only initialize reverse collection if it is not yet initialized.
|
|
|
|
if ( ! isset($this->_initializedRelations[spl_object_hash($element)][$relation->mappedByFieldName])) {
|
|
|
|
$this->initRelatedCollection($element, $relation->mappedByFieldName);
|
|
|
|
}
|
2009-05-07 20:36:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-20 14:52:07 +04:00
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
|
|
|
$field = $this->_rsm->indexByMap[$dqlAlias];
|
2009-05-17 23:27:12 +04:00
|
|
|
$indexValue = $this->_ce[$entityName]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$field]
|
2009-05-07 20:36:27 +04:00
|
|
|
->getValue($element);
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_ce[$parentClass]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$relationAlias]
|
2009-05-07 20:36:27 +04:00
|
|
|
->getValue($baseElement)
|
2009-07-02 15:48:44 +04:00
|
|
|
->hydrateSet($indexValue, $element);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_ce[$parentClass]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$relationAlias]
|
2009-05-07 20:36:27 +04:00
|
|
|
->getValue($baseElement)
|
2009-07-02 15:48:44 +04:00
|
|
|
->hydrateAdd($element);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-07-20 14:52:07 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = $this->getLastKey(
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_ce[$parentClass]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$relationAlias]
|
2009-04-12 23:02:12 +04:00
|
|
|
->getValue($baseElement)
|
|
|
|
);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
2009-05-17 23:27:12 +04:00
|
|
|
} else if ( ! $this->_ce[$parentClass]->reflFields[$relationAlias]->getValue($baseElement)) {
|
|
|
|
$coll = new PersistentCollection($this->_em, $this->_ce[$entityName]);
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_collections[] = $coll;
|
|
|
|
$this->setRelatedElement($baseElement, $relationAlias, $coll);
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
if ( ! $this->_ce[$parentClass]->reflFields[$relationAlias]->getValue($baseElement)) {
|
2009-07-15 10:46:43 +04:00
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
2009-05-26 15:30:07 +04:00
|
|
|
$this->setRelatedElement($baseElement, $relationAlias, $this->getEntity($data, $dqlAlias));
|
2009-05-17 23:27:12 +04:00
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 10:46:43 +04:00
|
|
|
$coll = $this->_ce[$parentClass]->reflFields[$relationAlias]->getValue($baseElement);
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($coll !== null) {
|
2009-05-07 20:36:27 +04:00
|
|
|
$this->updateResultPointer($coll, $index, $dqlAlias);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Its a root result element
|
|
|
|
|
|
|
|
$this->_rootAliases[$dqlAlias] = true; // Mark as root alias
|
|
|
|
|
|
|
|
if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
|
2009-05-26 15:30:07 +04:00
|
|
|
$element = $this->getEntity($rowData[$dqlAlias], $dqlAlias);
|
2009-07-20 14:52:07 +04:00
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
|
|
|
$field = $this->_rsm->indexByMap[$dqlAlias];
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$result[] = array(
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_ce[$entityName]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$field]
|
2009-04-09 22:12:48 +04:00
|
|
|
->getValue($element) => $element
|
|
|
|
);
|
|
|
|
++$this->_resultCounter;
|
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
$result->set($element, $this->_ce[$entityName]
|
2009-05-14 14:03:09 +04:00
|
|
|
->reflFields[$field]
|
2009-04-09 22:12:48 +04:00
|
|
|
->getValue($element));
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$result[] = array($element);
|
|
|
|
++$this->_resultCounter;
|
|
|
|
} else {
|
|
|
|
$result->add($element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $this->getLastKey($result);
|
|
|
|
} else {
|
|
|
|
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
|
|
|
|
}
|
2009-05-07 20:36:27 +04:00
|
|
|
$this->updateResultPointer($result, $index, $dqlAlias);
|
2009-04-09 22:12:48 +04:00
|
|
|
//unset($rowData[$dqlAlias]);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append scalar values to mixed result sets
|
|
|
|
if (isset($scalars)) {
|
|
|
|
foreach ($scalars as $name => $value) {
|
|
|
|
$result[$this->_resultCounter - 1][$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
|
|
|
protected function _getRowContainer()
|
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
return new \Doctrine\Common\Collections\Collection;
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|