init without trunk
This commit is contained in:
parent
ed24ac4994
commit
bb809e7233
14652 changed files with 177862 additions and 94817 deletions
156
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/CommitOrderCalculator.php
vendored
Normal file
156
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/CommitOrderCalculator.php
vendored
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal;
|
||||
|
||||
/**
|
||||
* The CommitOrderCalculator is used by the UnitOfWork to sort out the
|
||||
* correct order in which changes to entities need to be persisted.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
*/
|
||||
class CommitOrderCalculator
|
||||
{
|
||||
const NOT_VISITED = 1;
|
||||
const IN_PROGRESS = 2;
|
||||
const VISITED = 3;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_nodeStates = array();
|
||||
|
||||
/**
|
||||
* The nodes to sort.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_classes = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_relatedClasses = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_sorted = array();
|
||||
|
||||
/**
|
||||
* Clears the current graph.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->_classes = array();
|
||||
$this->_relatedClasses = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a valid commit order for all current nodes.
|
||||
*
|
||||
* Uses a depth-first search (DFS) to traverse the graph.
|
||||
* The desired topological sorting is the reverse postorder of these searches.
|
||||
*
|
||||
* @return array The list of ordered classes.
|
||||
*/
|
||||
public function getCommitOrder()
|
||||
{
|
||||
// Check whether we need to do anything. 0 or 1 node is easy.
|
||||
$nodeCount = count($this->_classes);
|
||||
|
||||
if ($nodeCount <= 1) {
|
||||
return ($nodeCount == 1) ? array_values($this->_classes) : array();
|
||||
}
|
||||
|
||||
// Init
|
||||
foreach ($this->_classes as $node) {
|
||||
$this->_nodeStates[$node->name] = self::NOT_VISITED;
|
||||
}
|
||||
|
||||
// Go
|
||||
foreach ($this->_classes as $node) {
|
||||
if ($this->_nodeStates[$node->name] == self::NOT_VISITED) {
|
||||
$this->_visitNode($node);
|
||||
}
|
||||
}
|
||||
|
||||
$sorted = array_reverse($this->_sorted);
|
||||
|
||||
$this->_sorted = $this->_nodeStates = array();
|
||||
|
||||
return $sorted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\Mapping\ClassMetadata $node
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function _visitNode($node)
|
||||
{
|
||||
$this->_nodeStates[$node->name] = self::IN_PROGRESS;
|
||||
|
||||
if (isset($this->_relatedClasses[$node->name])) {
|
||||
foreach ($this->_relatedClasses[$node->name] as $relatedNode) {
|
||||
if ($this->_nodeStates[$relatedNode->name] == self::NOT_VISITED) {
|
||||
$this->_visitNode($relatedNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->_nodeStates[$node->name] = self::VISITED;
|
||||
$this->_sorted[] = $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\Mapping\ClassMetadata $fromClass
|
||||
* @param \Doctrine\ORM\Mapping\ClassMetadata $toClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addDependency($fromClass, $toClass)
|
||||
{
|
||||
$this->_relatedClasses[$fromClass->name][] = $toClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasClass($className)
|
||||
{
|
||||
return isset($this->_classes[$className]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\Mapping\ClassMetadata $class
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addClass($class)
|
||||
{
|
||||
$this->_classes[$class->name] = $class;
|
||||
}
|
||||
}
|
||||
476
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
vendored
Normal file
476
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
vendored
Normal file
|
|
@ -0,0 +1,476 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* Base class for all hydrators. A hydrator is a class that provides some form
|
||||
* of transformation of an SQL result set into another structure.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
|
||||
*/
|
||||
abstract class AbstractHydrator
|
||||
{
|
||||
/**
|
||||
* The ResultSetMapping.
|
||||
*
|
||||
* @var \Doctrine\ORM\Query\ResultSetMapping
|
||||
*/
|
||||
protected $_rsm;
|
||||
|
||||
/**
|
||||
* The EntityManager instance.
|
||||
*
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $_em;
|
||||
|
||||
/**
|
||||
* The dbms Platform instance.
|
||||
*
|
||||
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
||||
*/
|
||||
protected $_platform;
|
||||
|
||||
/**
|
||||
* The UnitOfWork of the associated EntityManager.
|
||||
*
|
||||
* @var \Doctrine\ORM\UnitOfWork
|
||||
*/
|
||||
protected $_uow;
|
||||
|
||||
/**
|
||||
* Local ClassMetadata cache to avoid going to the EntityManager all the time.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_metadataCache = array();
|
||||
|
||||
/**
|
||||
* The cache used during row-by-row hydration.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_cache = array();
|
||||
|
||||
/**
|
||||
* The statement that provides the data to hydrate.
|
||||
*
|
||||
* @var \Doctrine\DBAL\Driver\Statement
|
||||
*/
|
||||
protected $_stmt;
|
||||
|
||||
/**
|
||||
* The query hints.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_hints;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of a class derived from <tt>AbstractHydrator</tt>.
|
||||
*
|
||||
* @param EntityManagerInterface $em The EntityManager to use.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->_em = $em;
|
||||
$this->_platform = $em->getConnection()->getDatabasePlatform();
|
||||
$this->_uow = $em->getUnitOfWork();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a row-by-row hydration.
|
||||
*
|
||||
* @param object $stmt
|
||||
* @param object $resultSetMapping
|
||||
* @param array $hints
|
||||
*
|
||||
* @return IterableResult
|
||||
*/
|
||||
public function iterate($stmt, $resultSetMapping, array $hints = array())
|
||||
{
|
||||
$this->_stmt = $stmt;
|
||||
$this->_rsm = $resultSetMapping;
|
||||
$this->_hints = $hints;
|
||||
|
||||
$evm = $this->_em->getEventManager();
|
||||
$evm->addEventListener(array(Events::onClear), $this);
|
||||
|
||||
$this->prepare();
|
||||
|
||||
return new IterableResult($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrates all rows returned by the passed statement instance at once.
|
||||
*
|
||||
* @param object $stmt
|
||||
* @param object $resultSetMapping
|
||||
* @param array $hints
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function hydrateAll($stmt, $resultSetMapping, array $hints = array())
|
||||
{
|
||||
$this->_stmt = $stmt;
|
||||
$this->_rsm = $resultSetMapping;
|
||||
$this->_hints = $hints;
|
||||
|
||||
$evm = $this->_em->getEventManager();
|
||||
$evm->addEventListener(array(Events::onClear), $this);
|
||||
|
||||
$this->prepare();
|
||||
|
||||
$result = $this->hydrateAllData();
|
||||
|
||||
$this->cleanup();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrates a single row returned by the current statement instance during
|
||||
* row-by-row hydration with {@link iterate()}.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function hydrateRow()
|
||||
{
|
||||
$row = $this->_stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ( ! $row) {
|
||||
$this->cleanup();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
|
||||
$this->hydrateRowData($row, $result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* When executed in a hydrate() loop we have to clear internal state to
|
||||
* decrease memory consumption.
|
||||
*
|
||||
* @param mixed $eventArgs
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function onClear($eventArgs)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes one-time preparation tasks, once each time hydration is started
|
||||
* through {@link hydrateAll} or {@link iterate()}.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function prepare()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes one-time cleanup tasks at the end of a hydration that was initiated
|
||||
* through {@link hydrateAll} or {@link iterate()}.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function cleanup()
|
||||
{
|
||||
$this->_stmt->closeCursor();
|
||||
|
||||
$this->_stmt = null;
|
||||
$this->_rsm = null;
|
||||
$this->_cache = array();
|
||||
$this->_metadataCache = array();
|
||||
|
||||
$evm = $this->_em->getEventManager();
|
||||
$evm->removeEventListener(array(Events::onClear), $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrates a single row from the current statement instance.
|
||||
*
|
||||
* Template method.
|
||||
*
|
||||
* @param array $data The row data.
|
||||
* @param array $result The result to fill.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws HydrationException
|
||||
*/
|
||||
protected function hydrateRowData(array $data, array &$result)
|
||||
{
|
||||
throw new HydrationException("hydrateRowData() not implemented by this hydrator.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrates all rows from the current statement instance at once.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function hydrateAllData();
|
||||
|
||||
/**
|
||||
* Processes a row of the result set.
|
||||
*
|
||||
* Used for identity-based hydration (HYDRATE_OBJECT and HYDRATE_ARRAY).
|
||||
* Puts the elements of a result row into a new array, grouped by the dql alias
|
||||
* they belong to. The column names in the result set are mapped to their
|
||||
* field names during this procedure as well as any necessary conversions on
|
||||
* the values applied. Scalar values are kept in a specific key 'scalars'.
|
||||
*
|
||||
* @param array $data SQL Result Row.
|
||||
* @param array &$id Dql-Alias => ID-Hash.
|
||||
* @param array &$nonemptyComponents Does this DQL-Alias has at least one non NULL value?
|
||||
*
|
||||
* @return array An array with all the fields (name => value) of the data row,
|
||||
* grouped by their component alias.
|
||||
*/
|
||||
protected function gatherRowData(array $data, array &$id, array &$nonemptyComponents)
|
||||
{
|
||||
$rowData = array('data' => array());
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (($cacheKeyInfo = $this->hydrateColumnInfo($key)) === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldName = $cacheKeyInfo['fieldName'];
|
||||
|
||||
switch (true) {
|
||||
case (isset($cacheKeyInfo['isNewObjectParameter'])):
|
||||
$argIndex = $cacheKeyInfo['argIndex'];
|
||||
$objIndex = $cacheKeyInfo['objIndex'];
|
||||
$type = $cacheKeyInfo['type'];
|
||||
$value = $type->convertToPHPValue($value, $this->_platform);
|
||||
|
||||
$rowData['newObjects'][$objIndex]['class'] = $cacheKeyInfo['class'];
|
||||
$rowData['newObjects'][$objIndex]['args'][$argIndex] = $value;
|
||||
break;
|
||||
|
||||
case (isset($cacheKeyInfo['isScalar'])):
|
||||
$type = $cacheKeyInfo['type'];
|
||||
$value = $type->convertToPHPValue($value, $this->_platform);
|
||||
|
||||
$rowData['scalars'][$fieldName] = $value;
|
||||
break;
|
||||
|
||||
//case (isset($cacheKeyInfo['isMetaColumn'])):
|
||||
default:
|
||||
$dqlAlias = $cacheKeyInfo['dqlAlias'];
|
||||
$type = $cacheKeyInfo['type'];
|
||||
|
||||
// in an inheritance hierarchy the same field could be defined several times.
|
||||
// We overwrite this value so long we don't have a non-null value, that value we keep.
|
||||
// Per definition it cannot be that a field is defined several times and has several values.
|
||||
if (isset($rowData['data'][$dqlAlias][$fieldName])) {
|
||||
break;
|
||||
}
|
||||
|
||||
$rowData['data'][$dqlAlias][$fieldName] = $type
|
||||
? $type->convertToPHPValue($value, $this->_platform)
|
||||
: $value;
|
||||
|
||||
if ($cacheKeyInfo['isIdentifier'] && $value !== null) {
|
||||
$id[$dqlAlias] .= '|' . $value;
|
||||
$nonemptyComponents[$dqlAlias] = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $rowData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a row of the result set.
|
||||
*
|
||||
* Used for HYDRATE_SCALAR. This is a variant of _gatherRowData() that
|
||||
* simply converts column names to field names and properly converts the
|
||||
* values according to their types. The resulting row has the same number
|
||||
* of elements as before.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return array The processed row.
|
||||
*/
|
||||
protected function gatherScalarRowData(&$data)
|
||||
{
|
||||
$rowData = array();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (($cacheKeyInfo = $this->hydrateColumnInfo($key)) === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldName = $cacheKeyInfo['fieldName'];
|
||||
|
||||
// WARNING: BC break! We know this is the desired behavior to type convert values, but this
|
||||
// erroneous behavior exists since 2.0 and we're forced to keep compatibility.
|
||||
if ( ! isset($cacheKeyInfo['isScalar'])) {
|
||||
$dqlAlias = $cacheKeyInfo['dqlAlias'];
|
||||
$type = $cacheKeyInfo['type'];
|
||||
$fieldName = $dqlAlias . '_' . $fieldName;
|
||||
$value = $type
|
||||
? $type->convertToPHPValue($value, $this->_platform)
|
||||
: $value;
|
||||
}
|
||||
|
||||
$rowData[$fieldName] = $value;
|
||||
}
|
||||
|
||||
return $rowData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve column information from ResultSetMapping.
|
||||
*
|
||||
* @param string $key Column name
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
protected function hydrateColumnInfo($key)
|
||||
{
|
||||
if (isset($this->_cache[$key])) {
|
||||
return $this->_cache[$key];
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
// NOTE: Most of the times it's a field mapping, so keep it first!!!
|
||||
case (isset($this->_rsm->fieldMappings[$key])):
|
||||
$classMetadata = $this->getClassMetadata($this->_rsm->declaringClasses[$key]);
|
||||
$fieldName = $this->_rsm->fieldMappings[$key];
|
||||
$fieldMapping = $classMetadata->fieldMappings[$fieldName];
|
||||
|
||||
return $this->_cache[$key] = array(
|
||||
'isIdentifier' => in_array($fieldName, $classMetadata->identifier),
|
||||
'fieldName' => $fieldName,
|
||||
'type' => Type::getType($fieldMapping['type']),
|
||||
'dqlAlias' => $this->_rsm->columnOwnerMap[$key],
|
||||
);
|
||||
|
||||
case (isset($this->_rsm->newObjectMappings[$key])):
|
||||
// WARNING: A NEW object is also a scalar, so it must be declared before!
|
||||
$mapping = $this->_rsm->newObjectMappings[$key];
|
||||
|
||||
return $this->_cache[$key] = array(
|
||||
'isScalar' => true,
|
||||
'isNewObjectParameter' => true,
|
||||
'fieldName' => $this->_rsm->scalarMappings[$key],
|
||||
'type' => Type::getType($this->_rsm->typeMappings[$key]),
|
||||
'argIndex' => $mapping['argIndex'],
|
||||
'objIndex' => $mapping['objIndex'],
|
||||
'class' => new \ReflectionClass($mapping['className']),
|
||||
);
|
||||
|
||||
case (isset($this->_rsm->scalarMappings[$key])):
|
||||
return $this->_cache[$key] = array(
|
||||
'isScalar' => true,
|
||||
'fieldName' => $this->_rsm->scalarMappings[$key],
|
||||
'type' => Type::getType($this->_rsm->typeMappings[$key]),
|
||||
);
|
||||
|
||||
case (isset($this->_rsm->metaMappings[$key])):
|
||||
// Meta column (has meaning in relational schema only, i.e. foreign keys or discriminator columns).
|
||||
$fieldName = $this->_rsm->metaMappings[$key];
|
||||
$dqlAlias = $this->_rsm->columnOwnerMap[$key];
|
||||
$classMetadata = $this->getClassMetadata($this->_rsm->aliasMap[$dqlAlias]);
|
||||
$type = isset($this->_rsm->typeMappings[$key])
|
||||
? Type::getType($this->_rsm->typeMappings[$key])
|
||||
: null;
|
||||
|
||||
return $this->_cache[$key] = array(
|
||||
'isIdentifier' => isset($this->_rsm->isIdentifierColumn[$dqlAlias][$key]),
|
||||
'isMetaColumn' => true,
|
||||
'fieldName' => $fieldName,
|
||||
'type' => $type,
|
||||
'dqlAlias' => $dqlAlias,
|
||||
);
|
||||
}
|
||||
|
||||
// this column is a left over, maybe from a LIMIT query hack for example in Oracle or DB2
|
||||
// maybe from an additional column that has not been defined in a NativeQuery ResultSetMapping.
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve ClassMetadata associated to entity class name.
|
||||
*
|
||||
* @param string $className
|
||||
*
|
||||
* @return \Doctrine\ORM\Mapping\ClassMetadata
|
||||
*/
|
||||
protected function getClassMetadata($className)
|
||||
{
|
||||
if ( ! isset($this->_metadataCache[$className])) {
|
||||
$this->_metadataCache[$className] = $this->_em->getClassMetadata($className);
|
||||
}
|
||||
|
||||
return $this->_metadataCache[$className];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register entity as managed in UnitOfWork.
|
||||
*
|
||||
* @param ClassMetadata $class
|
||||
* @param object $entity
|
||||
* @param array $data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @todo The "$id" generation is the same of UnitOfWork#createEntity. Remove this duplication somehow
|
||||
*/
|
||||
protected function registerManaged(ClassMetadata $class, $entity, array $data)
|
||||
{
|
||||
if ($class->isIdentifierComposite) {
|
||||
$id = array();
|
||||
|
||||
foreach ($class->identifier as $fieldName) {
|
||||
$id[$fieldName] = isset($class->associationMappings[$fieldName])
|
||||
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
|
||||
: $data[$fieldName];
|
||||
}
|
||||
} else {
|
||||
$fieldName = $class->identifier[0];
|
||||
$id = array(
|
||||
$fieldName => isset($class->associationMappings[$fieldName])
|
||||
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
|
||||
: $data[$fieldName]
|
||||
);
|
||||
}
|
||||
|
||||
$this->_em->getUnitOfWork()->registerManaged($entity, $id, $data);
|
||||
}
|
||||
}
|
||||
307
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php
vendored
Normal file
307
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php
vendored
Normal file
|
|
@ -0,0 +1,307 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
use PDO;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* The ArrayHydrator produces a nested array "graph" that is often (not always)
|
||||
* interchangeable with the corresponding object graph for read-only access.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
|
||||
*/
|
||||
class ArrayHydrator extends AbstractHydrator
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_rootAliases = array();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $_isSimpleQuery = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_identifierMap = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_resultPointers = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_idTemplate = array();
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $_resultCounter = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function prepare()
|
||||
{
|
||||
$this->_isSimpleQuery = count($this->_rsm->aliasMap) <= 1;
|
||||
|
||||
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
|
||||
$this->_identifierMap[$dqlAlias] = array();
|
||||
$this->_resultPointers[$dqlAlias] = array();
|
||||
$this->_idTemplate[$dqlAlias] = '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateAllData()
|
||||
{
|
||||
$result = array();
|
||||
|
||||
while ($data = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$this->hydrateRowData($data, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateRowData(array $row, array &$result)
|
||||
{
|
||||
// 1) Initialize
|
||||
$id = $this->_idTemplate; // initialize the id-memory
|
||||
$nonemptyComponents = array();
|
||||
$rowData = $this->gatherRowData($row, $id, $nonemptyComponents);
|
||||
|
||||
// 2) Now hydrate the data found in the current row.
|
||||
foreach ($rowData['data'] as $dqlAlias => $data) {
|
||||
$index = false;
|
||||
|
||||
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) {
|
||||
// It's a joined result
|
||||
|
||||
$parent = $this->_rsm->parentAliasMap[$dqlAlias];
|
||||
$path = $parent . '.' . $dqlAlias;
|
||||
|
||||
// missing parent data, skipping as RIGHT JOIN hydration is not supported.
|
||||
if ( ! isset($nonemptyComponents[$parent]) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get a reference to the right element in the result tree.
|
||||
// This element will get the associated element attached.
|
||||
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parent])) {
|
||||
$first = reset($this->_resultPointers);
|
||||
// TODO: Exception if $key === null ?
|
||||
$baseElement =& $this->_resultPointers[$parent][key($first)];
|
||||
} else if (isset($this->_resultPointers[$parent])) {
|
||||
$baseElement =& $this->_resultPointers[$parent];
|
||||
} else {
|
||||
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$relationAlias = $this->_rsm->relationMap[$dqlAlias];
|
||||
$parentClass = $this->_metadataCache[$this->_rsm->aliasMap[$parent]];
|
||||
$relation = $parentClass->associationMappings[$relationAlias];
|
||||
|
||||
// Check the type of the relation (many or single-valued)
|
||||
if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) {
|
||||
$oneToOne = false;
|
||||
|
||||
if ( ! isset($baseElement[$relationAlias])) {
|
||||
$baseElement[$relationAlias] = array();
|
||||
}
|
||||
|
||||
if (isset($nonemptyComponents[$dqlAlias])) {
|
||||
$indexExists = isset($this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]]);
|
||||
$index = $indexExists ? $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] : false;
|
||||
$indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false;
|
||||
|
||||
if ( ! $indexExists || ! $indexIsValid) {
|
||||
$element = $data;
|
||||
|
||||
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
||||
$baseElement[$relationAlias][$row[$this->_rsm->indexByMap[$dqlAlias]]] = $element;
|
||||
} else {
|
||||
$baseElement[$relationAlias][] = $element;
|
||||
}
|
||||
|
||||
end($baseElement[$relationAlias]);
|
||||
|
||||
$this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = key($baseElement[$relationAlias]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$oneToOne = true;
|
||||
|
||||
if (
|
||||
( ! isset($nonemptyComponents[$dqlAlias])) &&
|
||||
( ! isset($baseElement[$relationAlias]))
|
||||
) {
|
||||
$baseElement[$relationAlias] = null;
|
||||
} else if ( ! isset($baseElement[$relationAlias])) {
|
||||
$baseElement[$relationAlias] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
$coll =& $baseElement[$relationAlias];
|
||||
|
||||
if (is_array($coll)) {
|
||||
$this->updateResultPointer($coll, $index, $dqlAlias, $oneToOne);
|
||||
}
|
||||
} else {
|
||||
// It's a root result element
|
||||
|
||||
$this->_rootAliases[$dqlAlias] = true; // Mark as root
|
||||
$entityKey = $this->_rsm->entityMappings[$dqlAlias] ?: 0;
|
||||
|
||||
// if this row has a NULL value for the root result id then make it a null result.
|
||||
if ( ! isset($nonemptyComponents[$dqlAlias]) ) {
|
||||
$result[] = $this->_rsm->isMixed
|
||||
? array($entityKey => null)
|
||||
: null;
|
||||
|
||||
$resultKey = $this->_resultCounter;
|
||||
++$this->_resultCounter;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for an existing element
|
||||
if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
|
||||
$element = $this->_rsm->isMixed
|
||||
? array($entityKey => $data)
|
||||
: $data;
|
||||
|
||||
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
||||
$resultKey = $row[$this->_rsm->indexByMap[$dqlAlias]];
|
||||
$result[$resultKey] = $element;
|
||||
} else {
|
||||
$resultKey = $this->_resultCounter;
|
||||
$result[] = $element;
|
||||
|
||||
++$this->_resultCounter;
|
||||
}
|
||||
|
||||
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey;
|
||||
} else {
|
||||
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
|
||||
$resultKey = $index;
|
||||
}
|
||||
|
||||
$this->updateResultPointer($result, $index, $dqlAlias, false);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! isset($resultKey)) {
|
||||
$this->_resultCounter++;
|
||||
}
|
||||
|
||||
// Append scalar values to mixed result sets
|
||||
if (isset($rowData['scalars'])) {
|
||||
if ( ! isset($resultKey)) {
|
||||
// this only ever happens when no object is fetched (scalar result only)
|
||||
$resultKey = isset($this->_rsm->indexByMap['scalars'])
|
||||
? $row[$this->_rsm->indexByMap['scalars']]
|
||||
: $this->_resultCounter - 1;
|
||||
}
|
||||
|
||||
foreach ($rowData['scalars'] as $name => $value) {
|
||||
$result[$resultKey][$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Append new object to mixed result sets
|
||||
if (isset($rowData['newObjects'])) {
|
||||
if ( ! isset($resultKey)) {
|
||||
$resultKey = $this->_resultCounter - 1;
|
||||
}
|
||||
|
||||
$scalarCount = (isset($rowData['scalars'])? count($rowData['scalars']): 0);
|
||||
|
||||
foreach ($rowData['newObjects'] as $objIndex => $newObject) {
|
||||
$class = $newObject['class'];
|
||||
$args = $newObject['args'];
|
||||
$obj = $class->newInstanceArgs($args);
|
||||
|
||||
if (count($args) == $scalarCount || ($scalarCount == 0 && count($rowData['newObjects']) == 1)) {
|
||||
$result[$resultKey] = $obj;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$resultKey][$objIndex] = $obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 $coll The element.
|
||||
* @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.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function updateResultPointer(array &$coll, $index, $dqlAlias, $oneToOne)
|
||||
{
|
||||
if ($coll === null) {
|
||||
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($oneToOne) {
|
||||
$this->_resultPointers[$dqlAlias] =& $coll;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($index !== false) {
|
||||
$this->_resultPointers[$dqlAlias] =& $coll[$index];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $coll) {
|
||||
return;
|
||||
}
|
||||
|
||||
end($coll);
|
||||
$this->_resultPointers[$dqlAlias] =& $coll[key($coll)];
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
105
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/HydrationException.php
vendored
Normal file
105
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/HydrationException.php
vendored
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
class HydrationException extends \Doctrine\ORM\ORMException
|
||||
{
|
||||
/**
|
||||
* @return HydrationException
|
||||
*/
|
||||
public static function nonUniqueResult()
|
||||
{
|
||||
return new self("The result returned by the query was not unique.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $alias
|
||||
* @param string $parentAlias
|
||||
*
|
||||
* @return HydrationException
|
||||
*/
|
||||
public static function parentObjectOfRelationNotFound($alias, $parentAlias)
|
||||
{
|
||||
return new self("The parent object of entity result with alias '$alias' was not found."
|
||||
. " The parent alias is '$parentAlias'.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dqlAlias
|
||||
*
|
||||
* @return HydrationException
|
||||
*/
|
||||
public static function emptyDiscriminatorValue($dqlAlias)
|
||||
{
|
||||
return new self("The DQL alias '" . $dqlAlias . "' contains an entity ".
|
||||
"of an inheritance hierarchy with an empty discriminator value. This means " .
|
||||
"that the database contains inconsistent data with an empty " .
|
||||
"discriminator value in a table row."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.3
|
||||
*
|
||||
* @param string $entityName
|
||||
* @param string $discrColumnName
|
||||
* @param string $dqlAlias
|
||||
*
|
||||
* @return HydrationException
|
||||
*/
|
||||
public static function missingDiscriminatorColumn($entityName, $discrColumnName, $dqlAlias)
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The discriminator column "%s" is missing for "%s" using the DQL alias "%s".',
|
||||
$discrColumnName, $entityName, $dqlAlias
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.3
|
||||
*
|
||||
* @param string $entityName
|
||||
* @param string $discrColumnName
|
||||
* @param string $dqlAlias
|
||||
*
|
||||
* @return HydrationException
|
||||
*/
|
||||
public static function missingDiscriminatorMetaMappingColumn($entityName, $discrColumnName, $dqlAlias)
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The meta mapping for the discriminator column "%s" is missing for "%s" using the DQL alias "%s".',
|
||||
$discrColumnName, $entityName, $dqlAlias
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $discrValue
|
||||
* @param array $discrMap
|
||||
*
|
||||
* @return HydrationException
|
||||
*/
|
||||
public static function invalidDiscriminatorValue($discrValue, $discrMap)
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The discriminator value "%s" is invalid. It must be one of "%s".',
|
||||
$discrValue, implode('", "', $discrMap)
|
||||
));
|
||||
}
|
||||
}
|
||||
109
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/IterableResult.php
vendored
Normal file
109
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/IterableResult.php
vendored
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
/**
|
||||
* Represents a result structure that can be iterated over, hydrating row-by-row
|
||||
* during the iteration. An IterableResult is obtained by AbstractHydrator#iterate().
|
||||
*
|
||||
* @author robo
|
||||
* @since 2.0
|
||||
*/
|
||||
class IterableResult implements \Iterator
|
||||
{
|
||||
/**
|
||||
* @var \Doctrine\ORM\Internal\Hydration\AbstractHydrator
|
||||
*/
|
||||
private $_hydrator;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
private $_rewinded = false;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
private $_key = -1;
|
||||
|
||||
/**
|
||||
* @var object|null
|
||||
*/
|
||||
private $_current = null;
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\Internal\Hydration\AbstractHydrator $hydrator
|
||||
*/
|
||||
public function __construct($hydrator)
|
||||
{
|
||||
$this->_hydrator = $hydrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*
|
||||
* @throws HydrationException
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
if ($this->_rewinded == true) {
|
||||
throw new HydrationException("Can only iterate a Result once.");
|
||||
} else {
|
||||
$this->_current = $this->next();
|
||||
$this->_rewinded = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next set of results.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
$this->_current = $this->_hydrator->hydrateRow();
|
||||
$this->_key++;
|
||||
return $this->_current;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->_current;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return ($this->_current!=false);
|
||||
}
|
||||
}
|
||||
598
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
vendored
Normal file
598
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
vendored
Normal file
|
|
@ -0,0 +1,598 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
use Doctrine\ORM\UnitOfWork;
|
||||
use PDO;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\PersistentCollection;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||
use Doctrine\ORM\Event\PostLoadEventDispatcher;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\Proxy\Proxy;
|
||||
|
||||
/**
|
||||
* The ObjectHydrator constructs an object graph out of an SQL result set.
|
||||
*
|
||||
* Internal note: Highly performance-sensitive code.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
|
||||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
|
||||
*/
|
||||
class ObjectHydrator extends AbstractHydrator
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $identifierMap = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $resultPointers = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $idTemplate = array();
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
private $resultCounter = 0;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $rootAliases = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $initializedCollections = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $existingCollections = array();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function prepare()
|
||||
{
|
||||
if ( ! isset($this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD])) {
|
||||
$this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD] = true;
|
||||
}
|
||||
|
||||
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
|
||||
$this->identifierMap[$dqlAlias] = array();
|
||||
$this->idTemplate[$dqlAlias] = '';
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
$parent = $this->_rsm->parentAliasMap[$dqlAlias];
|
||||
|
||||
if ( ! isset($this->_rsm->aliasMap[$parent])) {
|
||||
throw HydrationException::parentObjectOfRelationNotFound($dqlAlias, $parent);
|
||||
}
|
||||
|
||||
$sourceClassName = $this->_rsm->aliasMap[$parent];
|
||||
$sourceClass = $this->getClassMetadata($sourceClassName);
|
||||
$assoc = $sourceClass->associationMappings[$this->_rsm->relationMap[$dqlAlias]];
|
||||
|
||||
$this->_hints['fetched'][$parent][$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->getClassMetadata($className);
|
||||
$inverseAssoc = $class->associationMappings[$assoc['inversedBy']];
|
||||
|
||||
if ( ! ($inverseAssoc['type'] & ClassMetadata::TO_ONE)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->_hints['fetched'][$dqlAlias][$inverseAssoc['fieldName']] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function cleanup()
|
||||
{
|
||||
$eagerLoad = (isset($this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD])) && $this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD] == true;
|
||||
|
||||
parent::cleanup();
|
||||
|
||||
$this->identifierMap =
|
||||
$this->initializedCollections =
|
||||
$this->existingCollections =
|
||||
$this->resultPointers = array();
|
||||
|
||||
if ($eagerLoad) {
|
||||
$this->_uow->triggerEagerLoads();
|
||||
}
|
||||
|
||||
$this->_uow->hydrationComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateAllData()
|
||||
{
|
||||
$result = array();
|
||||
|
||||
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$this->hydrateRowData($row, $result);
|
||||
}
|
||||
|
||||
// Take snapshots from all newly initialized collections
|
||||
foreach ($this->initializedCollections as $coll) {
|
||||
$coll->takeSnapshot();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a related collection.
|
||||
*
|
||||
* @param object $entity The entity to which the collection belongs.
|
||||
* @param ClassMetadata $class
|
||||
* @param string $fieldName The name of the field on the entity that holds the collection.
|
||||
* @param string $parentDqlAlias Alias of the parent fetch joining this collection.
|
||||
*
|
||||
* @return \Doctrine\ORM\PersistentCollection
|
||||
*/
|
||||
private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias)
|
||||
{
|
||||
$oid = spl_object_hash($entity);
|
||||
$relation = $class->associationMappings[$fieldName];
|
||||
$value = $class->reflFields[$fieldName]->getValue($entity);
|
||||
|
||||
if ($value === null || is_array($value)) {
|
||||
$value = new ArrayCollection((array) $value);
|
||||
}
|
||||
|
||||
if ( ! $value instanceof PersistentCollection) {
|
||||
$value = new PersistentCollection(
|
||||
$this->_em, $this->_metadataCache[$relation['targetEntity']], $value
|
||||
);
|
||||
$value->setOwner($entity, $relation);
|
||||
|
||||
$class->reflFields[$fieldName]->setValue($entity, $value);
|
||||
$this->_uow->setOriginalEntityProperty($oid, $fieldName, $value);
|
||||
|
||||
$this->initializedCollections[$oid . $fieldName] = $value;
|
||||
} else if (
|
||||
isset($this->_hints[Query::HINT_REFRESH]) ||
|
||||
isset($this->_hints['fetched'][$parentDqlAlias][$fieldName]) &&
|
||||
! $value->isInitialized()
|
||||
) {
|
||||
// Is already PersistentCollection, but either REFRESH or FETCH-JOIN and UNINITIALIZED!
|
||||
$value->setDirty(false);
|
||||
$value->setInitialized(true);
|
||||
$value->unwrap()->clear();
|
||||
|
||||
$this->initializedCollections[$oid . $fieldName] = $value;
|
||||
} else {
|
||||
// Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN!
|
||||
$this->existingCollections[$oid . $fieldName] = $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
* @param array $data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function getEntityFromIdentityMap($className, array $data)
|
||||
{
|
||||
// TODO: Abstract this code and UnitOfWork::createEntity() equivalent?
|
||||
$class = $this->_metadataCache[$className];
|
||||
|
||||
/* @var $class ClassMetadata */
|
||||
if ($class->isIdentifierComposite) {
|
||||
$idHash = '';
|
||||
|
||||
foreach ($class->identifier as $fieldName) {
|
||||
$idHash .= ' ' . (isset($class->associationMappings[$fieldName])
|
||||
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
|
||||
: $data[$fieldName]);
|
||||
}
|
||||
|
||||
return $this->_uow->tryGetByIdHash(ltrim($idHash), $class->rootEntityName);
|
||||
} else if (isset($class->associationMappings[$class->identifier[0]])) {
|
||||
return $this->_uow->tryGetByIdHash($data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']], $class->rootEntityName);
|
||||
}
|
||||
|
||||
return $this->_uow->tryGetByIdHash($data[$class->identifier[0]], $class->rootEntityName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrates a single row in an SQL result set.
|
||||
*
|
||||
* @internal
|
||||
* First, the data of the row is split into chunks where each chunk contains data
|
||||
* that belongs to a particular component/class. Afterwards, all these chunks
|
||||
* are processed, one after the other. For each chunk of class data only one of the
|
||||
* following code paths is executed:
|
||||
*
|
||||
* Path A: The data chunk belongs to a joined/associated object and the association
|
||||
* is collection-valued.
|
||||
* Path B: The data chunk belongs to a joined/associated object and the association
|
||||
* is single-valued.
|
||||
* Path C: The data chunk belongs to a root result element/object that appears in the topmost
|
||||
* level of the hydrated result. A typical example are the objects of the type
|
||||
* specified by the FROM clause in a DQL query.
|
||||
*
|
||||
* @param array $row The data of the row to process.
|
||||
* @param array $result The result array to fill.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function hydrateRowData(array $row, array &$result)
|
||||
{
|
||||
// Initialize
|
||||
$id = $this->idTemplate; // initialize the id-memory
|
||||
$nonemptyComponents = array();
|
||||
// Split the row data into chunks of class data.
|
||||
$rowData = $this->gatherRowData($row, $id, $nonemptyComponents);
|
||||
|
||||
// reset result pointers for each data row
|
||||
$this->resultPointers = [];
|
||||
|
||||
// Hydrate the data chunks
|
||||
foreach ($rowData['data'] as $dqlAlias => $data) {
|
||||
$entityName = $this->_rsm->aliasMap[$dqlAlias];
|
||||
|
||||
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) {
|
||||
// It's a joined result
|
||||
|
||||
$parentAlias = $this->_rsm->parentAliasMap[$dqlAlias];
|
||||
// we need the $path to save into the identifier map which entities were already
|
||||
// seen for this parent-child relationship
|
||||
$path = $parentAlias . '.' . $dqlAlias;
|
||||
|
||||
// We have a RIGHT JOIN result here. Doctrine cannot hydrate RIGHT JOIN Object-Graphs
|
||||
if ( ! isset($nonemptyComponents[$parentAlias])) {
|
||||
// TODO: Add special case code where we hydrate the right join objects into identity map at least
|
||||
continue;
|
||||
}
|
||||
|
||||
$parentClass = $this->_metadataCache[$this->_rsm->aliasMap[$parentAlias]];
|
||||
$relationField = $this->_rsm->relationMap[$dqlAlias];
|
||||
$relation = $parentClass->associationMappings[$relationField];
|
||||
$reflField = $parentClass->reflFields[$relationField];
|
||||
|
||||
// Get a reference to the parent object to which the joined element belongs.
|
||||
if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) {
|
||||
$objectClass = $this->resultPointers[$parentAlias];
|
||||
$parentObject = $objectClass[key($objectClass)];
|
||||
} else if (isset($this->resultPointers[$parentAlias])) {
|
||||
$parentObject = $this->resultPointers[$parentAlias];
|
||||
} else {
|
||||
// Parent object of relation not found, mark as not-fetched again
|
||||
$element = $this->getEntity($data, $dqlAlias);
|
||||
|
||||
// Update result pointer and provide initial fetch data for parent
|
||||
$this->resultPointers[$dqlAlias] = $element;
|
||||
$rowData['data'][$parentAlias][$relationField] = $element;
|
||||
|
||||
// Mark as not-fetched again
|
||||
unset($this->_hints['fetched'][$parentAlias][$relationField]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$oid = spl_object_hash($parentObject);
|
||||
|
||||
// Check the type of the relation (many or single-valued)
|
||||
if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) {
|
||||
// PATH A: Collection-valued association
|
||||
$reflFieldValue = $reflField->getValue($parentObject);
|
||||
|
||||
if (isset($nonemptyComponents[$dqlAlias])) {
|
||||
$collKey = $oid . $relationField;
|
||||
if (isset($this->initializedCollections[$collKey])) {
|
||||
$reflFieldValue = $this->initializedCollections[$collKey];
|
||||
} else if ( ! isset($this->existingCollections[$collKey])) {
|
||||
$reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
|
||||
}
|
||||
|
||||
$indexExists = isset($this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]);
|
||||
$index = $indexExists ? $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false;
|
||||
$indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false;
|
||||
|
||||
if ( ! $indexExists || ! $indexIsValid) {
|
||||
if (isset($this->existingCollections[$collKey])) {
|
||||
// Collection exists, only look for the element in the identity map.
|
||||
if ($element = $this->getEntityFromIdentityMap($entityName, $data)) {
|
||||
$this->resultPointers[$dqlAlias] = $element;
|
||||
} else {
|
||||
unset($this->resultPointers[$dqlAlias]);
|
||||
}
|
||||
} else {
|
||||
$element = $this->getEntity($data, $dqlAlias);
|
||||
|
||||
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
||||
$indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]];
|
||||
$reflFieldValue->hydrateSet($indexValue, $element);
|
||||
$this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue;
|
||||
} else {
|
||||
$reflFieldValue->hydrateAdd($element);
|
||||
$reflFieldValue->last();
|
||||
$this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key();
|
||||
}
|
||||
// Update result pointer
|
||||
$this->resultPointers[$dqlAlias] = $element;
|
||||
}
|
||||
} else {
|
||||
// Update result pointer
|
||||
$this->resultPointers[$dqlAlias] = $reflFieldValue[$index];
|
||||
}
|
||||
} else if ( ! $reflFieldValue) {
|
||||
$reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
|
||||
} else if ($reflFieldValue instanceof PersistentCollection && $reflFieldValue->isInitialized() === false) {
|
||||
$reflFieldValue->setInitialized(true);
|
||||
}
|
||||
|
||||
} else {
|
||||
// PATH B: Single-valued association
|
||||
$reflFieldValue = $reflField->getValue($parentObject);
|
||||
|
||||
if ( ! $reflFieldValue || isset($this->_hints[Query::HINT_REFRESH]) || ($reflFieldValue instanceof Proxy && !$reflFieldValue->__isInitialized__)) {
|
||||
// we only need to take action if this value is null,
|
||||
// we refresh the entity or its an unitialized proxy.
|
||||
if (isset($nonemptyComponents[$dqlAlias])) {
|
||||
$element = $this->getEntity($data, $dqlAlias);
|
||||
$reflField->setValue($parentObject, $element);
|
||||
$this->_uow->setOriginalEntityProperty($oid, $relationField, $element);
|
||||
$targetClass = $this->_metadataCache[$relation['targetEntity']];
|
||||
|
||||
if ($relation['isOwningSide']) {
|
||||
// TODO: Just check hints['fetched'] here?
|
||||
// If there is an inverse mapping on the target class its bidirectional
|
||||
if ($relation['inversedBy']) {
|
||||
$inverseAssoc = $targetClass->associationMappings[$relation['inversedBy']];
|
||||
if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) {
|
||||
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($element, $parentObject);
|
||||
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $inverseAssoc['fieldName'], $parentObject);
|
||||
}
|
||||
} else if ($parentClass === $targetClass && $relation['mappedBy']) {
|
||||
// Special case: bi-directional self-referencing one-one on the same class
|
||||
$targetClass->reflFields[$relationField]->setValue($element, $parentObject);
|
||||
}
|
||||
} else {
|
||||
// For sure bidirectional, as there is no inverse side in unidirectional mappings
|
||||
$targetClass->reflFields[$relation['mappedBy']]->setValue($element, $parentObject);
|
||||
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject);
|
||||
}
|
||||
// Update result pointer
|
||||
$this->resultPointers[$dqlAlias] = $element;
|
||||
} else {
|
||||
$this->_uow->setOriginalEntityProperty($oid, $relationField, null);
|
||||
$reflField->setValue($parentObject, null);
|
||||
}
|
||||
// else leave $reflFieldValue null for single-valued associations
|
||||
} else {
|
||||
// Update result pointer
|
||||
$this->resultPointers[$dqlAlias] = $reflFieldValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// PATH C: Its a root result element
|
||||
$this->rootAliases[$dqlAlias] = true; // Mark as root alias
|
||||
$entityKey = $this->_rsm->entityMappings[$dqlAlias] ?: 0;
|
||||
|
||||
// if this row has a NULL value for the root result id then make it a null result.
|
||||
if ( ! isset($nonemptyComponents[$dqlAlias]) ) {
|
||||
if ($this->_rsm->isMixed) {
|
||||
$result[] = array($entityKey => null);
|
||||
} else {
|
||||
$result[] = null;
|
||||
}
|
||||
$resultKey = $this->resultCounter;
|
||||
++$this->resultCounter;
|
||||
continue;
|
||||
}
|
||||
|
||||
// check for existing result from the iterations before
|
||||
if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) {
|
||||
$element = $this->getEntity($data, $dqlAlias);
|
||||
|
||||
if ($this->_rsm->isMixed) {
|
||||
$element = array($entityKey => $element);
|
||||
}
|
||||
|
||||
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
||||
$resultKey = $row[$this->_rsm->indexByMap[$dqlAlias]];
|
||||
|
||||
if (isset($this->_hints['collection'])) {
|
||||
$this->_hints['collection']->hydrateSet($resultKey, $element);
|
||||
}
|
||||
|
||||
$result[$resultKey] = $element;
|
||||
} else {
|
||||
$resultKey = $this->resultCounter;
|
||||
++$this->resultCounter;
|
||||
|
||||
if (isset($this->_hints['collection'])) {
|
||||
$this->_hints['collection']->hydrateAdd($element);
|
||||
}
|
||||
|
||||
$result[] = $element;
|
||||
}
|
||||
|
||||
$this->identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey;
|
||||
|
||||
// Update result pointer
|
||||
$this->resultPointers[$dqlAlias] = $element;
|
||||
|
||||
} else {
|
||||
// Update result pointer
|
||||
$index = $this->identifierMap[$dqlAlias][$id[$dqlAlias]];
|
||||
$this->resultPointers[$dqlAlias] = $result[$index];
|
||||
$resultKey = $index;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) {
|
||||
$this->_uow->hydrationComplete();
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! isset($resultKey) ) {
|
||||
$this->resultCounter++;
|
||||
}
|
||||
|
||||
// Append scalar values to mixed result sets
|
||||
if (isset($rowData['scalars'])) {
|
||||
if ( ! isset($resultKey) ) {
|
||||
$resultKey = (isset($this->_rsm->indexByMap['scalars']))
|
||||
? $row[$this->_rsm->indexByMap['scalars']]
|
||||
: $this->resultCounter - 1;
|
||||
}
|
||||
|
||||
foreach ($rowData['scalars'] as $name => $value) {
|
||||
$result[$resultKey][$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Append new object to mixed result sets
|
||||
if (isset($rowData['newObjects'])) {
|
||||
if ( ! isset($resultKey) ) {
|
||||
$resultKey = $this->resultCounter - 1;
|
||||
}
|
||||
|
||||
|
||||
$scalarCount = (isset($rowData['scalars'])? count($rowData['scalars']): 0);
|
||||
|
||||
foreach ($rowData['newObjects'] as $objIndex => $newObject) {
|
||||
$class = $newObject['class'];
|
||||
$args = $newObject['args'];
|
||||
$obj = $class->newInstanceArgs($args);
|
||||
|
||||
if ($scalarCount == 0 && count($rowData['newObjects']) == 1 ) {
|
||||
$result[$resultKey] = $obj;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$resultKey][$objIndex] = $obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When executed in a hydrate() loop we may have to clear internal state to
|
||||
* decrease memory consumption.
|
||||
*
|
||||
* @param mixed $eventArgs
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function onClear($eventArgs)
|
||||
{
|
||||
parent::onClear($eventArgs);
|
||||
|
||||
$aliases = array_keys($this->identifierMap);
|
||||
$this->identifierMap = array();
|
||||
|
||||
foreach ($aliases as $alias) {
|
||||
$this->identifierMap[$alias] = array();
|
||||
}
|
||||
}
|
||||
}
|
||||
54
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ScalarHydrator.php
vendored
Normal file
54
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ScalarHydrator.php
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
/**
|
||||
* Hydrator that produces flat, rectangular results of scalar data.
|
||||
* The created result is almost the same as a regular SQL result set, except
|
||||
* that column names are mapped to field names and data type conversions take place.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
*/
|
||||
class ScalarHydrator extends AbstractHydrator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateAllData()
|
||||
{
|
||||
$result = array();
|
||||
|
||||
while ($data = $this->_stmt->fetch(\PDO::FETCH_ASSOC)) {
|
||||
$this->hydrateRowData($data, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateRowData(array $data, array &$result)
|
||||
{
|
||||
$result[] = $this->gatherScalarRowData($data);
|
||||
}
|
||||
}
|
||||
155
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php
vendored
Normal file
155
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php
vendored
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
use PDO;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Query;
|
||||
|
||||
class SimpleObjectHydrator extends AbstractHydrator
|
||||
{
|
||||
/**
|
||||
* @var ClassMetadata
|
||||
*/
|
||||
private $class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function prepare()
|
||||
{
|
||||
if (count($this->_rsm->aliasMap) !== 1) {
|
||||
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.");
|
||||
}
|
||||
|
||||
if ($this->_rsm->scalarMappings) {
|
||||
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.");
|
||||
}
|
||||
|
||||
$this->class = $this->getClassMetadata(reset($this->_rsm->aliasMap));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function cleanup()
|
||||
{
|
||||
parent::cleanup();
|
||||
|
||||
$this->_uow->triggerEagerLoads();
|
||||
$this->_uow->hydrationComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateAllData()
|
||||
{
|
||||
$result = array();
|
||||
|
||||
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$this->hydrateRowData($row, $result);
|
||||
}
|
||||
|
||||
$this->_em->getUnitOfWork()->triggerEagerLoads();
|
||||
|
||||
return $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;
|
||||
}
|
||||
|
||||
// Check if value is null before conversion (because some types convert null to something else)
|
||||
$valueIsNull = null === $value;
|
||||
|
||||
// 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]) || ! $valueIsNull) {
|
||||
$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;
|
||||
|
||||
if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) {
|
||||
$this->_uow->hydrationComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
58
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php
vendored
Normal file
58
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php
vendored
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal\Hydration;
|
||||
|
||||
use Doctrine\ORM\NoResultException;
|
||||
use Doctrine\ORM\NonUniqueResultException;
|
||||
|
||||
/**
|
||||
* Hydrator that hydrates a single scalar value from the result set.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
*/
|
||||
class SingleScalarHydrator extends AbstractHydrator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function hydrateAllData()
|
||||
{
|
||||
$data = $this->_stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$numRows = count($data);
|
||||
|
||||
if ($numRows === 0) {
|
||||
throw new NoResultException();
|
||||
}
|
||||
|
||||
if ($numRows > 1) {
|
||||
throw new NonUniqueResultException('The query returned multiple rows. Change the query or use a different result function like getScalarResult().');
|
||||
}
|
||||
|
||||
if (count($data[key($data)]) > 1) {
|
||||
throw new NonUniqueResultException('The query returned a row containing multiple columns. Change the query or use a different result function like getScalarResult().');
|
||||
}
|
||||
|
||||
$result = $this->gatherScalarRowData($data[key($data)]);
|
||||
|
||||
return array_shift($result);
|
||||
}
|
||||
}
|
||||
103
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/HydrationCompleteHandler.php
vendored
Normal file
103
vendor/doctrine/orm/lib/Doctrine/ORM/Internal/HydrationCompleteHandler.php
vendored
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
<?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
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Internal;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||
use Doctrine\ORM\Event\ListenersInvoker;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* Class, which can handle completion of hydration cycle and produce some of tasks.
|
||||
* In current implementation triggers deferred postLoad event.
|
||||
*
|
||||
* @author Artur Eshenbrener <strate@yandex.ru>
|
||||
* @since 2.5
|
||||
*/
|
||||
final class HydrationCompleteHandler
|
||||
{
|
||||
/**
|
||||
* @var ListenersInvoker
|
||||
*/
|
||||
private $listenersInvoker;
|
||||
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var array[]
|
||||
*/
|
||||
private $deferredPostLoadInvocations = array();
|
||||
|
||||
/**
|
||||
* Constructor for this object
|
||||
*
|
||||
* @param ListenersInvoker $listenersInvoker
|
||||
* @param EntityManagerInterface $em
|
||||
*/
|
||||
public function __construct(ListenersInvoker $listenersInvoker, EntityManagerInterface $em)
|
||||
{
|
||||
$this->listenersInvoker = $listenersInvoker;
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schedules invoking of postLoad entity to the very end of current hydration cycle.
|
||||
*
|
||||
* @param ClassMetadata $class
|
||||
* @param object $entity
|
||||
*/
|
||||
public function deferPostLoadInvoking(ClassMetadata $class, $entity)
|
||||
{
|
||||
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postLoad);
|
||||
|
||||
if ($invoke === ListenersInvoker::INVOKE_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->deferredPostLoadInvocations[] = array($class, $invoke, $entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should me called after any hydration cycle completed.
|
||||
*
|
||||
* Method fires all deferred invocations of postLoad events
|
||||
*/
|
||||
public function hydrationComplete()
|
||||
{
|
||||
$toInvoke = $this->deferredPostLoadInvocations;
|
||||
$this->deferredPostLoadInvocations = array();
|
||||
|
||||
foreach ($toInvoke as $classAndEntity) {
|
||||
list($class, $invoke, $entity) = $classAndEntity;
|
||||
|
||||
$this->listenersInvoker->invoke(
|
||||
$class,
|
||||
Events::postLoad,
|
||||
$entity,
|
||||
new LifecycleEventArgs($entity, $this->em),
|
||||
$invoke
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue