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-07-29 15:57:27 +04:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection,
|
2009-07-29 16:00:08 +04:00
|
|
|
Doctrine\Common\Collections\Collection,
|
2009-07-29 15:57:27 +04:00
|
|
|
Doctrine\Common\DoctrineException,
|
|
|
|
Doctrine\Common\PropertyChangedListener,
|
|
|
|
Doctrine\ORM\Event\LifecycleEventArgs,
|
|
|
|
Doctrine\ORM\Internal\CommitOrderCalculator,
|
|
|
|
Doctrine\ORM\Internal\CommitOrderNode;
|
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>
|
2009-05-17 23:27:12 +04:00
|
|
|
* @internal This class contains performance-critical code. Work with care and
|
|
|
|
* regularly run the ORM performance tests.
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2009-04-09 22:12:48 +04:00
|
|
|
class UnitOfWork implements PropertyChangedListener
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* An entity is in MANAGED state when its persistence is managed by an EntityManager.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
|
|
|
const STATE_MANAGED = 1;
|
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* An entity is new if it has just been instantiated (i.e. using the "new" operator)
|
2008-12-18 17:08:11 +03:00
|
|
|
* and is not (yet) managed by an EntityManager.
|
|
|
|
*/
|
|
|
|
const STATE_NEW = 2;
|
|
|
|
|
|
|
|
/**
|
2009-05-21 23:18:14 +04:00
|
|
|
* A detached entity is an instance with a persistent identity that is not
|
2008-12-18 17:08:11 +03:00
|
|
|
* (or no longer) associated with an EntityManager (and a UnitOfWork).
|
|
|
|
*/
|
|
|
|
const STATE_DETACHED = 3;
|
|
|
|
|
|
|
|
/**
|
2009-05-21 23:18:14 +04:00
|
|
|
* A removed entity instance is an instance with a persistent identity,
|
2008-12-18 17:08:11 +03:00
|
|
|
* associated with an EntityManager, whose persistent state has been
|
|
|
|
* deleted (or is scheduled for deletion).
|
|
|
|
*/
|
2009-07-25 20:33:29 +04:00
|
|
|
const STATE_REMOVED = 4;
|
2008-12-18 17:08:11 +03:00
|
|
|
|
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
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Map of all identifiers of managed entities.
|
|
|
|
* Keys are object ids (spl_object_hash).
|
2009-01-03 22:50:13 +03:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_entityIdentifiers = array();
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Map of the original entity data of managed entities.
|
2009-07-19 20:54:53 +04:00
|
|
|
* Keys are object ids (spl_object_hash). This is used for calculating changesets
|
|
|
|
* at commit time.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @var array
|
2009-07-19 20:54:53 +04:00
|
|
|
* @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage.
|
|
|
|
* A value will only really be copied if the value in the entity is modified
|
|
|
|
* by the user.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_originalEntityData = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Map of entity changes. Keys are object ids (spl_object_hash).
|
2009-05-17 23:27:12 +04:00
|
|
|
* Filled at the beginning of a commit of the UnitOfWork and cleaned at the end.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_entityChangeSets = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* The (cached) states of any known entities.
|
2009-07-19 20:54:53 +04:00
|
|
|
* Keys are object ids (spl_object_hash).
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
|
|
|
* @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.
|
2009-07-25 20:33:29 +04:00
|
|
|
* This is only used for entities with a change tracking policy of DEFERRED_EXPLICIT.
|
2009-07-19 20:54:53 +04:00
|
|
|
* Keys are object ids (spl_object_hash).
|
|
|
|
*
|
|
|
|
* @var array
|
2009-01-29 20:00:44 +03:00
|
|
|
*/
|
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();
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Any pending extra updates that have been scheduled by persisters.
|
2009-07-18 15:41:37 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-07-03 21:36:41 +04:00
|
|
|
private $_extraUpdates = array();
|
|
|
|
|
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();
|
2009-07-03 21:36:41 +04:00
|
|
|
|
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();
|
2009-07-03 21:36:41 +04:00
|
|
|
|
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-07-19 20:54:53 +04:00
|
|
|
//private $_collectionCreations = array();
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* All pending collection updates.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_collectionUpdates = array();
|
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* List of collections visited during changeset calculation on a commit-phase of a UnitOfWork.
|
2009-02-02 14:55:50 +03:00
|
|
|
* 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
|
|
|
|
2009-07-11 01:47:42 +04:00
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Flag for whether or not to make use of the C extension.
|
2009-07-11 01:47:42 +04:00
|
|
|
*
|
2009-07-11 01:49:47 +04:00
|
|
|
* @var boolean
|
2009-07-11 01:47:42 +04:00
|
|
|
*/
|
|
|
|
private $_useCExtension = false;
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* The EventManager used for dispatching events.
|
2009-07-18 15:41:37 +04:00
|
|
|
*
|
|
|
|
* @var EventManager
|
|
|
|
*/
|
|
|
|
private $_evm;
|
2009-07-23 13:52:16 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Orphaned entities that are scheduled for removal.
|
2009-07-23 13:52:16 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_orphanRemovals = array();
|
2009-07-11 01:47:42 +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;
|
2009-07-18 15:41:37 +04:00
|
|
|
$this->_evm = $em->getEventManager();
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_commitOrderCalculator = new CommitOrderCalculator();
|
2009-07-11 01:47:42 +04:00
|
|
|
$this->_useCExtension = $this->_em->getConfiguration()->getUseCExtension();
|
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
|
2009-07-25 20:33:29 +04:00
|
|
|
* up to this point. The state of all managed entities will be synchronized with
|
|
|
|
* the database.
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2008-04-13 00:11:11 +04:00
|
|
|
public function commit()
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2009-04-09 22:12:48 +04:00
|
|
|
// Compute changes done since last commit.
|
|
|
|
// This populates _entityUpdates and _collectionUpdates.
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->computeChangeSets();
|
|
|
|
|
2009-07-23 13:52:16 +04:00
|
|
|
if ( ! ($this->_entityInsertions ||
|
|
|
|
$this->_entityDeletions ||
|
|
|
|
$this->_entityUpdates ||
|
|
|
|
$this->_collectionUpdates ||
|
|
|
|
$this->_collectionDeletions ||
|
|
|
|
$this->_orphanRemovals)) {
|
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();
|
2009-07-23 13:52:16 +04:00
|
|
|
|
|
|
|
if ($this->_orphanRemovals) {
|
|
|
|
foreach ($this->_orphanRemovals as $orphan) {
|
|
|
|
$this->remove($orphan);
|
|
|
|
}
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-05-19 20:11:08 +04:00
|
|
|
$conn = $this->_em->getConnection();
|
|
|
|
try {
|
|
|
|
$conn->beginTransaction();
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
if ($this->_entityInsertions) {
|
|
|
|
foreach ($commitOrder as $class) {
|
|
|
|
$this->_executeInserts($class);
|
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
if ($this->_entityUpdates) {
|
|
|
|
foreach ($commitOrder as $class) {
|
|
|
|
$this->_executeUpdates($class);
|
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-07-03 21:36:41 +04:00
|
|
|
// Extra updates that were requested by persisters.
|
|
|
|
if ($this->_extraUpdates) {
|
|
|
|
$this->_executeExtraUpdates();
|
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
|
|
|
|
// Collection deletions (deletions of complete collections)
|
|
|
|
foreach ($this->_collectionDeletions as $collectionToDelete) {
|
|
|
|
$this->getCollectionPersister($collectionToDelete->getMapping())
|
|
|
|
->delete($collectionToDelete);
|
|
|
|
}
|
|
|
|
// Collection updates (deleteRows, updateRows, insertRows)
|
|
|
|
foreach ($this->_collectionUpdates as $collectionToUpdate) {
|
|
|
|
$this->getCollectionPersister($collectionToUpdate->getMapping())
|
|
|
|
->update($collectionToUpdate);
|
|
|
|
}
|
|
|
|
//TODO: collection recreations (insertions of complete collections)
|
|
|
|
|
|
|
|
// Entity deletions come last and need to be in reverse commit order
|
2009-07-18 15:41:37 +04:00
|
|
|
if ($this->_entityDeletions) {
|
|
|
|
for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) {
|
|
|
|
$this->_executeDeletions($commitOrder[$i]);
|
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-05-19 20:11:08 +04:00
|
|
|
$conn->commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$conn->rollback();
|
2009-07-24 15:33:38 +04:00
|
|
|
$this->clear();
|
2009-05-19 20:11:08 +04:00
|
|
|
throw $e;
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
|
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-07-23 13:52:16 +04:00
|
|
|
$this->_entityInsertions =
|
|
|
|
$this->_entityUpdates =
|
|
|
|
$this->_entityDeletions =
|
|
|
|
$this->_extraUpdates =
|
|
|
|
$this->_entityChangeSets =
|
|
|
|
$this->_collectionUpdates =
|
|
|
|
$this->_collectionDeletions =
|
|
|
|
$this->_visitedCollections =
|
|
|
|
$this->_orphanRemovals = array();
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-07-24 15:33:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes any extra updates that have been scheduled.
|
|
|
|
*/
|
2009-07-23 13:52:16 +04:00
|
|
|
private function _executeExtraUpdates()
|
2009-07-03 21:36:41 +04:00
|
|
|
{
|
|
|
|
foreach ($this->_extraUpdates as $oid => $update) {
|
|
|
|
list ($entity, $changeset) = $update;
|
|
|
|
$this->_entityChangeSets[$oid] = $changeset;
|
|
|
|
$this->getEntityPersister(get_class($entity))->update($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-19 20:54:53 +04:00
|
|
|
public function computeChangeSets()
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-07-19 20:54:53 +04:00
|
|
|
// Compute changes for INSERTed entities first. This must always happen.
|
2009-07-23 13:52:16 +04:00
|
|
|
foreach ($this->_entityInsertions as $entity) {
|
2009-07-19 20:54:53 +04:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
$this->_computeEntityChanges($class, $entity);
|
|
|
|
// Look for changes in associations of the entity
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
$val = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
|
|
|
|
if ($val !== null) {
|
|
|
|
$this->_computeAssociationChanges($assoc, $val);
|
|
|
|
}
|
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
}
|
2009-05-05 21:20:55 +04:00
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
// Compute changes for other MANAGED entities. Change tracking policies take effect here.
|
2009-07-23 13:52:16 +04:00
|
|
|
foreach ($this->_identityMap as $className => $entities) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$class = $this->_em->getClassMetadata($className);
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-04-25 01:08:59 +04:00
|
|
|
// Skip class if change tracking happens through notification
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($class->isChangeTrackingNotify()) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-25 01:08:59 +04:00
|
|
|
|
|
|
|
// If change tracking is explicit, then only compute changes on explicitly saved entities
|
2009-04-09 22:12:48 +04:00
|
|
|
$entitiesToProcess = $class->isChangeTrackingDeferredExplicit() ?
|
|
|
|
$this->_scheduledForDirtyCheck[$className] : $entities;
|
|
|
|
|
2009-04-25 01:08:59 +04:00
|
|
|
foreach ($entitiesToProcess as $entity) {
|
2009-07-19 20:54:53 +04:00
|
|
|
// Only MANAGED entities that are NOT INSERTED are processed here.
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($this->_entityStates[$oid]) && ! isset($entityInsertions[$oid])) {
|
2009-05-05 22:39:25 +04:00
|
|
|
$this->_computeEntityChanges($class, $entity);
|
|
|
|
// Look for changes in associations of the entity
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2009-05-19 20:24:17 +04:00
|
|
|
$val = $class->reflFields[$assoc->sourceFieldName]->getValue($entity);
|
2009-05-05 22:39:25 +04:00
|
|
|
if ($val !== null) {
|
|
|
|
$this->_computeAssociationChanges($assoc, $val);
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
|
2009-05-05 22:39:25 +04:00
|
|
|
/**
|
|
|
|
* Computes the changes done to a single entity.
|
|
|
|
*
|
|
|
|
* Modifies/populates the following properties:
|
|
|
|
*
|
|
|
|
* {@link _originalEntityData}
|
|
|
|
* If the entity is NEW or MANAGED but not yet fully persisted (only has an id)
|
|
|
|
* then it was not fetched from the database and therefore we have no original
|
|
|
|
* entity data yet. All of the current entity data is stored as the original entity data.
|
|
|
|
*
|
|
|
|
* {@link _entityChangeSets}
|
|
|
|
* The changes detected on all properties of the entity are stored there.
|
|
|
|
* A change is a tuple array where the first entry is the old value and the second
|
|
|
|
* entry is the new value of the property. Changesets are used by persisters
|
|
|
|
* to INSERT/UPDATE the persistent entity state.
|
|
|
|
*
|
|
|
|
* {@link _entityUpdates}
|
|
|
|
* If the entity is already fully MANAGED (has been fetched from the database before)
|
|
|
|
* and any changes to its properties are detected, then a reference to the entity is stored
|
|
|
|
* there to mark it for an update.
|
|
|
|
*
|
|
|
|
* {@link _collectionDeletions}
|
|
|
|
* If a PersistentCollection has been de-referenced in a fully MANAGED entity,
|
|
|
|
* then this collection is marked for deletion.
|
|
|
|
*
|
|
|
|
* @param ClassMetadata $class The class descriptor of the entity.
|
|
|
|
* @param object $entity The entity for which to compute the changes.
|
|
|
|
*/
|
|
|
|
private function _computeEntityChanges($class, $entity)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2009-05-05 22:39:25 +04:00
|
|
|
if ( ! $class->isInheritanceTypeNone()) {
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
}
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2009-05-05 22:39:25 +04:00
|
|
|
$actualData = array();
|
2009-05-19 20:24:17 +04:00
|
|
|
foreach ($class->reflFields as $name => $refProp) {
|
2009-05-05 22:39:25 +04:00
|
|
|
if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) {
|
|
|
|
$actualData[$name] = $refProp->getValue($entity);
|
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
|
2009-07-03 21:36:41 +04:00
|
|
|
if ($class->isCollectionValuedAssociation($name) && $actualData[$name] !== null
|
|
|
|
&& ! ($actualData[$name] instanceof PersistentCollection)) {
|
2009-06-14 21:34:28 +04:00
|
|
|
// If $actualData[$name] is Collection then unwrap the array
|
2009-07-29 15:57:27 +04:00
|
|
|
if ( ! $actualData[$name] instanceof ArrayCollection) {
|
|
|
|
$actualData[$name] = new ArrayCollection($actualData[$name]);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2009-05-19 20:24:17 +04:00
|
|
|
$assoc = $class->associationMappings[$name];
|
2009-05-05 22:39:25 +04:00
|
|
|
// Inject PersistentCollection
|
2009-07-29 15:57:27 +04:00
|
|
|
$coll = new PersistentCollection($this->_em, $this->_em->getClassMetadata(
|
|
|
|
$assoc->targetEntityName), $actualData[$name]);
|
2009-05-05 22:39:25 +04:00
|
|
|
$coll->setOwner($entity, $assoc);
|
2009-06-22 22:48:42 +04:00
|
|
|
$coll->setDirty( ! $coll->isEmpty());
|
2009-05-14 14:03:09 +04:00
|
|
|
$class->reflFields[$name]->setValue($entity, $coll);
|
2009-05-05 22:39:25 +04:00
|
|
|
$actualData[$name] = $coll;
|
|
|
|
}
|
|
|
|
}
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2009-05-05 22:39:25 +04:00
|
|
|
if ( ! isset($this->_originalEntityData[$oid])) {
|
2009-07-18 15:41:37 +04:00
|
|
|
// Entity is either NEW or MANAGED but not yet fully persisted (only has an id).
|
|
|
|
// These result in an INSERT.
|
2009-05-05 22:39:25 +04:00
|
|
|
$this->_originalEntityData[$oid] = $actualData;
|
|
|
|
$this->_entityChangeSets[$oid] = array_map(
|
2009-06-22 22:48:42 +04:00
|
|
|
function($e) { return array(null, $e); }, $actualData
|
2009-05-05 22:39:25 +04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Entity is "fully" MANAGED: it was already fully persisted before
|
|
|
|
// and we have a copy of the original data
|
|
|
|
$originalData = $this->_originalEntityData[$oid];
|
|
|
|
$changeSet = array();
|
|
|
|
$entityIsDirty = false;
|
|
|
|
|
|
|
|
foreach ($actualData as $propName => $actualValue) {
|
|
|
|
$orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
|
|
|
|
if (is_object($orgValue) && $orgValue !== $actualValue) {
|
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
2009-05-11 14:43:27 +04:00
|
|
|
} else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
|
2009-05-05 22:39:25 +04:00
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($changeSet[$propName])) {
|
2009-05-19 20:24:17 +04:00
|
|
|
if (isset($class->associationMappings[$propName])) {
|
|
|
|
$assoc = $class->associationMappings[$propName];
|
2009-07-23 13:52:16 +04:00
|
|
|
if ($assoc->isOneToOne()) {
|
|
|
|
if ($assoc->isOwningSide) {
|
|
|
|
$entityIsDirty = true;
|
|
|
|
}
|
|
|
|
if ($actualValue === null && $assoc->orphanRemoval) {
|
|
|
|
$this->scheduleOrphanRemoval($orgValue);
|
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
} else if ($orgValue instanceof PersistentCollection) {
|
|
|
|
// A PersistentCollection was de-referenced, so delete it.
|
|
|
|
if ( ! in_array($orgValue, $this->_collectionDeletions, true)) {
|
|
|
|
$this->_collectionDeletions[] = $orgValue;
|
2009-01-29 20:00:44 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
} else {
|
|
|
|
$entityIsDirty = true;
|
2009-01-29 20:00:44 +03:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
if ($changeSet) {
|
|
|
|
if ($entityIsDirty) {
|
|
|
|
$this->_entityUpdates[$oid] = $entity;
|
|
|
|
}
|
|
|
|
$this->_entityChangeSets[$oid] = $changeSet;
|
|
|
|
$this->_originalEntityData[$oid] = $actualData;
|
|
|
|
}
|
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()) {
|
2009-05-19 20:24:17 +04:00
|
|
|
if ($assoc->isOwningSide) {
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_collectionUpdates[] = $value;
|
|
|
|
}
|
|
|
|
$this->_visitedCollections[] = $value;
|
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
if ( ! $assoc->isCascadePersist) {
|
|
|
|
return; // "Persistence by reachability" only if persist cascade specified
|
2009-02-07 20:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2009-05-19 20:24:17 +04:00
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
2009-01-09 19:25:06 +03:00
|
|
|
foreach ($value as $entry) {
|
2009-07-25 20:33:29 +04:00
|
|
|
$state = $this->getEntityState($entry, self::STATE_NEW);
|
2009-01-09 19:25:06 +03:00
|
|
|
$oid = spl_object_hash($entry);
|
|
|
|
if ($state == self::STATE_NEW) {
|
|
|
|
// Get identifier, if possible (not post-insert)
|
2009-05-28 15:13:12 +04:00
|
|
|
$idGen = $targetClass->idGenerator;
|
2009-01-09 19:25:06 +03:00
|
|
|
if ( ! $idGen->isPostInsertGenerator()) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$idValue = $idGen->generate($this->_em, $entry);
|
2009-01-09 19:25:06 +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);
|
|
|
|
$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-05-19 20:24:17 +04:00
|
|
|
foreach ($targetClass->reflFields as $name => $refProp) {
|
2009-01-09 19:25:06 +03:00
|
|
|
$data[$name] = $refProp->getValue($entry);
|
2009-02-06 20:16:39 +03:00
|
|
|
$changeSet[$name] = array(null, $data[$name]);
|
2009-05-19 20:24:17 +04:00
|
|
|
if (isset($targetClass->associationMappings[$name])) {
|
2009-02-07 20:02:13 +03:00
|
|
|
//TODO: Prevent infinite recursion
|
2009-05-19 20:24:17 +04:00
|
|
|
$this->_computeAssociationChanges($targetClass->associationMappings[$name], $data[$name]);
|
2009-02-07 20:02:13 +03:00
|
|
|
}
|
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;
|
2009-07-25 20:33:29 +04:00
|
|
|
} else if ($state == self::STATE_REMOVED) {
|
|
|
|
throw DoctrineException::updateMe("Removed entity in collection detected during flush."
|
2009-07-18 15:41:37 +04:00
|
|
|
. " Make sure you properly remove deleted entities from collections.");
|
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.
|
|
|
|
}
|
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* INTERNAL, EXPERIMENTAL:
|
2009-07-18 15:41:37 +04:00
|
|
|
* Computes the changeset of an individual entity, independently of the
|
|
|
|
* computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit().
|
|
|
|
*
|
|
|
|
* @param $class
|
|
|
|
* @param $entity
|
|
|
|
*/
|
|
|
|
public function computeSingleEntityChangeSet($class, $entity)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
|
|
|
|
if ( ! $class->isInheritanceTypeNone()) {
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
}
|
|
|
|
|
|
|
|
$actualData = array();
|
|
|
|
foreach ($class->reflFields as $name => $refProp) {
|
|
|
|
if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) {
|
|
|
|
$actualData[$name] = $refProp->getValue($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($this->_originalEntityData[$oid])) {
|
|
|
|
$this->_originalEntityData[$oid] = $actualData;
|
|
|
|
$this->_entityChangeSets[$oid] = array_map(
|
|
|
|
function($e) { return array(null, $e); }, $actualData
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$originalData = $this->_originalEntityData[$oid];
|
|
|
|
$changeSet = array();
|
|
|
|
|
|
|
|
foreach ($actualData as $propName => $actualValue) {
|
|
|
|
$orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
|
|
|
|
if (is_object($orgValue) && $orgValue !== $actualValue) {
|
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
|
|
|
} else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
|
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($changeSet) {
|
|
|
|
$this->_entityChangeSets[$oid] = $changeSet;
|
|
|
|
$this->_originalEntityData[$oid] = $actualData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
|
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
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $class->name;
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postPersist]);
|
|
|
|
$hasListeners = $this->_evm->hasListeners(Events::postPersist);
|
2009-07-18 22:06:30 +04:00
|
|
|
if ($hasLifecycleCallbacks || $hasListeners) {
|
|
|
|
$entities = array();
|
|
|
|
}
|
|
|
|
|
2009-05-26 19:42:54 +04:00
|
|
|
foreach ($this->_entityInsertions as $oid => $entity) {
|
2008-12-18 17:08:11 +03:00
|
|
|
if (get_class($entity) == $className) {
|
2009-05-19 20:11:08 +04:00
|
|
|
$persister->addInsert($entity);
|
2009-05-26 19:42:54 +04:00
|
|
|
unset($this->_entityInsertions[$oid]);
|
2009-07-18 22:06:30 +04:00
|
|
|
if ($hasLifecycleCallbacks || $hasListeners) {
|
|
|
|
$entities[] = $entity;
|
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
}
|
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-05-19 20:11:08 +04:00
|
|
|
$postInsertIds = $persister->executeInserts();
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-05-19 20:11:08 +04:00
|
|
|
if ($postInsertIds) {
|
2009-07-18 22:06:30 +04:00
|
|
|
// Persister returned a post-insert IDs
|
2009-05-19 20:11:08 +04:00
|
|
|
foreach ($postInsertIds as $id => $entity) {
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
$idField = $class->identifier[0];
|
|
|
|
$class->reflFields[$idField]->setValue($entity, $id);
|
|
|
|
$this->_entityIdentifiers[$oid] = array($id);
|
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
|
|
|
$this->_originalEntityData[$oid][$idField] = $id;
|
|
|
|
$this->addToIdentityMap($entity);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
|
|
|
|
if ($hasLifecycleCallbacks || $hasListeners) {
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
if ($hasLifecycleCallbacks) {
|
2009-07-24 15:33:38 +04:00
|
|
|
$class->invokeLifecycleCallbacks(Events::postPersist, $entity);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
|
|
|
if ($hasListeners) {
|
2009-07-24 15:33:38 +04:00
|
|
|
$this->_evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($entity));
|
2009-07-18 22:06:30 +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)
|
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $class->name;
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
|
|
|
$hasPreUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preUpdate]);
|
|
|
|
$hasPreUpdateListeners = $this->_evm->hasListeners(Events::preUpdate);
|
|
|
|
$hasPostUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postUpdate]);
|
|
|
|
$hasPostUpdateListeners = $this->_evm->hasListeners(Events::postUpdate);
|
|
|
|
|
2009-05-26 19:42:54 +04:00
|
|
|
foreach ($this->_entityUpdates as $oid => $entity) {
|
2009-01-09 19:25:06 +03:00
|
|
|
if (get_class($entity) == $className) {
|
2009-07-18 22:06:30 +04:00
|
|
|
if ($hasPreUpdateLifecycleCallbacks) {
|
|
|
|
$class->invokeLifecycleCallbacks(Events::preUpdate, $entity);
|
|
|
|
if ( ! $hasPreUpdateListeners) {
|
|
|
|
// Need to recompute entity changeset to detect changes made in the callback.
|
|
|
|
$this->computeSingleEntityChangeSet($class, $entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($hasPreUpdateListeners) {
|
|
|
|
$this->_evm->dispatchEvent(Events::preUpdate, new LifecycleEventArgs($entity));
|
|
|
|
// Need to recompute entity changeset to detect changes made in the listener.
|
|
|
|
$this->computeSingleEntityChangeSet($class, $entity);
|
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
$persister->update($entity);
|
2009-05-26 19:42:54 +04:00
|
|
|
unset($this->_entityUpdates[$oid]);
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
if ($hasPostUpdateLifecycleCallbacks) {
|
|
|
|
$class->invokeLifecycleCallbacks(Events::postUpdate, $entity);
|
|
|
|
}
|
|
|
|
if ($hasPostUpdateListeners) {
|
|
|
|
$this->_evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($entity));
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $class->name;
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2009-07-24 15:33:38 +04:00
|
|
|
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postRemove]);
|
|
|
|
$hasListeners = $this->_evm->hasListeners(Events::postRemove);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2009-05-26 19:42:54 +04:00
|
|
|
foreach ($this->_entityDeletions as $oid => $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);
|
2009-05-26 19:42:54 +04:00
|
|
|
unset($this->_entityDeletions[$oid]);
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
if ($hasLifecycleCallbacks) {
|
2009-07-24 15:33:38 +04:00
|
|
|
$class->invokeLifecycleCallbacks(Events::postRemove, $entity);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
|
|
|
if ($hasListeners) {
|
2009-07-24 15:33:38 +04:00
|
|
|
$this->_evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($entity));
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2009-03-14 12:05:52 +03:00
|
|
|
if ($entityChangeSet === null) {
|
2009-01-12 16:34:41 +03:00
|
|
|
$entityChangeSet = array_merge(
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_entityInsertions,
|
|
|
|
$this->_entityUpdates,
|
2009-07-03 21:36:41 +04:00
|
|
|
$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!
|
2009-07-03 21:36:41 +04:00
|
|
|
|
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);
|
2009-07-27 13:50:22 +04:00
|
|
|
if ( ! $this->_commitOrderCalculator->hasClass($className)) {
|
|
|
|
$class = $this->_em->getClassMetadata($className);
|
|
|
|
$this->_commitOrderCalculator->addClass($class);
|
|
|
|
$newNodes[] = $class;
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate dependencies for new nodes
|
2009-07-27 13:50:22 +04:00
|
|
|
foreach ($newNodes as $class) {
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->associationMappings as $assocMapping) {
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: should skip target classes that are not in the changeset.
|
2009-05-19 20:24:17 +04:00
|
|
|
if ($assocMapping->isOwningSide) {
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assocMapping->targetEntityName);
|
2009-07-27 13:50:22 +04:00
|
|
|
if ( ! $this->_commitOrderCalculator->hasClass($targetClass->name)) {
|
|
|
|
$this->_commitOrderCalculator->addClass($targetClass);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
// add dependency
|
2009-07-27 13:50:22 +04:00
|
|
|
$this->_commitOrderCalculator->addDependency($targetClass, $class);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_commitOrderCalculator->getCommitOrder();
|
|
|
|
}
|
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
2009-07-19 20:54:53 +04:00
|
|
|
* Schedules an entity for insertion into the database.
|
2009-05-21 23:18:14 +04:00
|
|
|
* If the entity already has an identifier, it will be added to the identity map.
|
2009-07-03 21:36:41 +04:00
|
|
|
*
|
2009-05-21 23:18:14 +04:00
|
|
|
* @param object $entity
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function scheduleForInsert($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-03-28 23:59:07 +03:00
|
|
|
throw DoctrineException::updateMe("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-03-28 23:59:07 +03:00
|
|
|
throw DoctrineException::updateMe("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-03-28 23:59:07 +03:00
|
|
|
throw DoctrineException::updateMe("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
|
|
|
|
|
|
|
/**
|
2009-05-21 23:18:14 +04:00
|
|
|
* Checks whether an entity is registered as new on this unit of work.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2009-05-21 23:18:14 +04:00
|
|
|
* @param object $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function isScheduledForInsert($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-05-21 23:18:14 +04:00
|
|
|
* @param object $entity
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function scheduleForUpdate($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-03-28 23:59:07 +03:00
|
|
|
throw DoctrineException::updateMe("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-03-28 23:59:07 +03:00
|
|
|
throw DoctrineException::updateMe("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
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* INTERNAL:
|
2009-07-18 15:41:37 +04:00
|
|
|
* Schedules an extra update that will be executed immediately after the
|
|
|
|
* regular entity updates.
|
|
|
|
*
|
|
|
|
* @param $entity
|
|
|
|
* @param $changeset
|
|
|
|
*/
|
2009-07-03 21:36:41 +04:00
|
|
|
public function scheduleExtraUpdate($entity, array $changeset)
|
|
|
|
{
|
|
|
|
$this->_extraUpdates[spl_object_hash($entity)] = array($entity, $changeset);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2009-07-18 15:41:37 +04:00
|
|
|
* @param object $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function isScheduledForUpdate($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.
|
2009-07-19 20:54:53 +04:00
|
|
|
*
|
|
|
|
* @param object $entity
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function scheduleForDelete($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-07-18 15:41:37 +04:00
|
|
|
* @param object $entity
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return boolean
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function isScheduledForDelete($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
|
|
|
|
2009-07-18 15:41:37 +04:00
|
|
|
/**
|
2009-07-19 20:54:53 +04:00
|
|
|
* Checks whether an entity is scheduled for insertion, update or deletion.
|
2009-07-18 15:41:37 +04:00
|
|
|
*
|
|
|
|
* @param $entity
|
2009-07-19 20:54:53 +04:00
|
|
|
* @return boolean
|
2009-07-18 15:41:37 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function isEntityScheduled($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
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* INTERNAL:
|
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.
|
|
|
|
*
|
2009-05-21 23:18:14 +04:00
|
|
|
* @param object $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-04-09 22:12:48 +04:00
|
|
|
$idHash = implode(' ', $this->_entityIdentifiers[spl_object_hash($entity)]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2009-03-28 23:59:07 +03:00
|
|
|
throw DoctrineException::updateMe("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
|
|
|
}
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $classMetadata->rootEntityName;
|
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;
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($entity instanceof \Doctrine\Common\NotifyPropertyChanged) {
|
|
|
|
$entity->addPropertyChangedListener($this);
|
|
|
|
}
|
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.
|
2009-07-25 20:33:29 +04:00
|
|
|
*
|
|
|
|
* NOTE: This method sees entities that are not MANAGED or REMOVED and have a
|
|
|
|
* populated identifier, whether it is generated or manually assigned, as
|
|
|
|
* DETACHED. This can be incorrect for manually assigned identifiers.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-05-11 14:43:27 +04:00
|
|
|
* @param object $entity
|
2009-07-25 20:33:29 +04:00
|
|
|
* @param integer $assume The state to assume if the state is not yet known. This is usually
|
|
|
|
* used to avoid costly state lookups, in the worst case with a database
|
|
|
|
* lookup.
|
2009-05-21 23:18:14 +04:00
|
|
|
* @return int The entity state.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
2009-07-25 20:33:29 +04:00
|
|
|
public function getEntityState($entity, $assume = null)
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
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-07-25 20:33:29 +04:00
|
|
|
// State can only be NEW or DETACHED, because MANAGED/REMOVED states are immediately
|
|
|
|
// set by the UnitOfWork directly. We treat all entities that have a populated
|
|
|
|
// identifier as DETACHED and all others as NEW. This is not really correct for
|
|
|
|
// manually assigned identifiers but in that case we would need to hit the database
|
|
|
|
// and we would like to avoid that.
|
|
|
|
if ($assume === null) {
|
|
|
|
if ($this->_em->getClassMetadata(get_class($entity))->getIdentifierValues($entity)) {
|
|
|
|
$this->_entityStates[$oid] = self::STATE_DETACHED;
|
|
|
|
} else {
|
|
|
|
$this->_entityStates[$oid] = self::STATE_NEW;
|
|
|
|
}
|
2009-01-04 19:15:32 +03:00
|
|
|
} else {
|
2009-07-25 20:33:29 +04:00
|
|
|
$this->_entityStates[$oid] = $assume;
|
2009-01-04 19:15:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->_entityStates[$oid];
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* INTERNAL:
|
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-05-11 14:43:27 +04:00
|
|
|
* @param object $entity
|
2009-01-03 22:50:13 +03:00
|
|
|
* @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-04-09 22:12:48 +04:00
|
|
|
$idHash = implode(' ', $this->_entityIdentifiers[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2009-03-30 23:43:05 +04:00
|
|
|
throw DoctrineException::updateMe("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
|
|
|
}
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $classMetadata->rootEntityName;
|
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-07-25 20:33:29 +04:00
|
|
|
* INTERNAL:
|
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
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* INTERNAL:
|
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-01 13:41:47 +04:00
|
|
|
/**
|
2009-05-21 23:18:14 +04:00
|
|
|
* Checks whether an entity is registered in the identity map of this 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-04-09 22:12:48 +04:00
|
|
|
$idHash = implode(' ', $this->_entityIdentifiers[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2008-05-01 13:41:47 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-03 21:36:41 +04:00
|
|
|
|
|
|
|
return isset($this->_identityMap[$classMetadata->rootEntityName][$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
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* INTERNAL:
|
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
|
|
|
|
|
|
|
/**
|
2009-07-25 20:33:29 +04:00
|
|
|
* Persists an entity as part of the current unit of work.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2009-07-25 20:33:29 +04:00
|
|
|
* @param object $entity The entity to persist.
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function persist($entity)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
$visited = array();
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_doPersist($entity, $visited);
|
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-07-25 20:33:29 +04:00
|
|
|
*
|
|
|
|
* NOTE: This method always considers entities with a manually assigned identifier as NEW.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2009-07-25 20:33:29 +04:00
|
|
|
* @param object $entity The entity to persist.
|
2009-01-04 19:15:32 +03:00
|
|
|
* @param array $visited The already visited entities.
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
private function _doPersist($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
|
|
|
|
}
|
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
$visited[$oid] = $entity; // Mark visited
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
switch ($this->getEntityState($entity, self::STATE_NEW)) {
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_MANAGED:
|
2009-05-11 14:43:27 +04:00
|
|
|
// Nothing to do, except if policy is "deferred explicit"
|
2009-04-25 01:08:59 +04:00
|
|
|
if ($class->isChangeTrackingDeferredExplicit()) {
|
2009-01-29 20:00:44 +03:00
|
|
|
$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-07-24 15:33:38 +04:00
|
|
|
if (isset($class->lifecycleCallbacks[Events::prePersist])) {
|
|
|
|
$class->invokeLifecycleCallbacks(Events::prePersist, $entity);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-07-24 15:33:38 +04:00
|
|
|
if ($this->_evm->hasListeners(Events::prePersist)) {
|
|
|
|
$this->_evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($entity));
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-05-19 20:24:17 +04:00
|
|
|
$idGen = $class->idGenerator;
|
2009-07-19 20:54:53 +04:00
|
|
|
if ( ! $idGen->isPostInsertGenerator()) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$idValue = $idGen->generate($this->_em, $entity);
|
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-05-11 14:43:27 +04:00
|
|
|
$class->setIdentifierValues($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
|
|
|
}
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
|
|
|
|
|
|
|
$this->scheduleForInsert($entity);
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_DETACHED:
|
2009-03-30 23:43:05 +04:00
|
|
|
throw DoctrineException::updateMe("Behavior of save() for a detached entity "
|
2008-07-21 00:13:24 +04:00
|
|
|
. "is not yet defined.");
|
2009-07-25 20:33:29 +04:00
|
|
|
case self::STATE_REMOVED:
|
2009-05-11 14:43:27 +04:00
|
|
|
// Entity becomes managed again
|
2009-07-19 20:54:53 +04:00
|
|
|
if ($this->isScheduledForDelete($entity)) {
|
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...
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->scheduleForInsert($entity);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
2009-03-30 23:43:05 +04:00
|
|
|
throw DoctrineException::updateMe("Encountered invalid entity state.");
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_cascadePersist($entity, $visited);
|
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-07-25 20:33:29 +04:00
|
|
|
* @param object $entity The entity to remove.
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
public function remove($entity)
|
2008-07-10 21:17:58 +04:00
|
|
|
{
|
2009-01-09 19:25:06 +03:00
|
|
|
$visited = array();
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_doRemove($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.
|
2009-07-03 21:36:41 +04:00
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* 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-04-09 22:12:48 +04:00
|
|
|
* @param object $entity The entity to delete.
|
|
|
|
* @param array $visited The map of the already visited entities.
|
2009-07-25 20:33:29 +04:00
|
|
|
* @throws InvalidArgumentException If the instance is a detached entity.
|
2008-09-07 17:48:40 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
private function _doRemove($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
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
2008-12-18 17:08:11 +03:00
|
|
|
switch ($this->getEntityState($entity)) {
|
|
|
|
case self::STATE_NEW:
|
2009-07-25 20:33:29 +04:00
|
|
|
case self::STATE_REMOVED:
|
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:
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($class->lifecycleCallbacks[Events::preRemove])) {
|
|
|
|
$class->invokeLifecycleCallbacks(Events::preRemove, $entity);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-07-24 15:33:38 +04:00
|
|
|
if ($this->_evm->hasListeners(Events::preRemove)) {
|
|
|
|
$this->_evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($entity));
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->scheduleForDelete($entity);
|
2008-07-21 00:13:24 +04:00
|
|
|
break;
|
2008-12-18 17:08:11 +03:00
|
|
|
case self::STATE_DETACHED:
|
2009-07-25 20:33:29 +04:00
|
|
|
throw new \InvalidArgumentException("A detached entity can not be removed.");
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
2009-03-30 23:43:05 +04:00
|
|
|
throw DoctrineException::updateMe("Encountered invalid entity state.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_cascadeRemove($entity, $visited);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
/**
|
|
|
|
* Merges the state of the given detached entity into this UnitOfWork.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @return object The managed copy of the entity.
|
|
|
|
*/
|
|
|
|
public function merge($entity)
|
|
|
|
{
|
|
|
|
$visited = array();
|
|
|
|
return $this->_doMerge($entity, $visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a merge operation on an entity.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
* @return object The managed copy of the entity.
|
2009-07-25 20:33:29 +04:00
|
|
|
* @throws OptimisticLockException If the entity uses optimistic locking through a version
|
|
|
|
* attribute and the version check against the managed copy fails.
|
|
|
|
* @throws InvalidArgumentException If the entity instance is NEW.
|
2009-05-11 14:43:27 +04:00
|
|
|
*/
|
|
|
|
private function _doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null)
|
|
|
|
{
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
$id = $class->getIdentifierValues($entity);
|
|
|
|
|
|
|
|
if ( ! $id) {
|
2009-07-25 20:33:29 +04:00
|
|
|
throw new \InvalidArgumentException('New entity detected during merge.'
|
|
|
|
. ' Persist the new entity before merging.');
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
// MANAGED entities are ignored by the merge operation
|
|
|
|
if ($this->getEntityState($entity, self::STATE_DETACHED) == self::STATE_MANAGED) {
|
|
|
|
$managedCopy = $entity;
|
|
|
|
} else {
|
|
|
|
// Try to look the entity up in the identity map.
|
|
|
|
$managedCopy = $this->tryGetById($id, $class->rootEntityName);
|
|
|
|
if ($managedCopy) {
|
|
|
|
// We have the entity in-memory already, just make sure its not removed.
|
|
|
|
if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
|
|
|
|
throw new \InvalidArgumentException('Removed entity detected during merge.'
|
|
|
|
. ' Can not merge with a removed entity.');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We need to fetch the managed copy in order to merge.
|
|
|
|
$managedCopy = $this->_em->find($class->name, $id);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
|
|
|
if ($managedCopy === null) {
|
|
|
|
throw new \InvalidArgumentException('New entity detected during merge.'
|
|
|
|
. ' Persist the new entity before merging.');
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
|
|
|
if ($class->isVersioned) {
|
|
|
|
$managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy);
|
|
|
|
$entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
|
|
|
|
// Throw exception if versions dont match.
|
|
|
|
if ($managedCopyVersion != $entity) {
|
|
|
|
throw OptimisticLockException::versionMismatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge state of $entity into existing (managed) entity
|
|
|
|
foreach ($class->reflFields as $name => $prop) {
|
|
|
|
if ( ! isset($class->associationMappings[$name])) {
|
|
|
|
$prop->setValue($managedCopy, $prop->getValue($entity));
|
|
|
|
} else {
|
|
|
|
$assoc2 = $class->associationMappings[$name];
|
|
|
|
if ($assoc2->isOneToOne() && ! $assoc2->isCascadeMerge) {
|
|
|
|
//TODO: Only do this when allowPartialObjects == false?
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc2->targetEntityName);
|
|
|
|
$prop->setValue($managedCopy, $this->_em->getProxyFactory()
|
|
|
|
->getReferenceProxy($assoc2->targetEntityName, $targetClass->getIdentifierValues($entity)));
|
|
|
|
} else {
|
|
|
|
//TODO: Only do this when allowPartialObjects == false?
|
|
|
|
$coll = new PersistentCollection($this->_em,
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_em->getClassMetadata($assoc2->targetEntityName),
|
|
|
|
new ArrayCollection
|
2009-07-25 20:33:29 +04:00
|
|
|
);
|
|
|
|
$coll->setOwner($managedCopy, $assoc2);
|
|
|
|
$coll->setInitialized($assoc2->isCascadeMerge);
|
|
|
|
$prop->setValue($managedCopy, $coll);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($class->isChangeTrackingNotify()) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($class->isChangeTrackingDeferredExplicit()) {
|
2009-05-11 14:43:27 +04:00
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prevManagedCopy !== null) {
|
2009-05-19 20:24:17 +04:00
|
|
|
$assocField = $assoc->sourceFieldName;
|
2009-05-11 14:43:27 +04:00
|
|
|
$prevClass = $this->_em->getClassMetadata(get_class($prevManagedCopy));
|
|
|
|
if ($assoc->isOneToOne()) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
|
2009-05-11 14:43:27 +04:00
|
|
|
} else {
|
2009-07-25 20:33:29 +04:00
|
|
|
$prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->hydrateAdd($managedCopy);
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_cascadeMerge($entity, $managedCopy, $visited);
|
|
|
|
|
|
|
|
return $managedCopy;
|
|
|
|
}
|
2009-07-21 19:53:58 +04:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
/**
|
|
|
|
* Detaches an entity from the persistence management. It's persistence will
|
|
|
|
* no longer be managed by Doctrine.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity to detach.
|
|
|
|
*/
|
|
|
|
public function detach($entity)
|
|
|
|
{
|
|
|
|
$visited = array();
|
|
|
|
$this->_doDetach($entity, $visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a detach operation on the given entity.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
* @internal This method always considers entities with an assigned identifier as DETACHED.
|
|
|
|
*/
|
|
|
|
private function _doDetach($entity, array &$visited)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($visited[$oid])) {
|
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
|
|
|
$visited[$oid] = $entity; // mark visited
|
|
|
|
|
|
|
|
switch ($this->getEntityState($entity, self::STATE_DETACHED)) {
|
|
|
|
case self::STATE_MANAGED:
|
|
|
|
$this->removeFromIdentityMap($entity);
|
|
|
|
unset($this->_entityInsertions[$oid], $this->_entityUpdates[$oid],
|
|
|
|
$this->_entityDeletions[$oid], $this->_entityIdentifiers[$oid],
|
|
|
|
$this->_entityStates[$oid]);
|
|
|
|
break;
|
|
|
|
case self::STATE_NEW:
|
|
|
|
case self::STATE_DETACHED:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_cascadeDetach($entity, $visited);
|
|
|
|
}
|
|
|
|
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
2009-07-21 19:57:11 +04:00
|
|
|
* Refreshes the state of the given entity from the database, overwriting
|
|
|
|
* any local, unpersisted changes.
|
2009-07-21 19:53:58 +04:00
|
|
|
*
|
2009-07-21 19:57:11 +04:00
|
|
|
* @param object $entity The entity to refresh.
|
2009-07-25 20:33:29 +04:00
|
|
|
* @throws InvalidArgumentException If the entity is not MANAGED.
|
2009-07-21 19:53:58 +04:00
|
|
|
*/
|
|
|
|
public function refresh($entity)
|
|
|
|
{
|
|
|
|
$visited = array();
|
2009-07-21 19:57:11 +04:00
|
|
|
$this->_doRefresh($entity, $visited);
|
2009-07-21 19:53:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a refresh operation on an entity.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity to refresh.
|
|
|
|
* @param array $visited The already visited entities during cascades.
|
2009-07-25 20:33:29 +04:00
|
|
|
* @throws InvalidArgumentException If the entity is not MANAGED.
|
2009-07-21 19:53:58 +04:00
|
|
|
*/
|
|
|
|
private function _doRefresh($entity, array &$visited)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($visited[$oid])) {
|
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
|
|
|
$visited[$oid] = $entity; // mark visited
|
|
|
|
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
switch ($this->getEntityState($entity)) {
|
|
|
|
case self::STATE_MANAGED:
|
|
|
|
$this->getEntityPersister($class->name)->load(
|
|
|
|
array_combine($class->identifier, $this->_entityIdentifiers[$oid]),
|
|
|
|
$entity
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-25 20:33:29 +04:00
|
|
|
throw new \InvalidArgumentException("Entity is not MANAGED.");
|
2009-07-21 19:53:58 +04:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
2009-07-21 19:53:58 +04:00
|
|
|
$this->_cascadeRefresh($entity, $visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades a refresh operation to associated entities.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
*/
|
|
|
|
private function _cascadeRefresh($entity, array &$visited)
|
|
|
|
{
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
foreach ($class->associationMappings as $assocMapping) {
|
|
|
|
if ( ! $assocMapping->isCascadeRefresh) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection) {
|
2009-07-21 19:53:58 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
|
|
|
$this->_doRefresh($relatedEntity, $visited);
|
|
|
|
}
|
|
|
|
} else if ($relatedEntities !== null) {
|
|
|
|
$this->_doRefresh($relatedEntities, $visited);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades a detach operation to associated entities.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
*/
|
|
|
|
private function _cascadeDetach($entity, array &$visited)
|
|
|
|
{
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
foreach ($class->associationMappings as $assocMapping) {
|
|
|
|
if ( ! $assocMapping->isCascadeDetach) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection) {
|
2009-07-25 20:33:29 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
|
|
|
$this->_doDetach($relatedEntity, $visited);
|
|
|
|
}
|
|
|
|
} else if ($relatedEntities !== null) {
|
|
|
|
$this->_doDetach($relatedEntities, $visited);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades a merge operation to associated entities.
|
2009-07-03 21:36:41 +04:00
|
|
|
*
|
2009-05-28 15:13:12 +04:00
|
|
|
* @param object $entity
|
|
|
|
* @param object $managedCopy
|
|
|
|
* @param array $visited
|
2009-05-11 14:43:27 +04:00
|
|
|
*/
|
|
|
|
private function _cascadeMerge($entity, $managedCopy, array &$visited)
|
|
|
|
{
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->associationMappings as $assocMapping) {
|
2009-05-19 20:24:17 +04:00
|
|
|
if ( ! $assocMapping->isCascadeMerge) {
|
2009-05-11 14:43:27 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection) {
|
2009-05-11 14:43:27 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
|
|
|
$this->_doMerge($relatedEntity, $visited, $managedCopy, $assocMapping);
|
|
|
|
}
|
2009-07-02 15:48:44 +04:00
|
|
|
} else if ($relatedEntities !== null) {
|
2009-05-11 14:43:27 +04:00
|
|
|
$this->_doMerge($relatedEntities, $visited, $managedCopy, $assocMapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2009-05-28 15:13:12 +04:00
|
|
|
* @param array $insertNow
|
2008-07-21 00:13:24 +04:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
private function _cascadePersist($entity, array &$visited)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2008-12-18 17:08:11 +03:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->associationMappings as $assocMapping) {
|
2009-07-25 20:33:29 +04:00
|
|
|
if ( ! $assocMapping->isCascadePersist) {
|
2008-07-21 00:13:24 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-19 20:24:17 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if (($relatedEntities instanceof Collection || is_array($relatedEntities))) {
|
2008-07-21 00:13:24 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_doPersist($relatedEntity, $visited);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-07-02 15:48:44 +04:00
|
|
|
} else if ($relatedEntities !== null) {
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_doPersist($relatedEntities, $visited);
|
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-05-28 15:13:12 +04:00
|
|
|
* @param array $visited
|
2009-01-04 19:15:32 +03:00
|
|
|
*/
|
2009-07-19 20:54:53 +04:00
|
|
|
private function _cascadeRemove($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));
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->associationMappings as $assocMapping) {
|
2009-07-25 20:33:29 +04:00
|
|
|
if ( ! $assocMapping->isCascadeRemove) {
|
2009-01-04 19:15:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
2009-07-30 19:16:02 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection || is_array($relatedEntities)) {
|
2009-01-04 19:15:32 +03:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_doRemove($relatedEntity, $visited);
|
2009-01-04 19:15:32 +03:00
|
|
|
}
|
2009-07-02 15:48:44 +04:00
|
|
|
} else if ($relatedEntities !== null) {
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_doRemove($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
|
|
|
/**
|
2009-05-11 14:43:27 +04:00
|
|
|
* Clears the UnitOfWork.
|
2009-01-04 19:15:32 +03:00
|
|
|
*/
|
2009-05-11 14:43:27 +04:00
|
|
|
public function clear()
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2009-07-24 15:33:38 +04:00
|
|
|
$this->_identityMap =
|
|
|
|
$this->_entityIdentifiers =
|
|
|
|
$this->_originalEntityData =
|
|
|
|
$this->_entityChangeSets =
|
|
|
|
$this->_entityStates =
|
|
|
|
$this->_scheduledForDirtyCheck =
|
|
|
|
$this->_entityInsertions =
|
|
|
|
$this->_entityUpdates =
|
|
|
|
$this->_entityDeletions =
|
|
|
|
$this->_collectionDeletions =
|
|
|
|
//$this->_collectionCreations =
|
|
|
|
$this->_collectionUpdates =
|
|
|
|
$this->_orphanRemovals = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_commitOrderCalculator->clear();
|
|
|
|
}
|
2009-07-23 13:52:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
* Schedules an orphaned entity for removal. The remove() operation will be
|
|
|
|
* invoked on that entity at the beginning of the next commit of this
|
|
|
|
* UnitOfWork.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
*/
|
|
|
|
public function scheduleOrphanRemoval($entity)
|
|
|
|
{
|
|
|
|
$this->_orphanRemovals[spl_object_hash($entity)] = $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
* Schedules a complete collection for removal when this UnitOfWork commits.
|
|
|
|
*
|
|
|
|
* @param PersistentCollection $coll
|
|
|
|
*/
|
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-07-03 21:36:41 +04:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
public function isCollectionScheduledForDeletion(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2009-07-23 13:52:16 +04:00
|
|
|
/*public function scheduleCollectionRecreation(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
$this->_collectionRecreations[] = $coll;
|
2009-07-23 13:52:16 +04:00
|
|
|
}*/
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2009-07-23 13:52:16 +04:00
|
|
|
/*public function isCollectionScheduledForRecreation(PersistentCollection $coll)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
|
|
|
//...
|
2009-07-23 13:52:16 +04:00
|
|
|
}*/
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
2009-05-13 19:19:27 +04:00
|
|
|
* INTERNAL:
|
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-07-23 13:52:16 +04:00
|
|
|
* @return object The created entity instance.
|
|
|
|
* @internal Highly performance-sensitive method. Run the performance test suites when
|
2009-05-17 23:27:12 +04:00
|
|
|
* making modifications.
|
2008-07-10 21:17:58 +04:00
|
|
|
*/
|
2009-05-17 23:27:12 +04:00
|
|
|
public function createEntity($className, array $data, $hints = array())
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2009-02-17 13:54:18 +03:00
|
|
|
$class = $this->_em->getClassMetadata($className);
|
2009-01-03 22:50:13 +03:00
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
if ($class->isIdentifierComposite) {
|
|
|
|
$id = array();
|
|
|
|
foreach ($class->identifier as $fieldName) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$id[] = $data[$fieldName];
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
$idHash = implode(' ', $id);
|
2009-01-03 22:50:13 +03:00
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
$id = array($data[$class->identifier[0]]);
|
2009-01-03 22:50:13 +03:00
|
|
|
$idHash = $id[0];
|
|
|
|
}
|
2009-06-22 22:48:42 +04:00
|
|
|
|
|
|
|
if (isset($this->_identityMap[$class->rootEntityName][$idHash])) {
|
|
|
|
$entity = $this->_identityMap[$class->rootEntityName][$idHash];
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-07-21 13:25:14 +04:00
|
|
|
$overrideLocalChanges = isset($hints[Query::HINT_REFRESH]);
|
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);
|
|
|
|
$this->_entityIdentifiers[$oid] = $id;
|
2009-05-07 20:36:27 +04:00
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
2009-05-11 14:43:27 +04:00
|
|
|
$this->_originalEntityData[$oid] = $data;
|
2009-07-18 15:41:37 +04:00
|
|
|
$this->_identityMap[$class->rootEntityName][$idHash] = $entity;
|
|
|
|
if ($entity instanceof \Doctrine\Common\NotifyPropertyChanged) {
|
|
|
|
$entity->addPropertyChangedListener($this);
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
$overrideLocalChanges = true;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($overrideLocalChanges) {
|
2009-07-11 01:47:42 +04:00
|
|
|
if ($this->_useCExtension) {
|
|
|
|
doctrine_populate_data($entity, $data);
|
|
|
|
} else {
|
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
if (isset($class->reflFields[$field])) {
|
|
|
|
$class->reflFields[$field]->setValue($entity, $value);
|
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($data as $field => $value) {
|
2009-05-14 14:03:09 +04:00
|
|
|
if (isset($class->reflFields[$field])) {
|
|
|
|
$currentValue = $class->reflFields[$field]->getValue($entity);
|
2009-07-15 10:46:43 +04:00
|
|
|
// Only override the current value if:
|
|
|
|
// a) There was no original value yet (nothing in _originalEntityData)
|
|
|
|
// or
|
|
|
|
// b) The original value is the same as the current value (it was not changed).
|
2009-04-12 23:02:12 +04:00
|
|
|
if ( ! isset($this->_originalEntityData[$oid][$field]) ||
|
2009-05-17 23:27:12 +04:00
|
|
|
$currentValue == $this->_originalEntityData[$oid][$field]) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$class->reflFields[$field]->setValue($entity, $value);
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-07-18 22:06:30 +04:00
|
|
|
if (isset($class->lifecycleCallbacks[Events::postLoad])) {
|
2009-07-18 15:41:37 +04:00
|
|
|
$class->invokeLifecycleCallbacks(Events::postLoad, $entity);
|
|
|
|
}
|
|
|
|
if ($this->_evm->hasListeners(Events::postLoad)) {
|
2009-07-18 22:06:30 +04:00
|
|
|
$this->_evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity));
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
return $entity;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
2009-05-11 14:43:27 +04:00
|
|
|
* Sets a property value of the original data array of an entity.
|
2009-01-03 22:50:13 +03:00
|
|
|
*
|
|
|
|
* @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
|
2009-03-30 23:43:05 +04:00
|
|
|
* has a composite identifier then the identifier values are in the same
|
2009-01-09 19:25:06 +03:00
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
2009-05-11 14:43:27 +04:00
|
|
|
* Tries to find an entity with the given identifier in the identity map of
|
|
|
|
* this UnitOfWork.
|
2009-01-07 20:46:02 +03:00
|
|
|
*
|
2009-05-11 14:43:27 +04:00
|
|
|
* @param mixed $id The entity identifier to look for.
|
|
|
|
* @param string $rootClassName The name of the root class of the mapped entity hierarchy.
|
|
|
|
* @return mixed Returns the entity with the specified identifier if it exists in
|
|
|
|
* this UnitOfWork, FALSE otherwise.
|
2009-01-07 20:46:02 +03:00
|
|
|
*/
|
|
|
|
public function tryGetById($id, $rootClassName)
|
|
|
|
{
|
2009-04-09 22:12:48 +04:00
|
|
|
$idHash = implode(' ', (array)$id);
|
2009-01-07 20:46:02 +03:00
|
|
|
if (isset($this->_identityMap[$rootClassName][$idHash])) {
|
|
|
|
return $this->_identityMap[$rootClassName][$idHash];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/**
|
|
|
|
* Schedules an entity for dirty-checking at commit-time.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity to schedule for dirty-checking.
|
|
|
|
*/
|
2009-01-29 20:00:44 +03:00
|
|
|
public function scheduleForDirtyCheck($entity)
|
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$rootClassName = $this->_em->getClassMetadata(get_class($entity))->rootEntityName;
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->_scheduledForDirtyCheck[$rootClassName] = $entity;
|
|
|
|
}
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/**
|
|
|
|
* Checks whether the UnitOfWork has any pending insertions.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function hasPendingInsertions()
|
|
|
|
{
|
|
|
|
return ! empty($this->_entityInsertions);
|
|
|
|
}
|
|
|
|
|
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;
|
2009-07-15 10:46:43 +04:00
|
|
|
foreach ($this->_identityMap as $entitySet) {
|
|
|
|
$count += count($entitySet);
|
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
return $count;
|
|
|
|
}
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EntityPersister for an Entity.
|
|
|
|
*
|
|
|
|
* @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);
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($class->isInheritanceTypeNone()) {
|
2009-05-18 00:45:50 +04:00
|
|
|
$persister = new Persisters\StandardEntityPersister($this->_em, $class);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($class->isInheritanceTypeSingleTable()) {
|
|
|
|
$persister = new Persisters\SingleTablePersister($this->_em, $class);
|
|
|
|
} else 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-04-09 22:12:48 +04:00
|
|
|
$persister = new Persisters\UnionSubclassPersister($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-04-09 22:12:48 +04:00
|
|
|
|
2009-05-13 19:19:27 +04:00
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
* Registers an entity as managed.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity.
|
|
|
|
* @param array $id The identifier values.
|
|
|
|
* @param array $data The original entity data.
|
|
|
|
*/
|
|
|
|
public function registerManaged($entity, $id, $data)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
$this->_entityIdentifiers[$oid] = $id;
|
|
|
|
$this->_entityStates[$oid] = self::STATE_MANAGED;
|
|
|
|
$this->_originalEntityData[$oid] = $data;
|
|
|
|
$this->addToIdentityMap($entity);
|
|
|
|
}
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2009-05-26 19:42:54 +04:00
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
* Clears the property changeset of the entity with the given OID.
|
|
|
|
*
|
|
|
|
* @param string $oid The entity's OID.
|
|
|
|
*/
|
|
|
|
public function clearEntityChangeSet($oid)
|
|
|
|
{
|
2009-05-28 15:39:16 +04:00
|
|
|
unset($this->_entityChangeSets[$oid]);
|
2009-05-26 19:42:54 +04:00
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/* PropertyChangedListener implementation */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies this UnitOfWork of a property change in an entity.
|
|
|
|
*
|
|
|
|
* @param object $entity The entity that owns the property.
|
|
|
|
* @param string $propertyName The name of the property that changed.
|
|
|
|
* @param mixed $oldValue The old value of the property.
|
|
|
|
* @param mixed $newValue The new value of the property.
|
|
|
|
*/
|
|
|
|
public function propertyChanged($entity, $propertyName, $oldValue, $newValue)
|
|
|
|
{
|
2009-07-25 20:33:29 +04:00
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
$this->_entityChangeSets[$oid][$propertyName] = array($oldValue, $newValue);
|
2009-05-11 14:43:27 +04:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
if (isset($class->associationMappings[$propertyName])) {
|
|
|
|
$assoc = $class->associationMappings[$propertyName];
|
|
|
|
if ($assoc->isOneToOne() && $assoc->isOwningSide) {
|
2009-05-11 14:43:27 +04:00
|
|
|
$this->_entityUpdates[$oid] = $entity;
|
2009-07-25 20:33:29 +04:00
|
|
|
} else if ($oldValue instanceof PersistentCollection) {
|
|
|
|
// A PersistentCollection was de-referenced, so delete it.
|
|
|
|
if ( ! in_array($oldValue, $this->_collectionDeletions, true)) {
|
|
|
|
$this->_collectionDeletions[] = $oldValue;
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
} else {
|
|
|
|
$this->_entityUpdates[$oid] = $entity;
|
2009-07-23 13:52:16 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
public function dump()
|
|
|
|
{
|
|
|
|
var_dump($this->_identityMap);
|
|
|
|
var_dump($this->_entityIdentifiers);
|
|
|
|
var_dump($this->_originalEntityData);
|
|
|
|
var_dump($this->_entityChangeSets);
|
|
|
|
var_dump($this->_entityStates);
|
|
|
|
var_dump($this->_scheduledForDirtyCheck);
|
|
|
|
var_dump($this->_entityInsertions);
|
|
|
|
var_dump($this->_entityUpdates);
|
|
|
|
var_dump($this->_entityDeletions);
|
|
|
|
var_dump($this->_collectionDeletions);
|
|
|
|
//$this->_collectionCreations =
|
|
|
|
var_dump($this->_collectionUpdates);
|
|
|
|
var_dump($this->_orphanRemovals);
|
|
|
|
//var_dump($this->_commitOrderCalculator->clear();
|
|
|
|
|
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|