2007-10-23 01:47:05 +04:00
|
|
|
<?php
|
|
|
|
/*
|
2008-09-12 21:25:38 +04:00
|
|
|
* $Id: UnitOfWork.php 4947 2008-09-12 13:16:05Z romanb $
|
2007-10-23 01:47:05 +04:00
|
|
|
*
|
|
|
|
* 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-02-04 19:35:36 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM;
|
2008-05-30 16:09:24 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\Internal\CommitOrderCalculator;
|
|
|
|
use Doctrine\ORM\Internal\CommitOrderNode;
|
2009-02-02 14:55:50 +03:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
2009-02-06 20:16:39 +03:00
|
|
|
use Doctrine\ORM\Mapping;
|
|
|
|
use Doctrine\ORM\Persisters;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\Exceptions\UnitOfWorkException;
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2007-10-23 01:47:05 +04:00
|
|
|
/**
|
2008-07-04 20:32:19 +04:00
|
|
|
* The UnitOfWork is responsible for tracking changes to objects during an
|
2008-07-21 00:13:24 +04:00
|
|
|
* "object-level" transaction and for writing out changes to the database
|
2008-07-04 20:32:19 +04:00
|
|
|
* in the correct order.
|
2007-10-23 01:47:05 +04:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2009-01-22 22:38:10 +03:00
|
|
|
* @link www.doctrine-project.org
|
2008-05-01 13:41:47 +04:00
|
|
|
* @since 2.0
|
2009-01-03 22:50:13 +03:00
|
|
|
* @version $Revision$
|
2008-02-24 01:04:39 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class UnitOfWork
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
|
|
|
* An Entity is in managed state when it has a primary key/identifier (and
|
|
|
|
* therefore persistent state) and is managed by an EntityManager
|
|
|
|
* (registered in the identity map).
|
|
|
|
* In MANAGED state the entity is associated with an EntityManager that manages
|
|
|
|
* the persistent state of the Entity.
|
|
|
|
*/
|
|
|
|
const STATE_MANAGED = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An Entity is new if it does not yet have an identifier/primary key
|
|
|
|
* and is not (yet) managed by an EntityManager.
|
|
|
|
*/
|
|
|
|
const STATE_NEW = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A detached Entity is an instance with a persistent identity that is not
|
|
|
|
* (or no longer) associated with an EntityManager (and a UnitOfWork).
|
|
|
|
* This means its no longer in the identity map.
|
|
|
|
*/
|
|
|
|
const STATE_DETACHED = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A removed Entity instance is an instance with a persistent identity,
|
|
|
|
* associated with an EntityManager, whose persistent state has been
|
|
|
|
* deleted (or is scheduled for deletion).
|
|
|
|
*/
|
|
|
|
const STATE_DELETED = 4;
|
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
|
|
|
* The identity map that holds references to all managed entities that have
|
2008-03-05 14:24:33 +03:00
|
|
|
* an identity. The entities are grouped by their class name.
|
2008-05-01 13:41:47 +04:00
|
|
|
* Since all classes in a hierarchy must share the same identifier set,
|
|
|
|
* we always take the root class name of the hierarchy.
|
2008-02-24 01:04:39 +03:00
|
|
|
*
|
2008-05-01 13:41:47 +04:00
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_identityMap = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
/**
|
|
|
|
* Map of all identifiers. Keys are object ids.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_entityIdentifiers = array();
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
|
|
|
* Map of the original entity data of entities fetched from the database.
|
|
|
|
* Keys are object ids. This is used for calculating changesets at commit time.
|
|
|
|
* Note that PHPs "copy-on-write" behavior helps a lot with the potentially
|
|
|
|
* high memory usage.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_originalEntityData = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of data changes. Keys are object ids.
|
|
|
|
* Filled at the beginning of a commit() of the UnitOfWork and cleaned at the end.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_entityChangeSets = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The states of entities in this UnitOfWork.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_entityStates = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
/**
|
|
|
|
* Map of entities that are scheduled for dirty checking at commit time.
|
|
|
|
* This is only used if automatic dirty checking is disabled.
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_scheduledForDirtyCheck = array();
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* A list of all pending entity insertions.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
private $_entityInsertions = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* A list of all pending entity updates.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
private $_entityUpdates = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* A list of all pending entity deletions.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
private $_entityDeletions = array();
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* All pending collection deletions.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_collectionDeletions = array();
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* All pending collection creations.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_collectionCreations = array();
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All collection updates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_collectionUpdates = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of collections visited during a commit-phase of a UnitOfWork.
|
|
|
|
* At the end of the UnitOfWork all these collections will make new snapshots
|
|
|
|
* of their data.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_visitedCollections = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
2009-01-09 19:25:06 +03:00
|
|
|
* The EntityManager that "owns" this UnitOfWork instance.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\ORM\EntityManager
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_em;
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2008-06-05 23:01:58 +04:00
|
|
|
* The calculator used to calculate the order in which changes to
|
|
|
|
* entities need to be written to the database.
|
2008-02-24 01:04:39 +03:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\ORM\Internal\CommitOrderCalculator
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_commitOrderCalculator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The entity persister instances used to persist entity instances.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_persisters = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The collection persister instances used to persist collections.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_collectionPersisters = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-04 20:32:19 +04:00
|
|
|
/**
|
2009-01-09 19:25:06 +03:00
|
|
|
* Initializes a new UnitOfWork instance, bound to the given EntityManager.
|
2008-07-04 20:32:19 +04:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @param Doctrine\ORM\EntityManager $em
|
2008-07-04 20:32:19 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
public function __construct(EntityManager $em)
|
2008-07-04 20:32:19 +04:00
|
|
|
{
|
|
|
|
$this->_em = $em;
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: any benefit with lazy init?
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_commitOrderCalculator = new CommitOrderCalculator();
|
2008-07-04 20:32:19 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2009-01-29 20:00:44 +03:00
|
|
|
* Commits the UnitOfWork, executing all operations that have been postponed
|
2008-02-24 01:04:39 +03:00
|
|
|
* up to this point.
|
|
|
|
*/
|
2008-04-13 00:11:11 +04:00
|
|
|
public function commit()
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2009-02-02 14:55:50 +03:00
|
|
|
// Compute changes done since last commit
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->computeChangeSets();
|
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
if (empty($this->_entityInsertions) &&
|
|
|
|
empty($this->_entityDeletions) &&
|
|
|
|
empty($this->_entityUpdates) &&
|
2009-02-06 20:16:39 +03:00
|
|
|
empty($this->_collectionUpdates) &&
|
|
|
|
empty($this->_collectionDeletions)) {
|
2008-07-21 00:13:24 +04:00
|
|
|
return; // Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we need a commit order to maintain referential integrity
|
|
|
|
$commitOrder = $this->_getCommitOrder();
|
|
|
|
|
|
|
|
//TODO: begin transaction here?
|
|
|
|
|
|
|
|
foreach ($commitOrder as $class) {
|
|
|
|
$this->_executeInserts($class);
|
2008-08-22 13:05:14 +04:00
|
|
|
}
|
|
|
|
foreach ($commitOrder as $class) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_executeUpdates($class);
|
|
|
|
}
|
2008-08-16 23:40:59 +04:00
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
// Collection deletions (deletions of complete collections)
|
|
|
|
foreach ($this->_collectionDeletions as $collectionToDelete) {
|
|
|
|
$this->getCollectionPersister($collectionToDelete->getMapping())
|
|
|
|
->delete($collectionToDelete);
|
|
|
|
}
|
|
|
|
// Collection updates (deleteRows, updateRows, insertRows)
|
2009-02-02 14:55:50 +03:00
|
|
|
foreach ($this->_collectionUpdates as $collectionToUpdate) {
|
|
|
|
$this->getCollectionPersister($collectionToUpdate->getMapping())
|
|
|
|
->update($collectionToUpdate);
|
|
|
|
}
|
2009-01-29 20:00:44 +03:00
|
|
|
//TODO: collection recreations (insertions of complete collections)
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
// Entity deletions come last and need to be in reverse commit order
|
2009-01-29 20:00:44 +03:00
|
|
|
for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_executeDeletions($commitOrder[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: commit transaction here?
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
// Take new snapshots from visited collections
|
|
|
|
foreach ($this->_visitedCollections as $coll) {
|
2009-02-07 20:02:13 +03:00
|
|
|
$coll->takeSnapshot();
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
// Clear up
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_entityInsertions = array();
|
|
|
|
$this->_entityUpdates = array();
|
|
|
|
$this->_entityDeletions = array();
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->_entityChangeSets = array();
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_collectionUpdates = array();
|
2009-02-06 20:16:39 +03:00
|
|
|
$this->_collectionDeletions = array();
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_visitedCollections = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-09 19:25:06 +03:00
|
|
|
* Gets the changeset for an entity.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-01-09 19:25:06 +03:00
|
|
|
public function getEntityChangeSet($entity)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-01-09 19:25:06 +03:00
|
|
|
if (isset($this->_entityChangeSets[$oid])) {
|
|
|
|
return $this->_entityChangeSets[$oid];
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-02 14:55:50 +03:00
|
|
|
* Computes all the changes that have been done to entities and collections
|
2009-01-09 19:25:06 +03:00
|
|
|
* since the last commit and stores these changes in the _entityChangeSet map
|
|
|
|
* temporarily for access by the persisters, until the UoW commit is finished.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @param array $entities The entities for which to compute the changesets. If this
|
|
|
|
* parameter is not specified, the changesets of all entities in the identity
|
2009-01-29 20:00:44 +03:00
|
|
|
* map are computed if automatic dirty checking is enabled (the default).
|
|
|
|
* If automatic dirty checking is disabled, only those changesets will be
|
|
|
|
* computed that have been scheduled through scheduleForDirtyCheck().
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-01-29 20:00:44 +03:00
|
|
|
public function computeChangeSets(array $entities = null)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
|
|
|
$entitySet = array();
|
|
|
|
if ( ! is_null($entities)) {
|
|
|
|
foreach ($entities as $entity) {
|
2009-01-09 19:25:06 +03:00
|
|
|
$entitySet[get_class($entity)][] = $entity;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-01-24 19:56:44 +03:00
|
|
|
} else if ( ! $this->_em->getConfiguration()->getAutomaticDirtyChecking()) {
|
2009-01-29 20:00:44 +03:00
|
|
|
$entitySet = $this->_scheduledForDirtyCheck;
|
2008-12-18 17:08:11 +03:00
|
|
|
} else {
|
|
|
|
$entitySet = $this->_identityMap;
|
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
foreach ($entitySet as $className => $entities) {
|
|
|
|
$class = $this->_em->getClassMetadata($className);
|
2009-01-29 20:00:44 +03:00
|
|
|
if ( ! $class->isInheritanceTypeNone() && count($entities) > 0) {
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entities[0]));
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
foreach ($entities as $entity) {
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-01-07 20:46:02 +03:00
|
|
|
$state = $this->getEntityState($entity);
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
// Look for changes in the entity itself by comparing against the
|
|
|
|
// original data we have.
|
2009-01-07 20:46:02 +03:00
|
|
|
if ($state == self::STATE_MANAGED || $state == self::STATE_NEW) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$actualData = array();
|
|
|
|
foreach ($class->getReflectionProperties() as $name => $refProp) {
|
2009-02-02 14:55:50 +03:00
|
|
|
if ( ! $class->isIdentifier($name) || $class->isIdentifierNatural()) {
|
|
|
|
$actualData[$name] = $refProp->getValue($entity);
|
|
|
|
}
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
if ($class->isCollectionValuedAssociation($name)
|
|
|
|
&& ! is_null($actualData[$name])
|
|
|
|
&& ! ($actualData[$name] instanceof PersistentCollection)) {
|
2009-02-02 14:55:50 +03:00
|
|
|
//TODO: If $actualData[$name] is Collection then unwrap the array
|
|
|
|
$assoc = $class->getAssociationMapping($name);
|
2009-02-07 20:02:13 +03:00
|
|
|
echo PHP_EOL . "INJECTING PCOLL into $name" . PHP_EOL;
|
|
|
|
// Inject PersistentCollection
|
|
|
|
$coll = new PersistentCollection($this->_em, $assoc->getTargetEntityName(),
|
|
|
|
$actualData[$name] ? $actualData[$name] : array());
|
|
|
|
$coll->setOwner($entity, $assoc);
|
|
|
|
if ( ! $coll->isEmpty()) $coll->setDirty(true);
|
|
|
|
$class->getReflectionProperty($name)->setValue($entity, $coll);
|
|
|
|
$actualData[$name] = $coll;
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
if ( ! isset($this->_originalEntityData[$oid])) {
|
|
|
|
// Entity is either NEW or MANAGED but not yet fully persisted
|
|
|
|
// (only has an id). These result in an INSERT.
|
2009-01-08 14:23:24 +03:00
|
|
|
$this->_originalEntityData[$oid] = $actualData;
|
2009-02-06 20:16:39 +03:00
|
|
|
$this->_entityChangeSets[$oid] = array_map(
|
|
|
|
function($e) { return array(null, $e); },
|
|
|
|
$actualData
|
|
|
|
);
|
2008-12-18 17:08:11 +03:00
|
|
|
} else {
|
2009-01-09 19:25:06 +03:00
|
|
|
// Entity is "fully" MANAGED: it was already fully persisted before
|
|
|
|
// and we have a copy of the original data
|
2008-12-18 17:08:11 +03:00
|
|
|
$originalData = $this->_originalEntityData[$oid];
|
|
|
|
$changeSet = array();
|
2009-01-09 19:25:06 +03:00
|
|
|
$entityIsDirty = false;
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
foreach ($actualData as $propName => $actualValue) {
|
2009-02-02 14:55:50 +03:00
|
|
|
$orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
|
2008-12-18 17:08:11 +03:00
|
|
|
if (is_object($orgValue) && $orgValue !== $actualValue) {
|
2009-02-02 14:55:50 +03:00
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
2008-12-18 17:08:11 +03:00
|
|
|
} else if ($orgValue != $actualValue || (is_null($orgValue) xor is_null($actualValue))) {
|
2009-02-02 14:55:50 +03:00
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
|
|
|
|
if (isset($changeSet[$propName])) {
|
|
|
|
if ($class->hasAssociation($propName)) {
|
|
|
|
$assoc = $class->getAssociationMapping($propName);
|
|
|
|
if ($assoc->isOneToOne() && $assoc->isOwningSide()) {
|
|
|
|
$entityIsDirty = true;
|
2009-02-07 20:02:13 +03:00
|
|
|
} else if ($orgValue instanceof PersistentCollection) {
|
2009-02-06 20:16:39 +03:00
|
|
|
// A PersistentCollection was de-referenced, so delete it.
|
|
|
|
if ( ! in_array($orgValue, $this->_collectionDeletions, true)) {
|
|
|
|
$this->_collectionDeletions[] = $orgValue;
|
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$entityIsDirty = true;
|
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-01-08 14:23:24 +03:00
|
|
|
if ($changeSet) {
|
2009-01-09 19:25:06 +03:00
|
|
|
if ($entityIsDirty) {
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_entityUpdates[$oid] = $entity;
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
|
|
|
$this->_entityChangeSets[$oid] = $changeSet;
|
2009-01-08 14:23:24 +03:00
|
|
|
$this->_originalEntityData[$oid] = $actualData;
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
// Look for changes in associations of the entity
|
2009-01-29 20:00:44 +03:00
|
|
|
if ($state == self::STATE_MANAGED) {
|
|
|
|
foreach ($class->getAssociationMappings() as $assoc) {
|
|
|
|
$val = $actualData[$assoc->getSourceFieldName()];
|
|
|
|
if ( ! is_null($val)) {
|
|
|
|
$this->_computeAssociationChanges($assoc, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
/**
|
2009-02-02 14:55:50 +03:00
|
|
|
* Computes the changes of an association.
|
2009-01-09 19:25:06 +03:00
|
|
|
*
|
2009-02-07 20:02:13 +03:00
|
|
|
* @param AssociationMapping $assoc
|
|
|
|
* @param mixed $value The value of the association.
|
2009-01-09 19:25:06 +03:00
|
|
|
*/
|
2009-01-29 20:00:44 +03:00
|
|
|
private function _computeAssociationChanges($assoc, $value)
|
2009-01-09 19:25:06 +03:00
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
if ($value instanceof PersistentCollection && $value->isDirty()) {
|
|
|
|
if ($assoc->isOwningSide()) {
|
|
|
|
$this->_collectionUpdates[] = $value;
|
|
|
|
}
|
|
|
|
$this->_visitedCollections[] = $value;
|
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
if ( ! $assoc->isCascadeSave()) {
|
|
|
|
echo "NOT CASCADING INTO " . $assoc->getSourceFieldName() . PHP_EOL;
|
|
|
|
return; // "Persistence by reachability" only if save cascade specified
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look through the entities, and in any of their associations, for transient
|
|
|
|
// enities, recursively. ("Persistence by reachability")
|
2009-01-09 19:25:06 +03:00
|
|
|
if ($assoc->isOneToOne()) {
|
|
|
|
$value = array($value);
|
|
|
|
}
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->getTargetEntityName());
|
|
|
|
foreach ($value as $entry) {
|
|
|
|
$state = $this->getEntityState($entry);
|
|
|
|
$oid = spl_object_hash($entry);
|
|
|
|
if ($state == self::STATE_NEW) {
|
|
|
|
// Get identifier, if possible (not post-insert)
|
|
|
|
$idGen = $this->_em->getIdGenerator($targetClass->getClassName());
|
|
|
|
if ( ! $idGen->isPostInsertGenerator()) {
|
|
|
|
$idValue = $idGen->generate($entry);
|
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
2009-01-22 22:38:10 +03:00
|
|
|
if ( ! $idGen instanceof \Doctrine\ORM\Id\Assigned) {
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->_entityIdentifiers[$oid] = array($idValue);
|
|
|
|
$targetClass->getSingleIdReflectionProperty()->setValue($entry, $idValue);
|
|
|
|
} else {
|
|
|
|
$this->_entityIdentifiers[$oid] = $idValue;
|
|
|
|
}
|
|
|
|
$this->addToIdentityMap($entry);
|
|
|
|
}
|
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
// Collect the original data and changeset, recursing into associations.
|
2009-01-09 19:25:06 +03:00
|
|
|
$data = array();
|
2009-02-06 20:16:39 +03:00
|
|
|
$changeSet = array();
|
2009-01-09 19:25:06 +03:00
|
|
|
foreach ($targetClass->getReflectionProperties() as $name => $refProp) {
|
|
|
|
$data[$name] = $refProp->getValue($entry);
|
2009-02-06 20:16:39 +03:00
|
|
|
$changeSet[$name] = array(null, $data[$name]);
|
2009-02-07 20:02:13 +03:00
|
|
|
if ($targetClass->hasAssociation($name)) {
|
|
|
|
//echo "RECURSING INTO $name" . PHP_EOL;
|
|
|
|
//TODO: Prevent infinite recursion
|
|
|
|
$this->_computeAssociationChanges($targetClass->getAssociationMapping($name), $data[$name]);
|
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
2009-02-02 14:55:50 +03:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
// NEW entities are INSERTed within the current unit of work.
|
|
|
|
$this->_entityInsertions[$oid] = $entry;
|
2009-02-06 20:16:39 +03:00
|
|
|
$this->_entityChangeSets[$oid] = $changeSet;
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->_originalEntityData[$oid] = $data;
|
|
|
|
} else if ($state == self::STATE_DELETED) {
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("Deleted entity in collection detected during flush.");
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
|
|
|
// MANAGED associated entities are already taken into account
|
|
|
|
// during changeset calculation anyway, since they are in the identity map.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-07 17:48:40 +04:00
|
|
|
/**
|
|
|
|
* Executes all entity insertions for entities of the specified type.
|
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Mapping\ClassMetadata $class
|
2008-09-07 17:48:40 +04:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
private function _executeInserts($class)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: Maybe $persister->addInsert($entity) in the loop and
|
|
|
|
// $persister->executeInserts() at the end to allow easy prepared
|
|
|
|
// statement reuse and maybe bulk operations in the persister.
|
|
|
|
// Same for update/delete.
|
|
|
|
$className = $class->getClassName();
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2009-02-07 20:02:13 +03:00
|
|
|
foreach ($this->_entityInsertions as $entity) {
|
2008-12-18 17:08:11 +03:00
|
|
|
if (get_class($entity) == $className) {
|
2009-01-03 22:50:13 +03:00
|
|
|
$returnVal = $persister->insert($entity);
|
|
|
|
if ( ! is_null($returnVal)) {
|
2009-01-29 20:00:44 +03:00
|
|
|
// Persister returned a post-insert ID
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-01-08 14:23:24 +03:00
|
|
|
$idField = $class->getSingleIdentifierFieldName();
|
|
|
|
$class->getReflectionProperty($idField)->setValue($entity, $returnVal);
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_entityIdentifiers[$oid] = array($returnVal);
|
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
2009-01-08 14:23:24 +03:00
|
|
|
$this->_originalEntityData[$oid][$idField] = $returnVal;
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->addToIdentityMap($entity);
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
|
2008-09-07 17:48:40 +04:00
|
|
|
/**
|
|
|
|
* Executes all entity updates for entities of the specified type.
|
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Mapping\ClassMetadata $class
|
2008-09-07 17:48:40 +04:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
private function _executeUpdates($class)
|
|
|
|
{
|
|
|
|
$className = $class->getClassName();
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2009-02-07 20:02:13 +03:00
|
|
|
foreach ($this->_entityUpdates as $entity) {
|
2009-01-09 19:25:06 +03:00
|
|
|
if (get_class($entity) == $className) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$persister->update($entity);
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-09-07 17:48:40 +04:00
|
|
|
/**
|
|
|
|
* Executes all entity deletions for entities of the specified type.
|
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Mapping\ClassMetadata $class
|
2008-09-07 17:48:40 +04:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
private function _executeDeletions($class)
|
|
|
|
{
|
|
|
|
$className = $class->getClassName();
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2009-02-07 20:02:13 +03:00
|
|
|
foreach ($this->_entityDeletions as $entity) {
|
2009-01-09 19:25:06 +03:00
|
|
|
if (get_class($entity) == $className) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$persister->delete($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the commit order.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-09-07 17:48:40 +04:00
|
|
|
private function _getCommitOrder(array $entityChangeSet = null)
|
2009-02-07 20:02:13 +03:00
|
|
|
{
|
2008-09-07 17:48:40 +04:00
|
|
|
if (is_null($entityChangeSet)) {
|
2009-01-12 16:34:41 +03:00
|
|
|
$entityChangeSet = array_merge(
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_entityInsertions,
|
|
|
|
$this->_entityUpdates,
|
|
|
|
$this->_entityDeletions);
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
|
|
|
|
// TODO: We can cache computed commit orders in the metadata cache!
|
|
|
|
// Check cache at this point here!
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
// See if there are any new classes in the changeset, that are not in the
|
|
|
|
// commit order graph yet (dont have a node).
|
|
|
|
$newNodes = array();
|
|
|
|
foreach ($entityChangeSet as $entity) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$className = get_class($entity);
|
|
|
|
if ( ! $this->_commitOrderCalculator->hasNodeWithKey($className)) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_commitOrderCalculator->addNodeWithItem(
|
2008-12-18 17:08:11 +03:00
|
|
|
$className, // index/key
|
|
|
|
$this->_em->getClassMetadata($className) // item
|
2008-07-21 00:13:24 +04:00
|
|
|
);
|
2008-12-18 17:08:11 +03:00
|
|
|
$newNodes[] = $this->_commitOrderCalculator->getNodeForKey($className);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate dependencies for new nodes
|
|
|
|
foreach ($newNodes as $node) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$class = $node->getClass();
|
|
|
|
foreach ($class->getAssociationMappings() as $assocMapping) {
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: should skip target classes that are not in the changeset.
|
|
|
|
if ($assocMapping->isOwningSide()) {
|
2008-08-16 23:40:59 +04:00
|
|
|
$targetClass = $this->_em->getClassMetadata($assocMapping->getTargetEntityName());
|
2008-07-21 00:13:24 +04:00
|
|
|
$targetClassName = $targetClass->getClassName();
|
2009-02-07 20:02:13 +03:00
|
|
|
// If the target class does not yet have a node, create it
|
2008-07-21 00:13:24 +04:00
|
|
|
if ( ! $this->_commitOrderCalculator->hasNodeWithKey($targetClassName)) {
|
|
|
|
$this->_commitOrderCalculator->addNodeWithItem(
|
|
|
|
$targetClassName, // index/key
|
|
|
|
$targetClass // item
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// add dependency
|
|
|
|
$otherNode = $this->_commitOrderCalculator->getNodeForKey($targetClassName);
|
2008-08-22 13:05:14 +04:00
|
|
|
$otherNode->before($node);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_commitOrderCalculator->getCommitOrder();
|
|
|
|
}
|
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
|
|
|
* Register a new entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @todo Rename to scheduleForInsert().
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function registerNew($entity)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityUpdates[$oid])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw new DoctrineException("Dirty object can't be registered as new.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityDeletions[$oid])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw new DoctrineException("Removed object can't be registered as new.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityInsertions[$oid])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw new DoctrineException("Object already registered as new. Can't register twice.");
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_entityInsertions[$oid] = $entity;
|
2009-01-03 22:50:13 +03:00
|
|
|
if (isset($this->_entityIdentifiers[$oid])) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->addToIdentityMap($entity);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether an entity is registered as new on the unit of work.
|
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Entity $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return boolean
|
|
|
|
* @todo Rename to isScheduledForInsert().
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isRegisteredNew($entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
return isset($this->_entityInsertions[spl_object_hash($entity)]);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
|
|
|
* Registers a dirty entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Entity $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @todo Rename to scheduleForUpdate().
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function registerDirty($entity)
|
2008-04-13 00:11:11 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if ( ! isset($this->_entityIdentifiers[$oid])) {
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("Entity without identity "
|
2008-05-06 17:41:22 +04:00
|
|
|
. "can't be registered as dirty.");
|
|
|
|
}
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityDeletions[$oid])) {
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("Removed object can't be registered as dirty.");
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
if ( ! isset($this->_entityUpdates[$oid]) && ! isset($this->_entityInsertions[$oid])) {
|
|
|
|
$this->_entityUpdates[$oid] = $entity;
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether an entity is registered as dirty in the unit of work.
|
|
|
|
* Note: Is not very useful currently as dirty entities are only registered
|
|
|
|
* at commit time.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
* @return boolean
|
|
|
|
* @todo Rename to isScheduledForUpdate().
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isRegisteredDirty($entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
return isset($this->_entityUpdates[spl_object_hash($entity)]);
|
2008-05-01 13:41:47 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
2008-04-13 00:11:11 +04:00
|
|
|
* Registers a deleted entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @todo Rename to scheduleForDelete().
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function registerDeleted($entity)
|
2008-04-13 00:11:11 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ( ! $this->isInIdentityMap($entity)) {
|
2008-05-06 17:41:22 +04:00
|
|
|
return;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
$this->removeFromIdentityMap($entity);
|
2009-01-03 22:50:13 +03:00
|
|
|
$className = get_class($entity);
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityInsertions[$oid])) {
|
|
|
|
unset($this->_entityInsertions[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
return; // entity has not been persisted yet, so nothing more to do.
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-09-07 17:48:40 +04:00
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityUpdates[$oid])) {
|
|
|
|
unset($this->_entityUpdates[$oid]);
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2009-02-07 20:02:13 +03:00
|
|
|
if ( ! isset($this->_entityDeletions[$oid])) {
|
|
|
|
$this->_entityDeletions[$oid] = $entity;
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
|
|
|
|
2007-10-23 01:47:05 +04:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Checks whether an entity is registered as removed/deleted with the unit
|
|
|
|
* of work.
|
2007-10-23 01:47:05 +04:00
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Entity $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return boolean
|
|
|
|
* @todo Rename to isScheduledForDelete().
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isRegisteredRemoved($entity)
|
2007-10-23 01:47:05 +04:00
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
return isset($this->_entityDeletions[spl_object_hash($entity)]);
|
2007-10-23 01:47:05 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Detaches an entity from the persistence management. It's persistence will
|
|
|
|
* no longer be managed by Doctrine.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param integer $oid object identifier
|
|
|
|
* @return boolean whether ot not the operation was successful
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function detach($entity)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
$this->removeFromIdentityMap($entity);
|
2009-02-07 20:02:13 +03:00
|
|
|
unset($this->_entityInsertions[$oid], $this->_entityUpdates[$oid],
|
|
|
|
$this->_entityDeletions[$oid], $this->_entityIdentifiers[$oid],
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_entityStates[$oid]);
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Enter description here...
|
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Entity $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return unknown
|
|
|
|
* @todo Rename to isScheduled()
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isEntityRegistered($entity)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-02-07 20:02:13 +03:00
|
|
|
return isset($this->_entityInsertions[$oid]) ||
|
|
|
|
isset($this->_entityUpdates[$oid]) ||
|
|
|
|
isset($this->_entityDeletions[$oid]);
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
/**
|
2008-03-05 14:24:33 +03:00
|
|
|
* Detaches all currently managed entities.
|
2008-07-21 00:13:24 +04:00
|
|
|
* Alternatively, if an entity class name is given, all entities of that type
|
|
|
|
* (or subtypes) are detached. Don't forget that entities are registered in
|
|
|
|
* the identity map with the name of the root entity class. So calling detachAll()
|
|
|
|
* with a class name that is not the name of a root entity has no effect.
|
2008-03-05 14:24:33 +03:00
|
|
|
*
|
|
|
|
* @return integer The number of detached entities.
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function detachAll($entityName = null)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: what do do with new/dirty/removed lists?
|
|
|
|
$numDetached = 0;
|
|
|
|
if ($entityName !== null && isset($this->_identityMap[$entityName])) {
|
|
|
|
$numDetached = count($this->_identityMap[$entityName]);
|
2009-01-03 22:50:13 +03:00
|
|
|
foreach ($this->_identityMap[$entityName] as $entity) {
|
|
|
|
$this->detach($entity);
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_identityMap[$entityName] = array();
|
|
|
|
} else {
|
|
|
|
$numDetached = count($this->_identityMap);
|
|
|
|
$this->_identityMap = array();
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_entityInsertions = array();
|
|
|
|
$this->_entityUpdates = array();
|
|
|
|
$this->_entityDeletions = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
|
2008-03-05 14:24:33 +03:00
|
|
|
return $numDetached;
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
/**
|
2008-03-05 14:24:33 +03:00
|
|
|
* Registers an entity in the identity map.
|
2008-07-21 00:13:24 +04:00
|
|
|
* Note that entities in a hierarchy are registered with the class name of
|
|
|
|
* the root entity.
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @param Doctrine\ORM\Entity $entity The entity to register.
|
2008-03-05 14:24:33 +03:00
|
|
|
* @return boolean TRUE if the registration was successful, FALSE if the identity of
|
|
|
|
* the entity in question is already managed.
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function addToIdentityMap($entity)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
$classMetadata = $this->_em->getClassMetadata(get_class($entity));
|
2009-01-03 22:50:13 +03:00
|
|
|
$idHash = $this->getIdentifierHash($this->_entityIdentifiers[spl_object_hash($entity)]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("Entity with oid '" . spl_object_hash($entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
. "' has no identity and therefore can't be added to the identity map.");
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
$className = $classMetadata->getRootClassName();
|
2008-05-01 13:41:47 +04:00
|
|
|
if (isset($this->_identityMap[$className][$idHash])) {
|
2008-02-28 18:30:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-01 13:41:47 +04:00
|
|
|
$this->_identityMap[$className][$idHash] = $entity;
|
2008-02-28 18:30:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
|
|
|
* Gets the state of an entity within the current unit of work.
|
|
|
|
*
|
|
|
|
* @param Doctrine\ORM\Entity $entity
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getEntityState($entity)
|
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-01-04 19:15:32 +03:00
|
|
|
if ( ! isset($this->_entityStates[$oid])) {
|
2009-02-07 20:02:13 +03:00
|
|
|
if (isset($this->_entityIdentifiers[$oid]) && ! isset($this->_entityInsertions[$oid])) {
|
2009-01-04 19:15:32 +03:00
|
|
|
$this->_entityStates[$oid] = self::STATE_DETACHED;
|
|
|
|
} else {
|
|
|
|
$this->_entityStates[$oid] = self::STATE_NEW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->_entityStates[$oid];
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
2009-01-04 19:15:32 +03:00
|
|
|
* Removes an entity from the identity map. This effectively detaches the
|
|
|
|
* entity from the persistence management of Doctrine.
|
2008-05-17 16:22:24 +04:00
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\ORM\Entity $entity
|
|
|
|
* @return boolean
|
2008-05-17 16:22:24 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function removeFromIdentityMap($entity)
|
2008-03-05 14:24:33 +03:00
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
$classMetadata = $this->_em->getClassMetadata(get_class($entity));
|
2009-01-04 19:15:32 +03:00
|
|
|
$idHash = $this->getIdentifierHash($this->_entityIdentifiers[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("Entity with oid '" . spl_object_hash($entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
. "' has no identity and therefore can't be removed from the identity map.");
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
$className = $classMetadata->getRootClassName();
|
2008-05-01 13:41:47 +04:00
|
|
|
if (isset($this->_identityMap[$className][$idHash])) {
|
|
|
|
unset($this->_identityMap[$className][$idHash]);
|
2009-01-04 19:15:32 +03:00
|
|
|
$this->_entityStates[$oid] = self::STATE_DETACHED;
|
2008-03-05 14:24:33 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
2009-01-04 19:15:32 +03:00
|
|
|
* Gets an entity in the identity map by its identifier hash.
|
2008-05-17 16:22:24 +04:00
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param string $idHash
|
|
|
|
* @param string $rootClassName
|
2009-02-06 20:16:39 +03:00
|
|
|
* @return object
|
2008-05-17 16:22:24 +04:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function getByIdHash($idHash, $rootClassName)
|
2008-03-05 14:24:33 +03:00
|
|
|
{
|
2008-05-01 13:41:47 +04:00
|
|
|
return $this->_identityMap[$rootClassName][$idHash];
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to get an entity by its identifier hash. If no entity is found for
|
|
|
|
* the given hash, FALSE is returned.
|
|
|
|
*
|
2009-01-04 19:15:32 +03:00
|
|
|
* @param string $idHash
|
|
|
|
* @param string $rootClassName
|
2008-12-18 17:08:11 +03:00
|
|
|
* @return mixed The found entity or FALSE.
|
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function tryGetByIdHash($idHash, $rootClassName)
|
|
|
|
{
|
|
|
|
if ($this->containsIdHash($idHash, $rootClassName)) {
|
|
|
|
return $this->getByIdHash($idHash, $rootClassName);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
|
|
|
* Gets the identifier hash for a set of identifier values.
|
2008-07-21 00:13:24 +04:00
|
|
|
* The hash is just a concatenation of the identifier values.
|
|
|
|
* The identifiers are concatenated with a space.
|
|
|
|
*
|
|
|
|
* Note that this method always returns a string. If the given array is
|
|
|
|
* empty, an empty string is returned.
|
2008-05-17 16:22:24 +04:00
|
|
|
*
|
|
|
|
* @param array $id
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return string The hash.
|
2008-05-17 16:22:24 +04:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function getIdentifierHash(array $id)
|
|
|
|
{
|
|
|
|
return implode(' ', $id);
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-01 13:41:47 +04:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Checks whether an entity is registered in the identity map of the
|
|
|
|
* UnitOfWork.
|
2008-05-01 13:41:47 +04:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @param object $entity
|
2008-05-01 13:41:47 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function isInIdentityMap($entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if ( ! isset($this->_entityIdentifiers[$oid])) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
$classMetadata = $this->_em->getClassMetadata(get_class($entity));
|
2009-01-03 22:50:13 +03:00
|
|
|
$idHash = $this->getIdentifierHash($this->_entityIdentifiers[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2008-05-01 13:41:47 +04:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
return isset($this->_identityMap
|
2008-12-18 17:08:11 +03:00
|
|
|
[$classMetadata->getRootClassName()]
|
2008-07-21 00:13:24 +04:00
|
|
|
[$idHash]
|
|
|
|
);
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
|
|
|
* Checks whether an identifier hash exists in the identity map.
|
|
|
|
*
|
|
|
|
* @param string $idHash
|
|
|
|
* @param string $rootClassName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function containsIdHash($idHash, $rootClassName)
|
2008-04-13 00:11:11 +04:00
|
|
|
{
|
2008-05-01 13:41:47 +04:00
|
|
|
return isset($this->_identityMap[$rootClassName][$idHash]);
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves an entity as part of the current unit of work.
|
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* @param object $entity The entity to save.
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function save($entity)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
$insertNow = array();
|
|
|
|
$visited = array();
|
|
|
|
$this->_doSave($entity, $visited, $insertNow);
|
|
|
|
if ( ! empty($insertNow)) {
|
2008-09-07 17:48:40 +04:00
|
|
|
// We have no choice. This means that there are new entities
|
2009-01-29 20:00:44 +03:00
|
|
|
// with a post-insert ID generation strategy.
|
|
|
|
$this->computeChangeSets($insertNow);
|
2008-07-27 23:38:56 +04:00
|
|
|
$commitOrder = $this->_getCommitOrder($insertNow);
|
|
|
|
foreach ($commitOrder as $class) {
|
|
|
|
$this->_executeInserts($class);
|
|
|
|
}
|
2009-02-07 20:02:13 +03:00
|
|
|
// remove them from _entityInsertions and _entityChangeSets
|
|
|
|
$this->_entityInsertions = array_diff_key($this->_entityInsertions, $insertNow);
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->_entityChangeSets = array_diff_key($this->_entityChangeSets, $insertNow);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves an entity as part of the current unit of work.
|
|
|
|
* This method is internally called during save() cascades as it tracks
|
|
|
|
* the already visited entities to prevent infinite recursions.
|
|
|
|
*
|
2009-01-09 19:25:06 +03:00
|
|
|
* @param object $entity The entity to save.
|
2009-01-04 19:15:32 +03:00
|
|
|
* @param array $visited The already visited entities.
|
2009-01-29 20:00:44 +03:00
|
|
|
* @param array $insertNow The entities that must be immediately inserted because of
|
|
|
|
* post-insert ID generation.
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
private function _doSave($entity, array &$visited, array &$insertNow)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
if (isset($visited[$oid])) {
|
2008-07-21 00:13:24 +04:00
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
$visited[$oid] = $entity; // mark visited
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
switch ($this->getEntityState($entity)) {
|
|
|
|
case self::STATE_MANAGED:
|
2009-01-29 20:00:44 +03:00
|
|
|
// nothing to do, except if automatic dirty checking is disabled
|
|
|
|
if ( ! $this->_em->getConfiguration()->getAutomaticDirtyChecking()) {
|
|
|
|
$this->scheduleForDirtyCheck($entity);
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_NEW:
|
2009-01-03 22:50:13 +03:00
|
|
|
$idGen = $this->_em->getIdGenerator($class->getClassName());
|
|
|
|
if ($idGen->isPostInsertGenerator()) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$insertNow[$oid] = $entity;
|
2008-07-21 00:13:24 +04:00
|
|
|
} else {
|
2009-01-03 22:50:13 +03:00
|
|
|
$idValue = $idGen->generate($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
2009-01-22 22:38:10 +03:00
|
|
|
if ( ! $idGen instanceof \Doctrine\ORM\Id\Assigned) {
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->_entityIdentifiers[$oid] = array($idValue);
|
2009-01-03 22:50:13 +03:00
|
|
|
$class->getSingleIdReflectionProperty()->setValue($entity, $idValue);
|
2009-01-09 19:25:06 +03:00
|
|
|
} else {
|
|
|
|
$this->_entityIdentifiers[$oid] = $idValue;
|
2009-01-03 22:50:13 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2008-09-07 17:48:40 +04:00
|
|
|
$this->registerNew($entity);
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_DETACHED:
|
2009-02-04 19:35:36 +03:00
|
|
|
throw new DoctrineException("Behavior of save() for a detached entity "
|
2008-07-21 00:13:24 +04:00
|
|
|
. "is not yet defined.");
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_DELETED:
|
2009-01-09 19:25:06 +03:00
|
|
|
// entity becomes managed again
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($this->isRegisteredRemoved($entity)) {
|
|
|
|
//TODO: better a method for this?
|
2009-02-07 20:02:13 +03:00
|
|
|
unset($this->_entityDeletions[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
} else {
|
|
|
|
//FIXME: There's more to think of here...
|
|
|
|
$this->registerNew($entity);
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
|
|
|
//TODO: throw UnitOfWorkException::invalidEntityState()
|
2009-02-04 19:35:36 +03:00
|
|
|
throw new DoctrineException("Encountered invalid entity state.");
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_cascadeSave($entity, $visited, $insertNow);
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an entity as part of the current unit of work.
|
|
|
|
*
|
2009-01-09 19:25:06 +03:00
|
|
|
* @param object $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public function delete($entity)
|
2008-07-10 21:17:58 +04:00
|
|
|
{
|
2009-01-09 19:25:06 +03:00
|
|
|
$visited = array();
|
|
|
|
$this->_doDelete($entity, $visited);
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-09-07 17:48:40 +04:00
|
|
|
/**
|
2009-01-29 20:00:44 +03:00
|
|
|
* Deletes an entity as part of the current unit of work.
|
|
|
|
* This method is internally called during delete() cascades as it tracks
|
|
|
|
* the already visited entities to prevent infinite recursions.
|
2008-09-07 17:48:40 +04:00
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* @param object $entity
|
2008-09-07 17:48:40 +04:00
|
|
|
* @param array $visited
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
private function _doDelete($entity, array &$visited)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
if (isset($visited[$oid])) {
|
2008-07-21 00:13:24 +04:00
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
$visited[$oid] = $entity; // mark visited
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
switch ($this->getEntityState($entity)) {
|
|
|
|
case self::STATE_NEW:
|
|
|
|
case self::STATE_DELETED:
|
2009-01-29 20:00:44 +03:00
|
|
|
// nothing to do
|
2008-07-21 00:13:24 +04:00
|
|
|
break;
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_MANAGED:
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->registerDeleted($entity);
|
|
|
|
break;
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_DETACHED:
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("A detached entity can't be deleted.");
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
2009-01-22 22:38:10 +03:00
|
|
|
throw new DoctrineException("Encountered invalid entity state.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
$this->_cascadeDelete($entity, $visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades the save operation to associated entities.
|
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* @param object $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param array $visited
|
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
private function _cascadeSave($entity, array &$visited, array &$insertNow)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
foreach ($class->getAssociationMappings() as $assocMapping) {
|
2008-07-21 00:13:24 +04:00
|
|
|
if ( ! $assocMapping->isCascadeSave()) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
$relatedEntities = $class->getReflectionProperty($assocMapping->getSourceFieldName())
|
|
|
|
->getValue($entity);
|
2009-01-29 20:00:44 +03:00
|
|
|
if (($relatedEntities instanceof \Doctrine\Common\Collections\Collection || is_array($relatedEntities))
|
2009-01-09 19:25:06 +03:00
|
|
|
&& count($relatedEntities) > 0) {
|
2008-07-21 00:13:24 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
|
|
|
$this->_doSave($relatedEntity, $visited, $insertNow);
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
} else if (is_object($relatedEntities)) {
|
|
|
|
$this->_doSave($relatedEntities, $visited, $insertNow);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 19:15:32 +03:00
|
|
|
/**
|
|
|
|
* Cascades the delete operation to associated entities.
|
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* @param object $entity
|
2009-01-04 19:15:32 +03:00
|
|
|
*/
|
2009-01-29 20:00:44 +03:00
|
|
|
private function _cascadeDelete($entity, array &$visited)
|
2008-07-10 21:17:58 +04:00
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
foreach ($class->getAssociationMappings() as $assocMapping) {
|
|
|
|
if ( ! $assocMapping->isCascadeDelete()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$relatedEntities = $class->getReflectionProperty($assocMapping->getSourceFieldName())
|
|
|
|
->getValue($entity);
|
2009-02-06 20:16:39 +03:00
|
|
|
if ($relatedEntities instanceof \Doctrine\Common\Collections\Collection || is_array($relatedEntities)
|
|
|
|
&& count($relatedEntities) > 0) {
|
2009-01-04 19:15:32 +03:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->_doDelete($relatedEntity, $visited);
|
2009-01-04 19:15:32 +03:00
|
|
|
}
|
|
|
|
} else if (is_object($relatedEntities)) {
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->_doDelete($relatedEntities, $visited);
|
2009-01-04 19:15:32 +03:00
|
|
|
}
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2009-01-04 19:15:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the CommitOrderCalculator used by the UnitOfWork to order commits.
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Internal\CommitOrderCalculator
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function getCommitOrderCalculator()
|
|
|
|
{
|
|
|
|
return $this->_commitOrderCalculator;
|
|
|
|
}
|
|
|
|
|
2009-01-04 19:15:32 +03:00
|
|
|
/**
|
|
|
|
* Closes the UnitOfWork.
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
$this->_commitOrderCalculator->clear();
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function scheduleCollectionUpdate(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
$this->_collectionUpdates[] = $coll;
|
|
|
|
}
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function isCollectionScheduledForUpdate(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function scheduleCollectionDeletion(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
//TODO: if $coll is already scheduled for recreation ... what to do?
|
|
|
|
// Just remove $coll from the scheduled recreations?
|
|
|
|
$this->_collectionDeletions[] = $coll;
|
|
|
|
}
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function isCollectionScheduledForDeletion(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function scheduleCollectionRecreation(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
$this->_collectionRecreations[] = $coll;
|
|
|
|
}
|
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function isCollectionScheduledForRecreation(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* Creates an entity. Used for reconstitution of entities during hydration.
|
2008-07-10 21:17:58 +04:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @param string $className The name of the entity class.
|
|
|
|
* @param array $data The data for the entity.
|
2009-01-29 20:00:44 +03:00
|
|
|
* @return object
|
2009-01-03 22:50:13 +03:00
|
|
|
* @internal Performance-sensitive method.
|
2008-07-10 21:17:58 +04:00
|
|
|
*/
|
2009-01-03 22:50:13 +03:00
|
|
|
public function createEntity($className, array $data, $query = null)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
|
|
|
$className = $this->_inferCorrectClassName($data, $className);
|
2009-02-17 13:54:18 +03:00
|
|
|
$class = $this->_em->getClassMetadata($className);
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
$id = array();
|
2009-02-17 13:54:18 +03:00
|
|
|
if ($class->isIdentifierComposite()) {
|
|
|
|
$identifierFieldNames = $class->getIdentifier();
|
2008-12-18 17:08:11 +03:00
|
|
|
foreach ($identifierFieldNames as $fieldName) {
|
|
|
|
$id[] = $data[$fieldName];
|
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
$idHash = $this->getIdentifierHash($id);
|
|
|
|
} else {
|
2009-02-17 13:54:18 +03:00
|
|
|
$id = array($data[$class->getSingleIdentifierFieldName()]);
|
2009-01-03 22:50:13 +03:00
|
|
|
$idHash = $id[0];
|
|
|
|
}
|
2009-02-17 13:54:18 +03:00
|
|
|
$entity = $this->tryGetByIdHash($idHash, $class->getRootClassName());
|
2009-01-03 22:50:13 +03:00
|
|
|
if ($entity) {
|
|
|
|
$oid = spl_object_hash($entity);
|
2009-02-17 13:54:18 +03:00
|
|
|
$this->_mergeData($entity, $data, $class/*, $query->getHint('doctrine.refresh')*/);
|
2009-01-03 22:50:13 +03:00
|
|
|
return $entity;
|
2008-12-18 17:08:11 +03:00
|
|
|
} else {
|
|
|
|
$entity = new $className;
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-02-17 13:54:18 +03:00
|
|
|
/*if ($class->hasLazySingleValuedAssociations()) {
|
|
|
|
foreach ($class->getLazyAssociations() as $lazyAssoc) {
|
|
|
|
// Inject VirtualProxy
|
|
|
|
$prop = $class->getReflectionProperty($lazyAssoc->getSourceFieldName());
|
|
|
|
$prop->setValue($entity, new \Doctrine\ORM\VirtualProxy($entity, $lazyAssoc, $prop));
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
$this->_mergeData($entity, $data, $class, true);
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_entityIdentifiers[$oid] = $id;
|
|
|
|
$this->addToIdentityMap($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_originalEntityData[$oid] = $data;
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
|
|
|
|
/**
|
2008-12-18 17:08:11 +03:00
|
|
|
* Merges the given data into the given entity, optionally overriding
|
|
|
|
* local changes.
|
2008-07-10 21:17:58 +04:00
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* @param object $entity
|
2008-12-18 17:08:11 +03:00
|
|
|
* @param array $data
|
|
|
|
* @param boolean $overrideLocalChanges
|
2009-02-17 13:54:18 +03:00
|
|
|
* @todo Consider moving to ClassMetadata for a little performance improvement.
|
2008-07-10 21:17:58 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
private function _mergeData($entity, array $data, $class, $overrideLocalChanges = false) {
|
|
|
|
if ($overrideLocalChanges) {
|
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
$class->getReflectionProperty($field)->setValue($entity, $value);
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2008-12-18 17:08:11 +03:00
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
$currentValue = $class->getReflectionProperty($field)->getValue($entity);
|
2009-01-03 22:50:13 +03:00
|
|
|
if ( ! isset($this->_originalEntityData[$oid][$field]) ||
|
|
|
|
$currentValue == $this->_originalEntityData[$oid][$field]) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$class->getReflectionProperty($field)->setValue($entity, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the dataset for a discriminator column to determine the correct
|
|
|
|
* class to instantiate. If no discriminator column is found, the given
|
|
|
|
* classname will be returned.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param string $className
|
|
|
|
* @return string The name of the class to instantiate.
|
|
|
|
*/
|
|
|
|
private function _inferCorrectClassName(array $data, $className)
|
|
|
|
{
|
|
|
|
$class = $this->_em->getClassMetadata($className);
|
2009-01-05 23:18:56 +03:00
|
|
|
$discCol = $class->getDiscriminatorColumn();
|
2008-12-18 17:08:11 +03:00
|
|
|
if ( ! $discCol) {
|
|
|
|
return $className;
|
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
$discMap = $class->getDiscriminatorMap();
|
|
|
|
if (isset($data[$discCol['name']], $discMap[$data[$discCol['name']]])) {
|
|
|
|
return $discMap[$data[$discCol['name']]];
|
2008-12-18 17:08:11 +03:00
|
|
|
} else {
|
|
|
|
return $className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the identity map of the UnitOfWork.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIdentityMap()
|
|
|
|
{
|
|
|
|
return $this->_identityMap;
|
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the original data of an entity. The original data is the data that was
|
|
|
|
* present at the time the entity was reconstituted from the database.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getOriginalEntityData($entity)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($this->_originalEntityData[$oid])) {
|
|
|
|
return $this->_originalEntityData[$oid];
|
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
* For hydration purposes only.
|
|
|
|
*
|
|
|
|
* Sets a property of the original data array of an entity.
|
|
|
|
*
|
|
|
|
* @param string $oid
|
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setOriginalEntityProperty($oid, $property, $value)
|
|
|
|
{
|
|
|
|
$this->_originalEntityData[$oid][$property] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the identifier of an entity.
|
2009-01-09 19:25:06 +03:00
|
|
|
* The returned value is always an array of identifier values. If the entity
|
|
|
|
* has a composite primary key then the identifier values are in the same
|
|
|
|
* order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames().
|
2009-01-03 22:50:13 +03:00
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @return array The identifier values.
|
|
|
|
*/
|
|
|
|
public function getEntityIdentifier($entity)
|
|
|
|
{
|
|
|
|
return $this->_entityIdentifiers[spl_object_hash($entity)];
|
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function tryGetById($id, $rootClassName)
|
|
|
|
{
|
|
|
|
$idHash = $this->getIdentifierHash((array)$id);
|
|
|
|
if (isset($this->_identityMap[$rootClassName][$idHash])) {
|
|
|
|
return $this->_identityMap[$rootClassName][$idHash];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
public function scheduleForDirtyCheck($entity)
|
|
|
|
{
|
|
|
|
$rootClassName = $this->_em->getClassMetadata(get_class($entity))->getRootClassName();
|
|
|
|
$this->_scheduledForDirtyCheck[$rootClassName] = $entity;
|
|
|
|
}
|
|
|
|
|
2009-01-12 16:34:41 +03:00
|
|
|
/**
|
|
|
|
* Calculates the size of the UnitOfWork. The size of the UnitOfWork is the
|
|
|
|
* number of entities in the identity map.
|
2009-01-29 20:00:44 +03:00
|
|
|
*
|
|
|
|
* @return integer
|
2009-01-12 16:34:41 +03:00
|
|
|
*/
|
|
|
|
public function size()
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
foreach ($this->_identityMap as $entitySet) $count += count($entitySet);
|
|
|
|
return $count;
|
|
|
|
}
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EntityPersister for an Entity.
|
|
|
|
*
|
|
|
|
* This is usually not of interest for users, mainly for internal use.
|
|
|
|
*
|
|
|
|
* @param string $entityName The name of the Entity.
|
|
|
|
* @return Doctrine\ORM\Persister\AbstractEntityPersister
|
|
|
|
*/
|
|
|
|
public function getEntityPersister($entityName)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_persisters[$entityName])) {
|
|
|
|
$class = $this->_em->getClassMetadata($entityName);
|
|
|
|
if ($class->isInheritanceTypeJoined()) {
|
2009-02-06 20:16:39 +03:00
|
|
|
$persister = new Persisters\JoinedSubclassPersister($this->_em, $class);
|
2009-02-02 14:55:50 +03:00
|
|
|
} else {
|
2009-02-06 20:16:39 +03:00
|
|
|
$persister = new Persisters\StandardEntityPersister($this->_em, $class);
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
|
|
|
$this->_persisters[$entityName] = $persister;
|
|
|
|
}
|
|
|
|
return $this->_persisters[$entityName];
|
|
|
|
}
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
/**
|
|
|
|
* Gets a collection persister for a collection-valued association.
|
|
|
|
*
|
|
|
|
* @param AssociationMapping $association
|
|
|
|
* @return AbstractCollectionPersister
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
public function getCollectionPersister($association)
|
|
|
|
{
|
|
|
|
$type = get_class($association);
|
|
|
|
if ( ! isset($this->_collectionPersisters[$type])) {
|
2009-02-06 20:16:39 +03:00
|
|
|
if ($association instanceof Mapping\OneToManyMapping) {
|
|
|
|
$persister = new Persisters\OneToManyPersister($this->_em);
|
|
|
|
} else if ($association instanceof Mapping\ManyToManyMapping) {
|
|
|
|
$persister = new Persisters\ManyToManyPersister($this->_em);
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
|
|
|
$this->_collectionPersisters[$type] = $persister;
|
|
|
|
}
|
|
|
|
return $this->_collectionPersisters[$type];
|
|
|
|
}
|
2009-02-07 20:02:13 +03:00
|
|
|
}
|