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-08-28 14:48:40 +04:00
|
|
|
use Doctrine\ORM\PersistentCollection,
|
2009-07-29 15:57:27 +04:00
|
|
|
Doctrine\ORM\Query,
|
|
|
|
Doctrine\Common\Collections\ArrayCollection,
|
2009-07-29 16:00:08 +04:00
|
|
|
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-10-09 18:27:35 +04:00
|
|
|
* @internal Highly performance-sensitive code.
|
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-08-28 14:48:40 +04:00
|
|
|
/* Local ClassMetadata cache to avoid going to the EntityManager all the time.
|
|
|
|
* This local cache is maintained between hydration runs and not cleared.
|
2009-05-14 22:34:12 +04:00
|
|
|
*/
|
2009-05-17 23:27:12 +04:00
|
|
|
private $_ce = array();
|
2009-08-28 14:48:40 +04:00
|
|
|
|
|
|
|
/* The following parts are reinitialized on every hydration run. */
|
|
|
|
|
2009-05-13 19:19:27 +04:00
|
|
|
private $_allowPartialObjects = false;
|
2009-08-28 14:48:40 +04:00
|
|
|
private $_identifierMap;
|
|
|
|
private $_resultPointers;
|
|
|
|
private $_idTemplate;
|
2009-07-28 20:36:24 +04:00
|
|
|
private $_resultCounter;
|
|
|
|
private $_fetchedAssociations;
|
2009-08-28 14:48:40 +04:00
|
|
|
private $_rootAliases = array();
|
2009-07-29 15:57:27 +04:00
|
|
|
private $_initializedCollections = array();
|
2009-10-09 18:27:35 +04:00
|
|
|
private $_existingCollections = array();
|
2009-10-06 14:04:32 +04:00
|
|
|
private $_proxyFactory;
|
2009-10-09 18:27:35 +04:00
|
|
|
//private $_createdEntities;
|
|
|
|
|
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-07-21 13:25:14 +04:00
|
|
|
$this->_allowPartialObjects = $this->_em->getConfiguration()->getAllowPartialObjects()
|
|
|
|
|| isset($this->_hints[Query::HINT_FORCE_PARTIAL_LOAD]);
|
2009-07-28 20:36:24 +04:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
$this->_proxyFactory = $this->_em->getProxyFactory();
|
2009-10-06 14:04:32 +04:00
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->_identifierMap =
|
|
|
|
$this->_resultPointers =
|
|
|
|
$this->_idTemplate =
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->_fetchedAssociations = array();
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->_resultCounter = 0;
|
2009-05-14 22:34:12 +04:00
|
|
|
|
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-07-29 15:57:27 +04:00
|
|
|
if ( ! isset($this->_ce[$className])) {
|
|
|
|
$this->_ce[$className] = $class;
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
// Remember which associations are "fetch joined", so that we know where to inject
|
|
|
|
// collection stubs or proxies and where not.
|
2009-05-14 22:34:12 +04:00
|
|
|
if (isset($this->_rsm->relationMap[$dqlAlias])) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$targetClassName = $this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]];
|
2009-10-09 18:27:35 +04:00
|
|
|
$targetClass = $this->_getClassMetadata($targetClassName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_ce[$targetClassName] = $targetClass;
|
|
|
|
$assoc = $targetClass->associationMappings[$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-08-11 14:51:38 +04:00
|
|
|
if (isset($targetClass->inverseMappings[$className][$assoc->sourceFieldName])) {
|
|
|
|
$inverseAssoc = $targetClass->inverseMappings[$className][$assoc->sourceFieldName];
|
2009-05-26 15:31:29 +04:00
|
|
|
$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-08-11 14:51:38 +04:00
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-10-09 18:27:35 +04:00
|
|
|
/*@override*/ protected function _cleanup()
|
2009-08-11 14:51:38 +04:00
|
|
|
{
|
|
|
|
parent::_cleanup();
|
|
|
|
$this->_identifierMap =
|
2009-10-09 18:27:35 +04:00
|
|
|
$this->_initializedCollections =
|
|
|
|
$this->_existingCollections =
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_resultPointers = array();
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-01-15 16:30:44 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-10-09 18:27:35 +04:00
|
|
|
/*@override*/ protected function _hydrateAll()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-08-03 17:25:56 +04:00
|
|
|
$result = array();
|
2009-01-14 00:56:43 +03:00
|
|
|
$cache = array();
|
2009-08-28 14:48:40 +04:00
|
|
|
while ($data = $this->_stmt->fetch(\Doctrine\DBAL\Connection::FETCH_ASSOC)) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_hydrateRow($data, $cache, $result);
|
|
|
|
}
|
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
// Take snapshots from all newly initialized collections
|
2009-07-29 15:57:27 +04:00
|
|
|
foreach ($this->_initializedCollections as $coll) {
|
2009-02-07 20:02:13 +03:00
|
|
|
$coll->takeSnapshot();
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
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-04-12 23:02:12 +04:00
|
|
|
*/
|
2009-08-28 14:48:40 +04:00
|
|
|
private function _initRelatedCollection($entity, $name)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
2009-07-28 20:36:24 +04:00
|
|
|
$class = $this->_ce[get_class($entity)];
|
|
|
|
$relation = $class->associationMappings[$name];
|
|
|
|
|
2009-09-06 19:25:23 +04:00
|
|
|
$value = $class->reflFields[$name]->getValue($entity);
|
|
|
|
if ($value === null) {
|
|
|
|
$value = new ArrayCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value instanceof ArrayCollection) {
|
|
|
|
$value = new PersistentCollection(
|
|
|
|
$this->_em,
|
|
|
|
$this->_ce[$relation->targetEntityName],
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
$value->setOwner($entity, $relation);
|
2009-10-09 18:27:35 +04:00
|
|
|
$class->reflFields[$name]->setValue($entity, $value);
|
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $name, $value);
|
|
|
|
$this->_initializedCollections[$oid . $name] = $value;
|
|
|
|
} else if (isset($this->_hints[Query::HINT_REFRESH])) {
|
|
|
|
// Is already PersistentCollection, but REFRESH
|
2009-09-06 19:25:23 +04:00
|
|
|
$value->clear();
|
|
|
|
$value->setDirty(false);
|
|
|
|
$value->setInitialized(true);
|
2009-10-09 18:27:35 +04:00
|
|
|
$this->_initializedCollections[$oid . $name] = $value;
|
|
|
|
} else {
|
|
|
|
// Is already PersistentCollection, and DONT REFRESH
|
|
|
|
$this->_existingCollections[$oid . $name] = $value;
|
2009-09-06 19:25:23 +04:00
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
2009-09-06 19:25:23 +04:00
|
|
|
return $value;
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
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-08-28 14:48:40 +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-08-17 21:58:16 +04:00
|
|
|
$className = $this->_ce[$className]->discriminatorMap[$data[$discrColumn]];
|
2009-04-12 23:02:12 +04:00
|
|
|
unset($data[$discrColumn]);
|
|
|
|
}
|
2009-07-29 15:57:27 +04:00
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
$entity = $this->_uow->createEntity($className, $data, $this->_hints);
|
2009-05-13 19:19:27 +04:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
//FIXME: If $entity comes from the identity map there is no need to do this!
|
2009-05-13 19:19:27 +04:00
|
|
|
// Properly initialize any unfetched associations, if partial objects are not allowed.
|
|
|
|
if ( ! $this->_allowPartialObjects) {
|
2009-10-06 14:04:32 +04:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-08-17 21:58:16 +04:00
|
|
|
foreach ($this->_getClassMetadata($className)->associationMappings as $field => $assoc) {
|
2009-07-28 20:36:24 +04:00
|
|
|
// Check if the association is not among the fetch-joined associatons already.
|
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-10-06 14:04:32 +04:00
|
|
|
$proxy = $this->_proxyFactory->getAssociationProxy($entity, $assoc, $joinColumns);
|
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $field, $proxy);
|
|
|
|
$this->_ce[$className]->reflFields[$field]->setValue($entity, $proxy);
|
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-29 15:57:27 +04:00
|
|
|
$reflField = $this->_ce[$className]->reflFields[$field];
|
2009-08-11 14:51:38 +04:00
|
|
|
$pColl = new PersistentCollection($this->_em,
|
|
|
|
$this->_getClassMetadata($assoc->targetEntityName),
|
|
|
|
$reflField->getValue($entity) ?: new ArrayCollection
|
2009-07-29 15:57:27 +04:00
|
|
|
);
|
2009-07-28 20:36:24 +04:00
|
|
|
$pColl->setOwner($entity, $assoc);
|
2009-07-29 15:57:27 +04:00
|
|
|
$reflField->setValue($entity, $pColl);
|
2009-08-13 14:13:06 +04:00
|
|
|
if ($assoc->isLazilyFetched()) {
|
|
|
|
$pColl->setInitialized(false);
|
|
|
|
} else {
|
2009-07-28 15:43:42 +04:00
|
|
|
//TODO: Allow more efficient and configurable batching of these loads
|
2009-07-28 20:36:24 +04:00
|
|
|
$assoc->load($entity, $pColl, $this->_em);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2009-10-06 14:04:32 +04:00
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $field, $pColl);
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
return $entity;
|
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
private function _getEntityFromIdentityMap($className, array $data)
|
|
|
|
{
|
|
|
|
$class = $this->_ce[$className];
|
|
|
|
|
|
|
|
if ($class->isIdentifierComposite) {
|
|
|
|
$idHash = '';
|
|
|
|
foreach ($class->identifier as $fieldName) {
|
|
|
|
$idHash .= $data[$fieldName] . ' ';
|
|
|
|
}
|
|
|
|
$idHash = rtrim($idHash);
|
|
|
|
} else {
|
|
|
|
$idHash = $data[$class->identifier[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_uow->tryGetByIdHash($idHash, $class->rootEntityName);
|
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* Gets a ClassMetadata instance from the local cache.
|
|
|
|
* If the instance is not yet in the local cache, it is loaded into the
|
|
|
|
* local cache.
|
|
|
|
*
|
|
|
|
* @param string $className The name of the class.
|
|
|
|
* @return ClassMetadata
|
|
|
|
*/
|
|
|
|
private function _getClassMetadata($className)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_ce[$className])) {
|
|
|
|
$this->_ce[$className] = $this->_em->getClassMetadata($className);
|
|
|
|
}
|
|
|
|
return $this->_ce[$className];
|
|
|
|
}
|
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
|
|
|
*/
|
2009-10-09 18:27:35 +04:00
|
|
|
/*@override*/ protected function _hydrateRow(array &$data, array &$cache, array &$result)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-08-28 14:48:40 +04:00
|
|
|
// Initialize
|
2009-01-14 00:56:43 +03:00
|
|
|
$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-10-09 18:27:35 +04:00
|
|
|
// Hydrate the 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-10-09 18:27:35 +04:00
|
|
|
$parentAlias = $this->_rsm->parentAliasMap[$dqlAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
// Get a reference to the parent object to which the joined element belongs.
|
|
|
|
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parentAlias])) {
|
2009-05-21 23:18:14 +04:00
|
|
|
$first = reset($this->_resultPointers);
|
2009-10-09 18:27:35 +04:00
|
|
|
$parentObject = $this->_resultPointers[$parentAlias][key($first)];
|
|
|
|
} else if (isset($this->_resultPointers[$parentAlias])) {
|
|
|
|
$parentObject = $this->_resultPointers[$parentAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-10-09 18:27:35 +04:00
|
|
|
// Parent object of relation not found, so skip it.
|
|
|
|
continue;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
$parentClass = get_class($parentObject);
|
|
|
|
$oid = spl_object_hash($parentObject);
|
2009-08-11 14:51:38 +04:00
|
|
|
$relationField = $this->_rsm->relationMap[$dqlAlias];
|
|
|
|
$relation = $this->_ce[$parentClass]->associationMappings[$relationField];
|
2009-07-29 15:57:27 +04:00
|
|
|
$reflField = $this->_ce[$parentClass]->reflFields[$relationField];
|
2009-07-30 19:16:02 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
// Check the type of the relation (many or single-valued)
|
|
|
|
if ( ! $relation->isOneToOne()) {
|
2009-07-29 15:57:27 +04:00
|
|
|
// Collection-valued association
|
2009-04-09 22:12:48 +04:00
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
2009-10-09 18:27:35 +04:00
|
|
|
$collKey = $oid . $relationField;
|
|
|
|
if (isset($this->_initializedCollections[$collKey])) {
|
|
|
|
$reflFieldValue = $this->_initializedCollections[$collKey];
|
|
|
|
} else if ( ! isset($this->_existingCollections[$collKey])) {
|
|
|
|
$reflFieldValue = $this->_initRelatedCollection($parentObject, $relationField);
|
2009-05-07 20:36:27 +04:00
|
|
|
}
|
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
$indexExists = isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]]);
|
|
|
|
$index = $indexExists ? $this->_identifierMap[$dqlAlias][$id[$dqlAlias]] : false;
|
|
|
|
$indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false;
|
2009-07-15 10:46:43 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! $indexExists || ! $indexIsValid) {
|
2009-10-09 18:27:35 +04:00
|
|
|
if (isset($this->_existingCollections[$collKey])) {
|
|
|
|
// Collection exists, only look for $element in identity map.
|
|
|
|
if ($element = $this->_getEntityFromIdentityMap($entityName, $data)) {
|
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
|
|
|
} else {
|
|
|
|
unset($this->_resultPointers[$dqlAlias]);
|
2009-05-07 20:36:27 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-10-09 18:27:35 +04:00
|
|
|
$element = $this->_getEntity($data, $dqlAlias);
|
|
|
|
|
|
|
|
// If it's a bi-directional many-to-many, also initialize the reverse collection.
|
|
|
|
if ($relation->isManyToMany()) {
|
|
|
|
if ($relation->isOwningSide && isset($this->_ce[$entityName]->inverseMappings[$relation->sourceEntityName][$relationField])) {
|
|
|
|
$inverseFieldName = $this->_ce[$entityName]->inverseMappings[$relation->sourceEntityName][$relationField]->sourceFieldName;
|
|
|
|
// Only initialize reverse collection if it is not yet initialized.
|
|
|
|
if ( ! isset($this->_initializedCollections[spl_object_hash($element) . $inverseFieldName])) {
|
|
|
|
$this->_initRelatedCollection($element, $inverseFieldName);
|
|
|
|
}
|
|
|
|
} else if ($relation->mappedByFieldName) {
|
|
|
|
// Only initialize reverse collection if it is not yet initialized.
|
|
|
|
if ( ! isset($this->_initializedCollections[spl_object_hash($element) . $relation->mappedByFieldName])) {
|
|
|
|
$this->_initRelatedCollection($element, $relation->mappedByFieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
|
|
|
$field = $this->_rsm->indexByMap[$dqlAlias];
|
|
|
|
$indexValue = $this->_ce[$entityName]->reflFields[$field]->getValue($element);
|
|
|
|
$reflFieldValue->hydrateSet($indexValue, $element);
|
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $indexValue;
|
|
|
|
} else {
|
|
|
|
$reflFieldValue->hydrateAdd($element);
|
|
|
|
$reflFieldValue->last();
|
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $reflFieldValue->key();
|
|
|
|
}
|
|
|
|
// Update result pointer
|
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-10-09 18:27:35 +04:00
|
|
|
} else {
|
|
|
|
// Update result pointer
|
|
|
|
$this->_resultPointers[$dqlAlias] = $reflFieldValue[$index];
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
2009-10-09 18:27:35 +04:00
|
|
|
} else if ( ! $reflField->getValue($parentObject)) {
|
2009-07-29 15:57:27 +04:00
|
|
|
$coll = new PersistentCollection($this->_em, $this->_ce[$entityName], new ArrayCollection);
|
2009-10-09 18:27:35 +04:00
|
|
|
$reflField->setValue($parentObject, $coll);
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $relationField, $coll);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-07-29 15:57:27 +04:00
|
|
|
// Single-valued association
|
2009-10-09 18:27:35 +04:00
|
|
|
$reflFieldValue = $reflField->getValue($parentObject);
|
|
|
|
if ( ! $reflFieldValue || isset($this->_hints[Query::HINT_REFRESH])) {
|
2009-07-15 10:46:43 +04:00
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
2009-08-28 14:48:40 +04:00
|
|
|
$element = $this->_getEntity($data, $dqlAlias);
|
2009-10-09 18:27:35 +04:00
|
|
|
$reflField->setValue($parentObject, $element);
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $relationField, $element);
|
|
|
|
$targetClass = $this->_ce[$relation->targetEntityName];
|
|
|
|
if ($relation->isOwningSide) {
|
|
|
|
// If there is an inverse mapping on the target class its bidirectional
|
2009-08-11 14:51:38 +04:00
|
|
|
if (isset($targetClass->inverseMappings[$relation->sourceEntityName][$relationField])) {
|
|
|
|
$sourceProp = $targetClass->inverseMappings[$relation->sourceEntityName][$relationField]->sourceFieldName;
|
2009-10-09 18:27:35 +04:00
|
|
|
$targetClass->reflFields[$sourceProp]->setValue($element, $parentObject);
|
2009-07-29 15:57:27 +04:00
|
|
|
} else if ($this->_ce[$parentClass] === $targetClass && $relation->mappedByFieldName) {
|
|
|
|
// Special case: bi-directional self-referencing one-one on the same class
|
2009-10-09 18:27:35 +04:00
|
|
|
$targetClass->reflFields[$relationField]->setValue($element, $parentObject);
|
2009-07-29 15:57:27 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For sure bidirectional, as there is no inverse side in unidirectional mappings
|
2009-10-09 18:27:35 +04:00
|
|
|
$targetClass->reflFields[$relation->mappedByFieldName]->setValue($element, $parentObject);
|
2009-07-29 15:57:27 +04:00
|
|
|
}
|
2009-10-09 18:27:35 +04:00
|
|
|
// Update result pointer
|
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
2009-05-17 23:27:12 +04:00
|
|
|
}
|
2009-08-28 14:48:40 +04:00
|
|
|
// else leave $reflFieldValue null for single-valued associations
|
|
|
|
} else {
|
|
|
|
// Update result pointer
|
2009-08-03 17:25:56 +04:00
|
|
|
$this->_resultPointers[$dqlAlias] = $reflFieldValue;
|
2009-07-29 15:57:27 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Its a root result element
|
|
|
|
$this->_rootAliases[$dqlAlias] = true; // Mark as root alias
|
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
if ( ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
|
|
|
|
$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-08-28 14:48:40 +04:00
|
|
|
$key = $this->_ce[$entityName]->reflFields[$field]->getValue($element);
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2009-08-28 14:48:40 +04:00
|
|
|
$element = array($key => $element);
|
|
|
|
$result[] = $element;
|
2009-08-31 20:21:29 +04:00
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $this->_resultCounter;
|
2009-04-09 22:12:48 +04:00
|
|
|
++$this->_resultCounter;
|
|
|
|
} else {
|
2009-08-28 14:48:40 +04:00
|
|
|
$result[$key] = $element;
|
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $key;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2009-08-28 14:48:40 +04:00
|
|
|
$element = array(0 => $element);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-08-28 14:48:40 +04:00
|
|
|
$result[] = $element;
|
2009-08-31 20:21:29 +04:00
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $this->_resultCounter;
|
|
|
|
++$this->_resultCounter;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-08-03 17:25:56 +04:00
|
|
|
|
|
|
|
// Update result pointer
|
2009-08-28 14:48:40 +04:00
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-08-03 17:25:56 +04:00
|
|
|
// Update result pointer
|
2009-04-09 22:12:48 +04:00
|
|
|
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
|
2009-08-03 17:25:56 +04:00
|
|
|
$this->_resultPointers[$dqlAlias] = $result[$index];
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|