2007-11-20 17:26:42 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $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
|
2009-01-03 22:50:13 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2007-11-20 17:26:42 +03:00
|
|
|
*/
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
#namespace Doctrine\ORM\Internal\Hydration;
|
2008-09-13 14:28:29 +04:00
|
|
|
|
2007-11-20 17:26:42 +03:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* Defines the object hydration strategy.
|
2007-11-20 17:26:42 +03:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2009-01-03 22:50:13 +03:00
|
|
|
* @link www.doctrine-project.org
|
2007-11-20 17:26:42 +03:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2008-01-12 22:49:11 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-01-03 22:50:13 +03:00
|
|
|
* @internal All the methods in this class are performance-sentitive.
|
2007-11-20 17:26:42 +03:00
|
|
|
*/
|
2008-09-12 13:58:02 +04:00
|
|
|
class Doctrine_ORM_Internal_Hydration_ObjectDriver
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
2008-05-06 17:41:22 +04:00
|
|
|
/** Collections initialized by the driver */
|
2007-11-20 17:26:42 +03:00
|
|
|
protected $_collections = array();
|
2008-05-06 17:41:22 +04:00
|
|
|
/** Memory for initialized relations */
|
|
|
|
private $_initializedRelations = array();
|
|
|
|
/** Null object */
|
2009-01-03 22:50:13 +03:00
|
|
|
//private $_nullObject;
|
2008-05-14 01:20:34 +04:00
|
|
|
/** The EntityManager */
|
|
|
|
private $_em;
|
2009-01-03 22:50:13 +03:00
|
|
|
private $_uow;
|
2008-12-18 17:08:11 +03:00
|
|
|
private $_metadataMap = array();
|
2009-01-03 22:50:13 +03:00
|
|
|
private $_entityData = array();
|
2008-03-19 21:33:14 +03:00
|
|
|
|
2008-09-12 17:16:05 +04:00
|
|
|
public function __construct(Doctrine_ORM_EntityManager $em)
|
2008-03-19 21:33:14 +03:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
//$this->_nullObject = Doctrine_ORM_Internal_Null::$INSTANCE;
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->_em = $em;
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_uow = $this->_em->getUnitOfWork();
|
2008-03-19 21:33:14 +03:00
|
|
|
}
|
2007-11-20 17:26:42 +03:00
|
|
|
|
|
|
|
public function getElementCollection($component)
|
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
$coll = new Doctrine_ORM_Collection($this->_em, $component);
|
2007-11-20 17:26:42 +03:00
|
|
|
$this->_collections[] = $coll;
|
|
|
|
return $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLastKey($coll)
|
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
// check needed because of mixed results.
|
2009-01-03 22:50:13 +03:00
|
|
|
// is_object instead of is_array because is_array is slow on large arrays.
|
2008-12-18 17:08:11 +03:00
|
|
|
if (is_object($coll)) {
|
2009-01-12 16:34:41 +03:00
|
|
|
$coll->last();
|
2008-12-18 17:08:11 +03:00
|
|
|
return $coll->key();
|
|
|
|
} else {
|
2008-05-06 17:41:22 +04:00
|
|
|
end($coll);
|
|
|
|
return key($coll);
|
|
|
|
}
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
public function initRelatedCollection($entity, $name)
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
$classMetadata = $this->_metadataMap[$oid];
|
|
|
|
if ( ! isset($this->_initializedRelations[$oid][$name])) {
|
|
|
|
$relation = $classMetadata->getAssociationMapping($name);
|
2008-08-16 23:40:59 +04:00
|
|
|
$relatedClass = $this->_em->getClassMetadata($relation->getTargetEntityName());
|
2008-05-06 17:41:22 +04:00
|
|
|
$coll = $this->getElementCollection($relatedClass->getClassName());
|
2008-08-31 22:27:16 +04:00
|
|
|
$coll->_setOwner($entity, $relation);
|
|
|
|
$coll->_setHydrationFlag(true);
|
2008-12-18 17:08:11 +03:00
|
|
|
$classMetadata->getReflectionProperty($name)->setValue($entity, $coll);
|
|
|
|
$this->_initializedRelations[$oid][$name] = true;
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $name, $coll);
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-12 16:34:46 +04:00
|
|
|
public function registerCollection(Doctrine_ORM_Collection $coll)
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
|
|
|
$this->_collections[] = $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNullPointer()
|
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
//TODO: Return VirtualProxy if lazy association
|
|
|
|
return null;
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
|
2008-03-05 14:24:33 +03:00
|
|
|
public function getElement(array $data, $className)
|
2007-11-20 17:26:42 +03:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
$entity = $this->_em->getUnitOfWork()->createEntity($className, $data);
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
$this->_metadataMap[$oid] = $this->_em->getClassMetadata($className);
|
2008-12-18 17:08:11 +03:00
|
|
|
return $entity;
|
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an element to an indexed collection-valued property.
|
|
|
|
*
|
|
|
|
* @param <type> $entity1
|
|
|
|
* @param <type> $property
|
|
|
|
* @param <type> $entity2
|
|
|
|
* @param <type> $indexField
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function addRelatedIndexedElement($entity1, $property, $entity2, $indexField)
|
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$classMetadata1 = $this->_metadataMap[spl_object_hash($entity1)];
|
|
|
|
$classMetadata2 = $this->_metadataMap[spl_object_hash($entity2)];
|
2008-12-18 17:08:11 +03:00
|
|
|
$indexValue = $classMetadata2->getReflectionProperty($indexField)->getValue($entity2);
|
2009-01-12 16:34:41 +03:00
|
|
|
$classMetadata1->getReflectionProperty($property)->getValue($entity1)->set($indexValue, $entity2);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an element to a collection-valued property.
|
|
|
|
*
|
|
|
|
* @param <type> $entity1
|
|
|
|
* @param <type> $property
|
|
|
|
* @param <type> $entity2
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function addRelatedElement($entity1, $property, $entity2)
|
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$classMetadata1 = $this->_metadataMap[spl_object_hash($entity1)];
|
|
|
|
$classMetadata1->getReflectionProperty($property)->getValue($entity1)->add($entity2);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a related element.
|
|
|
|
*
|
|
|
|
* @param <type> $entity1
|
|
|
|
* @param <type> $property
|
|
|
|
* @param <type> $entity2
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function setRelatedElement($entity1, $property, $entity2)
|
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity1);
|
|
|
|
$classMetadata1 = $this->_metadataMap[$oid];
|
|
|
|
$classMetadata1->getReflectionProperty($property)->setValue($entity1, $entity2);
|
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $property, $entity2);
|
2008-12-18 17:08:11 +03:00
|
|
|
$relation = $classMetadata1->getAssociationMapping($property);
|
|
|
|
if ($relation->isOneToOne()) {
|
|
|
|
$targetClass = $this->_em->getClassMetadata($relation->getTargetEntityName());
|
|
|
|
if ($relation->isOwningSide()) {
|
|
|
|
// If there is an inverse mapping on the target class its bidirectional
|
|
|
|
if ($targetClass->hasInverseAssociationMapping($property)) {
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid2 = spl_object_hash($entity2);
|
|
|
|
$sourceProp = $targetClass->getInverseAssociationMapping($fieldName)->getSourceFieldName();
|
|
|
|
$targetClass->getReflectionProperty($sourceProp)->setValue($entity2, $entity1);
|
|
|
|
$this->_entityData[$oid2][$sourceProp] = $entity1;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// for sure bidirectional, as there is no inverse side in unidirectional
|
2009-01-03 22:50:13 +03:00
|
|
|
$mappedByProp = $relation->getMappedByFieldName();
|
|
|
|
$targetClass->getReflectionProperty($mappedByProp)->setValue($entity2, $entity1);
|
|
|
|
$this->_entityData[spl_object_hash($entity2)][$mappedByProp] = $entity1;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isIndexKeyInUse($entity, $assocField, $indexField)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
return $this->_metadataMap[spl_object_hash($entity)]->getReflectionProperty($assocField)
|
2008-12-18 17:08:11 +03:00
|
|
|
->getValue($entity)->containsKey($indexField);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isFieldSet($entity, $field)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
return $this->_metadataMap[spl_object_hash($entity)]->getReflectionProperty($field)
|
2008-12-18 17:08:11 +03:00
|
|
|
->getValue($entity) !== null;
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
public function getFieldValue($entity, $field)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
return $this->_metadataMap[spl_object_hash($entity)]->getReflectionProperty($field)
|
2008-12-18 17:08:11 +03:00
|
|
|
->getValue($entity);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
public function getReferenceValue($entity, $field)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
return $this->_metadataMap[spl_object_hash($entity)]->getReflectionProperty($field)
|
2008-12-18 17:08:11 +03:00
|
|
|
->getValue($entity);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addElementToIndexedCollection($coll, $entity, $keyField)
|
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
$coll->set($entity, $this->getFieldValue($keyField, $entity));
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addElementToCollection($coll, $entity)
|
|
|
|
{
|
|
|
|
$coll->add($entity);
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03: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.
|
|
|
|
*
|
|
|
|
* @param array $resultPointers The result pointers.
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Collection $coll The element.
|
2008-12-18 17:08:11 +03:00
|
|
|
* @param boolean|integer $index Index of the element in the collection.
|
|
|
|
* @param string $dqlAlias
|
|
|
|
* @param boolean $oneToOne Whether it is a single-valued association or not.
|
|
|
|
*/
|
|
|
|
public function updateResultPointer(&$resultPointers, &$coll, $index, $dqlAlias, $oneToOne)
|
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
if ($coll === null) {
|
2009-01-03 22:50:13 +03:00
|
|
|
echo "HERE!";
|
2008-12-18 17:08:11 +03:00
|
|
|
unset($resultPointers[$dqlAlias]); // Ticket #1228
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($index !== false) {
|
|
|
|
$resultPointers[$dqlAlias] = $coll[$index];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_object($coll)) {
|
|
|
|
end($coll);
|
|
|
|
$resultPointers[$dqlAlias] =& $coll[key($coll)];
|
|
|
|
} else if ($coll instanceof Doctrine_ORM_Collection) {
|
|
|
|
if (count($coll) > 0) {
|
2009-01-12 16:34:41 +03:00
|
|
|
$resultPointers[$dqlAlias] = $coll->last();
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$resultPointers[$dqlAlias] = $coll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 17:26:42 +03:00
|
|
|
public function flush()
|
|
|
|
{
|
|
|
|
// take snapshots from all initialized collections
|
2008-04-13 00:11:11 +04:00
|
|
|
foreach ($this->_collections as $coll) {
|
2008-08-31 22:27:16 +04:00
|
|
|
$coll->_takeSnapshot();
|
|
|
|
$coll->_setHydrationFlag(false);
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_uow->addManagedCollection($coll);
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
// clean up
|
2008-03-05 14:24:33 +03:00
|
|
|
$this->_collections = array();
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_initializedRelations = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
$this->_metadataMap = array();
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_entityData = array();
|
2008-01-12 22:49:11 +03:00
|
|
|
}
|
|
|
|
|
2007-11-20 17:26:42 +03:00
|
|
|
}
|