2007-10-23 01:47:05 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the 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
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
use Exception, InvalidArgumentException, UnexpectedValueException,
|
2010-05-11 01:51:56 +04:00
|
|
|
Doctrine\Common\Collections\ArrayCollection,
|
2009-07-29 16:00:08 +04:00
|
|
|
Doctrine\Common\Collections\Collection,
|
2009-10-07 16:39:46 +04:00
|
|
|
Doctrine\Common\NotifyPropertyChanged,
|
2009-07-29 15:57:27 +04:00
|
|
|
Doctrine\Common\PropertyChangedListener,
|
2009-10-12 15:55:22 +04:00
|
|
|
Doctrine\ORM\Event\LifecycleEventArgs,
|
2010-08-09 15:13:21 +04:00
|
|
|
Doctrine\ORM\Mapping\ClassMetadata,
|
2009-10-12 15:55:22 +04:00
|
|
|
Doctrine\ORM\Proxy\Proxy;
|
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
|
|
|
*
|
2008-05-01 13:41:47 +04:00
|
|
|
* @since 2.0
|
2010-03-31 01:14:17 +04:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
2008-02-24 01:04:39 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2010-04-26 15:02:30 +04:00
|
|
|
* @internal This class contains highly performance-sensitive code.
|
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;
|
|
|
|
|
|
|
|
/**
|
2010-07-08 02:20:54 +04:00
|
|
|
* A detached entity is an instance with persistent state and 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,
|
2010-07-08 02:20:54 +04:00
|
|
|
* associated with an EntityManager, whose persistent state will be deleted
|
|
|
|
* on commit.
|
2008-12-18 17:08:11 +03:00
|
|
|
*/
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $entityIdentifiers = array();
|
2009-01-03 22:50:13 +03:00
|
|
|
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
2010-07-08 02:20:54 +04:00
|
|
|
* @todo rename: scheduledForSynchronization
|
2009-01-29 20:00:44 +03:00
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $extraUpdates = array();
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* A list of all pending entity deletions.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $collectionDeletions = 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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $collectionUpdates = array();
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04: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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $commitOrderCalculator;
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The entity persister instances used to persist entity instances.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $persisters = array();
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The collection persister instances used to persist collections.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $collectionPersisters = array();
|
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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
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
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private $orphanRemovals = array();
|
2009-12-16 00:06:32 +03:00
|
|
|
|
|
|
|
//private $_readOnlyObjects = array();
|
2009-07-11 01:47:42 +04:00
|
|
|
|
2010-12-31 15:11:01 +03:00
|
|
|
/**
|
|
|
|
* Map of Entity Class-Names and corresponding IDs that should eager loaded when requested.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $eagerLoadingEntities = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of Collections that should be eager loaded when requested.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $eagerLoadingCollections = array();
|
|
|
|
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->em = $em;
|
|
|
|
$this->evm = $em->getEventManager();
|
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.
|
2009-10-07 16:39:46 +04:00
|
|
|
*
|
|
|
|
* The operations are executed in the following order:
|
|
|
|
*
|
|
|
|
* 1) All entity insertions
|
|
|
|
* 2) All entity updates
|
|
|
|
* 3) All collection deletions
|
|
|
|
* 4) All collection updates
|
|
|
|
* 5) All entity deletions
|
|
|
|
*
|
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.
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->computeChangeSets();
|
|
|
|
|
2010-07-08 02:20:54 +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.
|
|
|
|
}
|
2010-03-18 16:43:38 +03:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->orphanRemovals) {
|
|
|
|
foreach ($this->orphanRemovals as $orphan) {
|
2009-07-23 13:52:16 +04:00
|
|
|
$this->remove($orphan);
|
|
|
|
}
|
|
|
|
}
|
2009-10-26 00:40:57 +03:00
|
|
|
|
2010-02-24 22:19:04 +03:00
|
|
|
// Raise onFlush
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->evm->hasListeners(Events::onFlush)) {
|
|
|
|
$this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em));
|
2010-02-24 22:19:04 +03:00
|
|
|
}
|
|
|
|
|
2009-10-26 00:40:57 +03:00
|
|
|
// Now we need a commit order to maintain referential integrity
|
2010-07-08 02:20:54 +04:00
|
|
|
$commitOrder = $this->getCommitOrder();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$conn = $this->em->getConnection();
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-08-26 13:10:14 +04:00
|
|
|
$conn->beginTransaction();
|
2010-05-11 01:51:56 +04:00
|
|
|
try {
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->entityInsertions) {
|
2009-07-18 15:41:37 +04:00
|
|
|
foreach ($commitOrder as $class) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->executeInserts($class);
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
}
|
2010-05-11 01:51:56 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->entityUpdates) {
|
2009-07-18 15:41:37 +04:00
|
|
|
foreach ($commitOrder as $class) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->executeUpdates($class);
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
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.
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->extraUpdates) {
|
|
|
|
$this->executeExtraUpdates();
|
2009-07-03 21:36:41 +04:00
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
|
|
|
|
// Collection deletions (deletions of complete collections)
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->collectionDeletions as $collectionToDelete) {
|
2009-05-19 20:11:08 +04:00
|
|
|
$this->getCollectionPersister($collectionToDelete->getMapping())
|
|
|
|
->delete($collectionToDelete);
|
|
|
|
}
|
|
|
|
// Collection updates (deleteRows, updateRows, insertRows)
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->collectionUpdates as $collectionToUpdate) {
|
2009-05-19 20:11:08 +04:00
|
|
|
$this->getCollectionPersister($collectionToUpdate->getMapping())
|
|
|
|
->update($collectionToUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entity deletions come last and need to be in reverse commit order
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->entityDeletions) {
|
2009-07-18 15:41:37 +04:00
|
|
|
for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->executeDeletions($commitOrder[$i]);
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
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();
|
2010-05-11 01:51:56 +04:00
|
|
|
} catch (Exception $e) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->em->close();
|
2010-05-11 01:51:56 +04:00
|
|
|
$conn->rollback();
|
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
|
2010-07-08 02:20:54 +04:00
|
|
|
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
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityInsertions =
|
|
|
|
$this->entityUpdates =
|
|
|
|
$this->entityDeletions =
|
|
|
|
$this->extraUpdates =
|
|
|
|
$this->entityChangeSets =
|
|
|
|
$this->collectionUpdates =
|
|
|
|
$this->collectionDeletions =
|
|
|
|
$this->visitedCollections =
|
|
|
|
$this->scheduledForDirtyCheck =
|
|
|
|
$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.
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function executeExtraUpdates()
|
2009-07-03 21:36:41 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->extraUpdates as $oid => $update) {
|
2009-07-03 21:36:41 +04:00
|
|
|
list ($entity, $changeset) = $update;
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityChangeSets[$oid] = $changeset;
|
2009-07-03 21:36:41 +04:00
|
|
|
$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);
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityChangeSets[$oid])) {
|
|
|
|
return $this->entityChangeSets[$oid];
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2010-02-22 00:55:39 +03:00
|
|
|
/**
|
2010-03-18 16:43:38 +03:00
|
|
|
* Computes the changes that happened to a single entity.
|
2009-05-05 22:39:25 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-08-09 23:44:52 +04:00
|
|
|
public function computeChangeSet(ClassMetadata $class, $entity)
|
2009-05-05 22:39:25 +04:00
|
|
|
{
|
|
|
|
if ( ! $class->isInheritanceTypeNone()) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-05-05 22:39:25 +04:00
|
|
|
}
|
2010-03-18 16:43:38 +03:00
|
|
|
|
|
|
|
$oid = spl_object_hash($entity);
|
2009-05-05 22:39:25 +04:00
|
|
|
$actualData = array();
|
2009-05-19 20:24:17 +04:00
|
|
|
foreach ($class->reflFields as $name => $refProp) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$value = $refProp->getValue($entity);
|
|
|
|
if ($class->isCollectionValuedAssociation($name) && $value !== null
|
|
|
|
&& ! ($value instanceof PersistentCollection)) {
|
|
|
|
// If $value is not a Collection then use an ArrayCollection.
|
|
|
|
if ( ! $value instanceof Collection) {
|
|
|
|
$value = new ArrayCollection($value);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2009-09-06 07:25:44 +04:00
|
|
|
|
2009-05-19 20:24:17 +04:00
|
|
|
$assoc = $class->associationMappings[$name];
|
2009-09-06 07:25:44 +04:00
|
|
|
|
2009-05-05 22:39:25 +04:00
|
|
|
// Inject PersistentCollection
|
2009-09-06 07:25:44 +04:00
|
|
|
$coll = new PersistentCollection(
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->em,
|
2010-08-09 15:13:21 +04:00
|
|
|
$this->em->getClassMetadata($assoc['targetEntity']),
|
2010-07-08 02:20:54 +04:00
|
|
|
$value
|
2009-09-06 07:25:44 +04:00
|
|
|
);
|
|
|
|
|
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;
|
2010-07-08 02:20:54 +04:00
|
|
|
} else if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) {
|
|
|
|
$actualData[$name] = $value;
|
2009-05-05 22:39:25 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2010-07-08 02:20:54 +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.
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid] = $actualData;
|
|
|
|
$changeSet = array();
|
|
|
|
foreach ($actualData as $propName => $actualValue) {
|
|
|
|
if (isset($class->associationMappings[$propName])) {
|
|
|
|
$assoc = $class->associationMappings[$propName];
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$changeSet[$propName] = array(null, $actualValue);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$changeSet[$propName] = array(null, $actualValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->entityChangeSets[$oid] = $changeSet;
|
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
|
2010-07-08 02:20:54 +04:00
|
|
|
$originalData = $this->originalEntityData[$oid];
|
2010-07-16 13:16:06 +04:00
|
|
|
$isChangeTrackingNotify = $class->isChangeTrackingNotify();
|
2010-07-15 17:52:42 +04:00
|
|
|
$changeSet = $isChangeTrackingNotify ? $this->entityChangeSets[$oid] : array();
|
2009-05-05 22:39:25 +04:00
|
|
|
|
|
|
|
foreach ($actualData as $propName => $actualValue) {
|
|
|
|
$orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($class->associationMappings[$propName])) {
|
|
|
|
$assoc = $class->associationMappings[$propName];
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] & ClassMetadata::TO_ONE && $orgValue !== $actualValue) {
|
|
|
|
if ($assoc['isOwningSide']) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$changeSet[$propName] = array($orgValue, $actualValue);
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($orgValue !== null && $assoc['orphanRemoval']) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->scheduleOrphanRemoval($orgValue);
|
|
|
|
}
|
|
|
|
} else if ($orgValue instanceof PersistentCollection && $orgValue !== $actualValue) {
|
|
|
|
// A PersistentCollection was de-referenced, so delete it.
|
|
|
|
if ( ! in_array($orgValue, $this->collectionDeletions, true)) {
|
|
|
|
$this->collectionDeletions[] = $orgValue;
|
2011-01-23 17:40:16 +03:00
|
|
|
$changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored.
|
2010-07-08 02:20:54 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-15 17:52:42 +04:00
|
|
|
} else if ($isChangeTrackingNotify) {
|
|
|
|
continue;
|
2010-07-08 02:20:54 +04:00
|
|
|
} else if (is_object($orgValue) && $orgValue !== $actualValue) {
|
2009-05-05 22:39:25 +04:00
|
|
|
$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);
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-05-05 22:39:25 +04:00
|
|
|
if ($changeSet) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityChangeSets[$oid] = $changeSet;
|
|
|
|
$this->originalEntityData[$oid] = $actualData;
|
|
|
|
$this->entityUpdates[$oid] = $entity;
|
2009-05-05 22:39:25 +04:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2010-05-01 14:14:16 +04:00
|
|
|
|
2010-03-18 16:43:38 +03:00
|
|
|
// Look for changes in associations of the entity
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($class->associationMappings as $field => $assoc) {
|
|
|
|
$val = $class->reflFields[$field]->getValue($entity);
|
2010-03-18 16:43:38 +03:00
|
|
|
if ($val !== null) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->computeAssociationChanges($assoc, $val);
|
2010-03-18 16:43:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes all the changes that have been done to entities and collections
|
|
|
|
* since the last commit and stores these changes in the _entityChangeSet map
|
|
|
|
* temporarily for access by the persisters, until the UoW commit is finished.
|
|
|
|
*/
|
|
|
|
public function computeChangeSets()
|
|
|
|
{
|
|
|
|
// Compute changes for INSERTed entities first. This must always happen.
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->entityInsertions as $entity) {
|
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2010-03-18 16:43:38 +03:00
|
|
|
$this->computeChangeSet($class, $entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute changes for other MANAGED entities. Change tracking policies take effect here.
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->identityMap as $className => $entities) {
|
|
|
|
$class = $this->em->getClassMetadata($className);
|
2010-03-18 16:43:38 +03:00
|
|
|
|
2010-07-15 17:52:42 +04:00
|
|
|
// Skip class if instances are read-only
|
|
|
|
//if ($class->isReadOnly) {
|
|
|
|
// continue;
|
|
|
|
//}
|
2010-03-18 16:43:38 +03:00
|
|
|
|
2010-07-15 17:52:42 +04:00
|
|
|
// If change tracking is explicit or happens through notification, then only compute
|
|
|
|
// changes on entities of that type that are explicitly marked for synchronization.
|
|
|
|
$entitiesToProcess = ! $class->isChangeTrackingDeferredImplicit() ?
|
2010-07-08 02:20:54 +04:00
|
|
|
(isset($this->scheduledForDirtyCheck[$className]) ?
|
|
|
|
$this->scheduledForDirtyCheck[$className] : array())
|
2010-03-20 12:40:50 +03:00
|
|
|
: $entities;
|
2010-03-18 16:43:38 +03:00
|
|
|
|
|
|
|
foreach ($entitiesToProcess as $entity) {
|
|
|
|
// Ignore uninitialized proxy objects
|
|
|
|
if (/* $entity is readOnly || */ $entity instanceof Proxy && ! $entity->__isInitialized__) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Only MANAGED entities that are NOT SCHEDULED FOR INSERTION are processed here.
|
|
|
|
$oid = spl_object_hash($entity);
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->entityInsertions[$oid]) && isset($this->entityStates[$oid])) {
|
2010-03-18 16:43:38 +03:00
|
|
|
$this->computeChangeSet($class, $entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04: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()) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['isOwningSide']) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->collectionUpdates[] = $value;
|
2009-02-07 20:02:13 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->visitedCollections[] = $value;
|
2009-02-07 20:02:13 +03:00
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
2010-07-04 20:55:49 +04:00
|
|
|
// Look through the entities, and in any of their associations, for transient (new)
|
2009-02-07 20:02:13 +03:00
|
|
|
// enities, recursively. ("Persistence by reachability")
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] & ClassMetadata::TO_ONE) {
|
2010-01-05 14:45:38 +03:00
|
|
|
if ($value instanceof Proxy && ! $value->__isInitialized__) {
|
2009-10-15 18:39:43 +04:00
|
|
|
return; // Ignore uninitialized proxy objects
|
2009-10-12 15:55:22 +04:00
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
$value = array($value);
|
2009-10-15 18:39:43 +04:00
|
|
|
} else if ($value instanceof PersistentCollection) {
|
2010-07-08 02:20:54 +04:00
|
|
|
// Unwrap. Uninitialized collections will simply be empty.
|
2009-10-15 18:39:43 +04:00
|
|
|
$value = $value->unwrap();
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
2009-01-09 19:25:06 +03:00
|
|
|
foreach ($value as $entry) {
|
2010-07-08 02:20:54 +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) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! $assoc['isCascadePersist']) {
|
2010-07-08 02:20:54 +04:00
|
|
|
throw new InvalidArgumentException("A new entity was found through a relationship that was not"
|
|
|
|
. " configured to cascade persist operations: " . self::objToStr($entry) . "."
|
|
|
|
. " Explicitly persist the new entity or configure cascading persist operations"
|
|
|
|
. " on the relationship.");
|
|
|
|
}
|
2010-07-04 20:55:49 +04:00
|
|
|
$this->persistNew($targetClass, $entry);
|
2010-02-22 00:55:39 +03:00
|
|
|
$this->computeChangeSet($targetClass, $entry);
|
2009-07-25 20:33:29 +04:00
|
|
|
} else if ($state == self::STATE_REMOVED) {
|
2010-07-08 02:20:54 +04:00
|
|
|
return new InvalidArgumentException("Removed entity detected during flush: "
|
2010-10-07 01:09:49 +04:00
|
|
|
. self::objToStr($entry).". Remove deleted entities from associations.");
|
2010-07-04 20:55:49 +04:00
|
|
|
} else if ($state == self::STATE_DETACHED) {
|
2010-07-08 02:20:54 +04:00
|
|
|
// Can actually not happen right now as we assume STATE_NEW,
|
|
|
|
// so the exception will be raised from the DBAL layer (constraint violation).
|
|
|
|
throw new InvalidArgumentException("A detached entity was found through a "
|
|
|
|
. "relationship during cascading a persist operation.");
|
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.
|
|
|
|
}
|
|
|
|
}
|
2010-07-04 20:55:49 +04:00
|
|
|
|
|
|
|
private function persistNew($class, $entity)
|
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($class->lifecycleCallbacks[Events::prePersist])) {
|
|
|
|
$class->invokeLifecycleCallbacks(Events::prePersist, $entity);
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->evm->hasListeners(Events::prePersist)) {
|
|
|
|
$this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($entity, $this->em));
|
2010-07-04 20:55:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$idGen = $class->idGenerator;
|
|
|
|
if ( ! $idGen->isPostInsertGenerator()) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$idValue = $idGen->generate($this->em, $entity);
|
2010-07-04 20:55:49 +04:00
|
|
|
if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityIdentifiers[$oid] = array($class->identifier[0] => $idValue);
|
2010-07-25 18:32:04 +04:00
|
|
|
$class->setIdentifierValues($entity, $this->entityIdentifiers[$oid]);
|
2010-07-04 20:55:49 +04:00
|
|
|
} else {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityIdentifiers[$oid] = $idValue;
|
2010-07-04 20:55:49 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityStates[$oid] = self::STATE_MANAGED;
|
2010-07-04 20:55:49 +04:00
|
|
|
|
|
|
|
$this->scheduleForInsert($entity);
|
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
/**
|
2009-08-13 14:13:06 +04:00
|
|
|
* INTERNAL:
|
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().
|
|
|
|
*
|
2009-11-13 19:39:28 +03:00
|
|
|
* The passed entity must be a managed entity. If the entity already has a change set
|
|
|
|
* because this method is invoked during a commit cycle then the change sets are added.
|
|
|
|
* whereby changes detected in this method prevail.
|
2009-10-06 14:04:32 +04:00
|
|
|
*
|
|
|
|
* @ignore
|
2009-11-13 19:39:28 +03:00
|
|
|
* @param ClassMetadata $class The class descriptor of the entity.
|
|
|
|
* @param object $entity The entity for which to (re)calculate the change set.
|
|
|
|
* @throws InvalidArgumentException If the passed entity is not MANAGED.
|
2009-07-18 15:41:37 +04:00
|
|
|
*/
|
2010-02-22 00:55:39 +03:00
|
|
|
public function recomputeSingleEntityChangeSet($class, $entity)
|
2009-07-18 15:41:37 +04:00
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
2009-11-13 19:39:28 +03:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) {
|
|
|
|
throw new InvalidArgumentException('Entity must be managed.');
|
2009-11-13 19:39:28 +03:00
|
|
|
}
|
2009-11-13 22:34:10 +03:00
|
|
|
|
|
|
|
/* TODO: Just return if changetracking policy is NOTIFY?
|
|
|
|
if ($class->isChangeTrackingNotify()) {
|
|
|
|
return;
|
|
|
|
}*/
|
2009-07-18 15:41:37 +04:00
|
|
|
|
|
|
|
if ( ! $class->isInheritanceTypeNone()) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$actualData = array();
|
|
|
|
foreach ($class->reflFields as $name => $refProp) {
|
|
|
|
if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) {
|
|
|
|
$actualData[$name] = $refProp->getValue($entity);
|
|
|
|
}
|
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$originalData = $this->originalEntityData[$oid];
|
2009-11-13 19:39:28 +03:00
|
|
|
$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);
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
2009-11-13 19:39:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($changeSet) {
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityChangeSets[$oid])) {
|
|
|
|
$this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet);
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid] = $actualData;
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +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]);
|
2010-07-08 02:20:54 +04:00
|
|
|
$hasListeners = $this->evm->hasListeners(Events::postPersist);
|
2009-07-18 22:06:30 +04:00
|
|
|
if ($hasLifecycleCallbacks || $hasListeners) {
|
|
|
|
$entities = array();
|
|
|
|
}
|
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->entityInsertions as $oid => $entity) {
|
2009-10-26 00:40:57 +03:00
|
|
|
if (get_class($entity) === $className) {
|
2009-05-19 20:11:08 +04:00
|
|
|
$persister->addInsert($entity);
|
2010-07-08 02:20: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-12-18 15:30:19 +03:00
|
|
|
|
2009-05-19 20:11:08 +04:00
|
|
|
$postInsertIds = $persister->executeInserts();
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-05-19 20:11:08 +04:00
|
|
|
if ($postInsertIds) {
|
2009-11-13 22:34:10 +03:00
|
|
|
// Persister returned 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);
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityIdentifiers[$oid] = array($idField => $id);
|
|
|
|
$this->entityStates[$oid] = self::STATE_MANAGED;
|
|
|
|
$this->originalEntityData[$oid][$idField] = $id;
|
2009-05-19 20:11:08 +04:00
|
|
|
$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) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($entity, $this->em));
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function executeUpdates($class)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $class->name;
|
2009-02-02 14:55:50 +03:00
|
|
|
$persister = $this->getEntityPersister($className);
|
2010-02-23 23:36:07 +03:00
|
|
|
|
|
|
|
$hasPreUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preUpdate]);
|
2010-07-08 02:20:54 +04:00
|
|
|
$hasPreUpdateListeners = $this->evm->hasListeners(Events::preUpdate);
|
2009-07-18 22:06:30 +04:00
|
|
|
$hasPostUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postUpdate]);
|
2010-07-08 02:20:54 +04:00
|
|
|
$hasPostUpdateListeners = $this->evm->hasListeners(Events::postUpdate);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->entityUpdates as $oid => $entity) {
|
2010-02-23 23:36:07 +03:00
|
|
|
if (get_class($entity) == $className || $entity instanceof Proxy && $entity instanceof $className) {
|
|
|
|
|
|
|
|
if ($hasPreUpdateLifecycleCallbacks) {
|
|
|
|
$class->invokeLifecycleCallbacks(Events::preUpdate, $entity);
|
2010-03-15 01:16:15 +03:00
|
|
|
$this->recomputeSingleEntityChangeSet($class, $entity);
|
2010-02-23 23:36:07 +03:00
|
|
|
}
|
2010-03-15 01:16:15 +03:00
|
|
|
|
2010-02-23 23:36:07 +03:00
|
|
|
if ($hasPreUpdateListeners) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->evm->dispatchEvent(Events::preUpdate, new Event\PreUpdateEventArgs(
|
|
|
|
$entity, $this->em, $this->entityChangeSets[$oid])
|
2010-03-15 01:16:15 +03:00
|
|
|
);
|
2010-02-23 23:36:07 +03:00
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
$persister->update($entity);
|
2010-07-08 02:20: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) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($entity, $this->em));
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function executeDeletions($class)
|
2008-07-21 00:13:24 +04: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::postRemove]);
|
2010-07-08 02:20:54 +04:00
|
|
|
$hasListeners = $this->evm->hasListeners(Events::postRemove);
|
2009-07-18 22:06:30 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->entityDeletions as $oid => $entity) {
|
2010-02-10 13:47:42 +03:00
|
|
|
if (get_class($entity) == $className || $entity instanceof Proxy && $entity instanceof $className) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$persister->delete($entity);
|
2009-11-11 19:20:29 +03:00
|
|
|
unset(
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityDeletions[$oid],
|
|
|
|
$this->entityIdentifiers[$oid],
|
|
|
|
$this->originalEntityData[$oid],
|
|
|
|
$this->entityStates[$oid]
|
2009-11-11 19:20:29 +03:00
|
|
|
);
|
|
|
|
// Entity with this $oid after deletion treated as NEW, even if the $oid
|
|
|
|
// is obtained by a new entity because the old one went out of scope.
|
2010-07-08 02:20:54 +04:00
|
|
|
//$this->entityStates[$oid] = self::STATE_NEW;
|
|
|
|
if ( ! $class->isIdentifierNatural()) {
|
|
|
|
$class->reflFields[$class->identifier[0]]->setValue($entity, null);
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($entity, $this->em));
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the commit order.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-07-08 02:20:54 +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(
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityInsertions,
|
|
|
|
$this->entityUpdates,
|
|
|
|
$this->entityDeletions
|
2009-07-03 21:36:41 +04:00
|
|
|
);
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2009-10-26 00:40:57 +03:00
|
|
|
|
2009-12-21 14:06:27 +03:00
|
|
|
$calc = $this->getCommitOrderCalculator();
|
|
|
|
|
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();
|
2009-10-26 00:40:57 +03:00
|
|
|
foreach ($entityChangeSet as $oid => $entity) {
|
2009-10-28 14:00:17 +03:00
|
|
|
$className = get_class($entity);
|
2009-12-21 14:06:27 +03:00
|
|
|
if ( ! $calc->hasClass($className)) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata($className);
|
2009-12-21 14:06:27 +03:00
|
|
|
$calc->addClass($class);
|
2009-07-27 13:50:22 +04:00
|
|
|
$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-10-26 00:40:57 +03:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
|
|
|
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
2009-12-21 14:06:27 +03:00
|
|
|
if ( ! $calc->hasClass($targetClass->name)) {
|
|
|
|
$calc->addClass($targetClass);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-12-21 14:06:27 +03:00
|
|
|
$calc->addDependency($targetClass, $class);
|
2010-06-06 20:23:37 +04:00
|
|
|
// If the target class has mapped subclasses,
|
|
|
|
// these share the same dependency.
|
|
|
|
if ($targetClass->subClasses) {
|
|
|
|
foreach ($targetClass->subClasses as $subClassName) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$targetSubClass = $this->em->getClassMetadata($subClassName);
|
2010-06-06 20:23:37 +04:00
|
|
|
if ( ! $calc->hasClass($subClassName)) {
|
|
|
|
$calc->addClass($targetSubClass);
|
|
|
|
}
|
|
|
|
$calc->addDependency($targetSubClass, $class);
|
|
|
|
}
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-21 14:06:27 +03:00
|
|
|
return $calc->getCommitOrder();
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
|
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-08-13 14:13:06 +04:00
|
|
|
* @param object $entity The entity to schedule for insertion.
|
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
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityUpdates[$oid])) {
|
|
|
|
throw new InvalidArgumentException("Dirty entity can not be scheduled for insertion.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityDeletions[$oid])) {
|
|
|
|
throw new InvalidArgumentException("Removed entity can not be scheduled for insertion.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityInsertions[$oid])) {
|
|
|
|
throw new InvalidArgumentException("Entity can not be scheduled for insertion twice.");
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityInsertions[$oid] = $entity;
|
2009-12-18 15:30:19 +03:00
|
|
|
|
2010-07-08 02:20:54 +04: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-08-13 14:13:06 +04:00
|
|
|
* Checks whether an entity is scheduled for insertion.
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04: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
|
|
|
/**
|
2009-08-13 14:13:06 +04:00
|
|
|
* Schedules an entity for being updated.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2009-08-13 14:13:06 +04:00
|
|
|
* @param object $entity The entity to schedule for being updated.
|
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);
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->entityIdentifiers[$oid])) {
|
|
|
|
throw new InvalidArgumentException("Entity has no identity.");
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityDeletions[$oid])) {
|
|
|
|
throw new InvalidArgumentException("Entity is removed.");
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2010-07-08 02:20:54 +04: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
|
2009-08-13 14:13:06 +04:00
|
|
|
* regular entity updates within the currently running commit cycle.
|
2009-07-18 15:41:37 +04:00
|
|
|
*
|
2009-11-15 14:42:05 +03:00
|
|
|
* Extra updates for entities are stored as (entity, changeset) tuples.
|
|
|
|
*
|
2009-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
2009-11-15 14:42:05 +03:00
|
|
|
* @param object $entity The entity for which to schedule an extra update.
|
|
|
|
* @param array $changeset The changeset of the entity (what to update).
|
2009-07-18 15:41:37 +04:00
|
|
|
*/
|
2009-07-03 21:36:41 +04:00
|
|
|
public function scheduleExtraUpdate($entity, array $changeset)
|
|
|
|
{
|
2009-11-15 14:42:05 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->extraUpdates[$oid])) {
|
|
|
|
list($ignored, $changeset2) = $this->extraUpdates[$oid];
|
|
|
|
$this->extraUpdates[$oid] = array($entity, $changeset + $changeset2);
|
2009-11-15 14:42:05 +03:00
|
|
|
} else {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->extraUpdates[$oid] = array($entity, $changeset);
|
2009-11-15 14:42:05 +03:00
|
|
|
}
|
2009-07-03 21:36:41 +04:00
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
/**
|
|
|
|
* Checks whether an entity is registered as dirty in the unit of work.
|
|
|
|
* Note: Is not very useful currently as dirty entities are only registered
|
|
|
|
* at commit time.
|
|
|
|
*
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04: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
|
|
|
|
2010-07-15 17:52:42 +04:00
|
|
|
public function isScheduledForDirtyCheck($entity)
|
|
|
|
{
|
|
|
|
$rootEntityName = $this->em->getClassMetadata(get_class($entity))->rootEntityName;
|
|
|
|
return isset($this->scheduledForDirtyCheck[$rootEntityName][spl_object_hash($entity)]);
|
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
/**
|
2010-02-24 22:19:04 +03:00
|
|
|
* INTERNAL:
|
|
|
|
* Schedules an entity for deletion.
|
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);
|
2009-11-11 19:20:29 +03:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityInsertions[$oid])) {
|
2009-11-11 19:20:29 +03:00
|
|
|
if ($this->isInIdentityMap($entity)) {
|
|
|
|
$this->removeFromIdentityMap($entity);
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
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
|
|
|
}
|
2010-02-10 13:47:42 +03:00
|
|
|
|
2009-11-11 19:20:29 +03:00
|
|
|
if ( ! $this->isInIdentityMap($entity)) {
|
|
|
|
return; // ignore
|
|
|
|
}
|
2010-02-10 13:47:42 +03:00
|
|
|
|
2009-11-11 19:20:29 +03:00
|
|
|
$this->removeFromIdentityMap($entity);
|
2008-09-07 17:48:40 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->entityUpdates[$oid])) {
|
|
|
|
unset($this->entityUpdates[$oid]);
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04: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
|
|
|
{
|
2010-07-08 02:20:54 +04: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);
|
2010-07-08 02:20:54 +04: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-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$classMetadata = $this->em->getClassMetadata(get_class($entity));
|
|
|
|
$idHash = implode(' ', $this->entityIdentifiers[spl_object_hash($entity)]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2010-07-08 02:20:54 +04:00
|
|
|
throw new InvalidArgumentException("The given entity has no identity.");
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $classMetadata->rootEntityName;
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->identityMap[$className][$idHash])) {
|
2008-02-28 18:30:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->identityMap[$className][$idHash] = $entity;
|
2009-10-07 16:39:46 +04:00
|
|
|
if ($entity instanceof NotifyPropertyChanged) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$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
|
|
|
/**
|
2010-07-04 20:55:49 +04:00
|
|
|
* Gets the state of an entity with regard to the current unit of work.
|
2008-12-18 17:08:11 +03:00
|
|
|
*
|
2009-05-11 14:43:27 +04:00
|
|
|
* @param object $entity
|
2010-07-04 20:55:49 +04:00
|
|
|
* @param integer $assume The state to assume if the state is not yet known (not MANAGED or REMOVED).
|
|
|
|
* This parameter can be set to improve performance of entity state detection
|
|
|
|
* by potentially avoiding a database lookup if the distinction between NEW and DETACHED
|
|
|
|
* is either known or does not matter for the caller of the method.
|
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);
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->entityStates[$oid])) {
|
2010-07-04 20:55:49 +04:00
|
|
|
// State can only be NEW or DETACHED, because MANAGED/REMOVED states are known.
|
|
|
|
// Note that you can not remember the NEW or DETACHED state in _entityStates since
|
|
|
|
// the UoW does not hold references to such objects and the object hash can be reused.
|
|
|
|
// More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it.
|
2009-07-25 20:33:29 +04:00
|
|
|
if ($assume === null) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2010-07-04 20:55:49 +04:00
|
|
|
$id = $class->getIdentifierValues($entity);
|
|
|
|
if ( ! $id) {
|
|
|
|
return self::STATE_NEW;
|
|
|
|
} else if ($class->isIdentifierNatural()) {
|
|
|
|
// Check for a version field, if available, to avoid a db lookup.
|
|
|
|
if ($class->isVersioned) {
|
|
|
|
if ($class->getFieldValue($entity, $class->versionField)) {
|
|
|
|
return self::STATE_DETACHED;
|
|
|
|
} else {
|
|
|
|
return self::STATE_NEW;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Last try before db lookup: check the identity map.
|
|
|
|
if ($this->tryGetById($id, $class->rootEntityName)) {
|
|
|
|
return self::STATE_DETACHED;
|
|
|
|
} else {
|
|
|
|
// db lookup
|
|
|
|
if ($this->getEntityPersister(get_class($entity))->exists($entity)) {
|
|
|
|
return self::STATE_DETACHED;
|
|
|
|
} else {
|
|
|
|
return self::STATE_NEW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
} else {
|
2010-07-04 20:55:49 +04:00
|
|
|
return self::STATE_DETACHED;
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
2009-01-04 19:15:32 +03:00
|
|
|
} else {
|
2010-07-04 20:55:49 +04:00
|
|
|
return $assume;
|
2009-01-04 19:15:32 +03:00
|
|
|
}
|
|
|
|
}
|
2010-07-08 02:20:54 +04: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-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
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);
|
2010-07-08 02:20:54 +04:00
|
|
|
$classMetadata = $this->em->getClassMetadata(get_class($entity));
|
|
|
|
$idHash = implode(' ', $this->entityIdentifiers[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($idHash === '') {
|
2010-07-08 02:20:54 +04:00
|
|
|
throw new InvalidArgumentException("The given entity has no identity.");
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2009-05-14 14:03:09 +04:00
|
|
|
$className = $classMetadata->rootEntityName;
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->identityMap[$className][$idHash])) {
|
|
|
|
unset($this->identityMap[$className][$idHash]);
|
|
|
|
//$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
|
|
|
*
|
2009-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->identityMap[$rootClassName][$idHash];
|
2008-05-01 13:41:47 +04:00
|
|
|
}
|
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-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
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)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return isset($this->identityMap[$rootClassName][$idHash]) ?
|
|
|
|
$this->identityMap[$rootClassName][$idHash] : false;
|
2008-05-01 13:41:47 +04:00
|
|
|
}
|
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);
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->entityIdentifiers[$oid])) {
|
2009-01-03 22:50:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$classMetadata = $this->em->getClassMetadata(get_class($entity));
|
|
|
|
$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-11-11 19:20:29 +03:00
|
|
|
|
2010-07-08 02:20:54 +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.
|
|
|
|
*
|
2009-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
2008-05-17 16:22:24 +04:00
|
|
|
* @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
|
|
|
{
|
2010-07-08 02:20:54 +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();
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doPersist($entity, $visited);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-04 20:55:49 +04:00
|
|
|
* Persists an entity as part of the current unit of work.
|
2009-07-25 20:33:29 +04:00
|
|
|
*
|
2010-07-04 20:55:49 +04:00
|
|
|
* This method is internally called during persist() cascades as it tracks
|
|
|
|
* the already visited entities to prevent infinite recursions.
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +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
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
|
|
|
|
|
|
|
// We assume NEW, so DETACHED entities result in an exception on flush (constraint violation).
|
|
|
|
// If we would detect DETACHED here we would throw an exception anyway with the same
|
|
|
|
// consequences (not recoverable/programming error), so just assuming NEW here
|
|
|
|
// lets us avoid some database lookups for entities with natural identifiers.
|
|
|
|
$entityState = $this->getEntityState($entity, self::STATE_NEW);
|
2010-07-04 20:55:49 +04:00
|
|
|
|
2009-08-27 02:03:39 +04:00
|
|
|
switch ($entityState) {
|
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:
|
2010-07-04 20:55:49 +04:00
|
|
|
$this->persistNew($class, $entity);
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2009-07-25 20:33:29 +04:00
|
|
|
case self::STATE_REMOVED:
|
2009-05-11 14:43:27 +04:00
|
|
|
// Entity becomes managed again
|
2010-07-08 02:20:54 +04:00
|
|
|
unset($this->entityDeletions[$oid]);
|
|
|
|
$this->entityStates[$oid] = self::STATE_MANAGED;
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2010-07-08 02:20:54 +04:00
|
|
|
case self::STATE_DETACHED:
|
|
|
|
// Can actually not happen right now since we assume STATE_NEW.
|
|
|
|
throw new InvalidArgumentException("Detached entity passed to persist().");
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
2010-07-08 02:20:54 +04:00
|
|
|
throw new UnexpectedValueException("Unexpected entity state: $entityState.");
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2010-07-04 20:55:49 +04:00
|
|
|
|
2010-07-08 02:20:54 +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();
|
2010-07-08 02:20:54 +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
|
|
|
*/
|
2010-07-08 02:20:54 +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
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-08-27 02:03:39 +04:00
|
|
|
$entityState = $this->getEntityState($entity);
|
|
|
|
switch ($entityState) {
|
2008-12-18 17:08:11 +03:00
|
|
|
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
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->evm->hasListeners(Events::preRemove)) {
|
|
|
|
$this->evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($entity, $this->em));
|
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:
|
2010-07-08 02:20:54 +04:00
|
|
|
throw new InvalidArgumentException("A detached entity can not be removed.");
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
2010-07-08 02:20:54 +04:00
|
|
|
throw new UnexpectedValueException("Unexpected entity state: $entityState.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2009-08-27 02:03:39 +04:00
|
|
|
|
2010-07-08 02:20:54 +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.
|
2010-05-11 01:51:56 +04:00
|
|
|
* @throws OptimisticLockException If the entity uses optimistic locking through a version
|
|
|
|
* attribute and the version check against the managed copy fails.
|
2010-05-13 15:19:59 +04:00
|
|
|
*
|
|
|
|
* @todo Require active transaction!? OptimisticLockException may result in undefined state!?
|
2009-05-11 14:43:27 +04:00
|
|
|
*/
|
|
|
|
public function merge($entity)
|
|
|
|
{
|
|
|
|
$visited = array();
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->doMerge($entity, $visited);
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null)
|
2009-05-11 14:43:27 +04:00
|
|
|
{
|
2010-07-29 16:08:36 +04:00
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($visited[$oid])) {
|
|
|
|
return; // Prevent infinite recursion
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2010-05-11 01:51:56 +04:00
|
|
|
|
2010-07-30 00:27:00 +04:00
|
|
|
$visited[$oid] = $entity; // mark visited
|
|
|
|
|
2010-07-29 16:08:36 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
|
|
|
|
|
|
|
// First we assume DETACHED, although it can still be NEW but we can avoid
|
2010-07-30 00:27:00 +04:00
|
|
|
// an extra db-roundtrip this way. If it is not MANAGED but has an identity,
|
|
|
|
// we need to fetch it from the db anyway in order to merge.
|
2010-07-29 16:08:36 +04:00
|
|
|
// MANAGED entities are ignored by the merge operation.
|
2009-07-25 20:33:29 +04:00
|
|
|
if ($this->getEntityState($entity, self::STATE_DETACHED) == self::STATE_MANAGED) {
|
|
|
|
$managedCopy = $entity;
|
|
|
|
} else {
|
|
|
|
// Try to look the entity up in the identity map.
|
2010-07-29 16:08:36 +04:00
|
|
|
$id = $class->getIdentifierValues($entity);
|
|
|
|
|
|
|
|
// If there is no ID, it is actually NEW.
|
|
|
|
if ( ! $id) {
|
|
|
|
$managedCopy = $class->newInstance();
|
|
|
|
$this->persistNew($class, $managedCopy);
|
2009-07-25 20:33:29 +04:00
|
|
|
} else {
|
2010-07-29 16:08:36 +04:00
|
|
|
$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);
|
|
|
|
}
|
2010-04-10 02:00:36 +04:00
|
|
|
|
2010-07-29 16:08:36 +04:00
|
|
|
if ($managedCopy === null) {
|
|
|
|
// If the identifier is ASSIGNED, it is NEW, otherwise an error
|
|
|
|
// since the managed entity was not found.
|
|
|
|
if ($class->isIdentifierNatural()) {
|
|
|
|
$managedCopy = $class->newInstance();
|
|
|
|
$class->setIdentifierValues($managedCopy, $id);
|
|
|
|
$this->persistNew($class, $managedCopy);
|
|
|
|
} else {
|
|
|
|
throw new EntityNotFoundException;
|
|
|
|
}
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2010-04-10 02:00:36 +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.
|
2009-08-31 20:21:29 +04:00
|
|
|
if ($managedCopyVersion != $entityVersion) {
|
2010-04-11 18:43:33 +04:00
|
|
|
throw OptimisticLockException::lockFailedVersionMissmatch($entityVersion, $managedCopyVersion);
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
}
|
2010-04-10 02:00:36 +04:00
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
// Merge state of $entity into existing (managed) entity
|
|
|
|
foreach ($class->reflFields as $name => $prop) {
|
|
|
|
if ( ! isset($class->associationMappings[$name])) {
|
2010-08-07 13:33:33 +04:00
|
|
|
if ( ! $class->isIdentifier($name)) {
|
|
|
|
$prop->setValue($managedCopy, $prop->getValue($entity));
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
} else {
|
|
|
|
$assoc2 = $class->associationMappings[$name];
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc2['type'] & ClassMetadata::TO_ONE) {
|
2010-07-30 00:27:00 +04:00
|
|
|
$other = $prop->getValue($entity);
|
|
|
|
if ($other === null) {
|
|
|
|
$prop->setValue($managedCopy, null);
|
|
|
|
} else if ($other instanceof Proxy && !$other->__isInitialized__) {
|
|
|
|
// do not merge fields marked lazy that have not been fetched.
|
|
|
|
continue;
|
2010-08-09 15:13:21 +04:00
|
|
|
} else if ( ! $assoc2['isCascadeMerge']) {
|
2010-07-30 00:27:00 +04:00
|
|
|
if ($this->getEntityState($other, self::STATE_DETACHED) == self::STATE_MANAGED) {
|
|
|
|
$prop->setValue($managedCopy, $other);
|
|
|
|
} else {
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
|
2010-07-30 00:27:00 +04:00
|
|
|
$id = $targetClass->getIdentifierValues($other);
|
2010-08-09 15:13:21 +04:00
|
|
|
$proxy = $this->em->getProxyFactory()->getProxy($assoc2['targetEntity'], $id);
|
2010-07-30 00:27:00 +04:00
|
|
|
$prop->setValue($managedCopy, $proxy);
|
|
|
|
$this->registerManaged($proxy, $id, array());
|
2009-10-28 18:32:55 +03:00
|
|
|
}
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
} else {
|
2010-07-02 01:48:44 +04:00
|
|
|
$mergeCol = $prop->getValue($entity);
|
|
|
|
if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) {
|
2010-07-30 00:27:00 +04:00
|
|
|
// do not merge fields marked lazy that have not been fetched.
|
2010-07-02 01:48:44 +04:00
|
|
|
// keep the lazy persistent collection of the managed copy.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-08 18:07:40 +04:00
|
|
|
$managedCol = $prop->getValue($managedCopy);
|
|
|
|
if (!$managedCol) {
|
|
|
|
$managedCol = new PersistentCollection($this->em,
|
2010-08-09 15:13:21 +04:00
|
|
|
$this->em->getClassMetadata($assoc2['targetEntity']),
|
2010-08-08 18:07:40 +04:00
|
|
|
new ArrayCollection
|
|
|
|
);
|
|
|
|
$managedCol->setOwner($managedCopy, $assoc2);
|
|
|
|
$prop->setValue($managedCopy, $managedCol);
|
|
|
|
$this->originalEntityData[$oid][$name] = $managedCol;
|
|
|
|
}
|
2010-10-31 09:06:53 +03:00
|
|
|
if ($assoc2['isCascadeMerge']) {
|
|
|
|
$managedCol->initialize();
|
|
|
|
if (!$managedCol->isEmpty()) {
|
|
|
|
$managedCol->unwrap()->clear();
|
|
|
|
$managedCol->setDirty(true);
|
2010-10-31 09:23:58 +03:00
|
|
|
if ($assoc2['isOwningSide'] && $assoc2['type'] == ClassMetadata::MANY_TO_MANY && $class->isChangeTrackingNotify()) {
|
2010-10-31 13:11:15 +03:00
|
|
|
$this->scheduleForDirtyCheck($managedCopy);
|
2010-10-31 09:23:58 +03:00
|
|
|
}
|
2010-10-31 09:06:53 +03:00
|
|
|
}
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($class->isChangeTrackingNotify()) {
|
2010-07-30 19:18:25 +04:00
|
|
|
// Just treat all properties as changed, there is no other choice.
|
|
|
|
$this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-30 19:18:25 +04:00
|
|
|
if ($class->isChangeTrackingDeferredExplicit()) {
|
2010-07-29 16:08:36 +04:00
|
|
|
$this->scheduleForDirtyCheck($entity);
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prevManagedCopy !== null) {
|
2010-08-09 15:13:21 +04:00
|
|
|
$assocField = $assoc['fieldName'];
|
2010-07-08 02:20:54 +04:00
|
|
|
$prevClass = $this->em->getClassMetadata(get_class($prevManagedCopy));
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] & ClassMetadata::TO_ONE) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
|
2009-05-11 14:43:27 +04:00
|
|
|
} else {
|
2010-10-31 09:06:53 +03:00
|
|
|
$prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy);
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) {
|
|
|
|
$class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy);
|
2009-12-11 13:38:21 +03:00
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 00:27:00 +04:00
|
|
|
// Mark the managed copy visited as well
|
|
|
|
$visited[spl_object_hash($managedCopy)] = true;
|
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->cascadeMerge($entity, $managedCopy, $visited);
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
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();
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doDetach($entity, $visited);
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a detach operation on the given entity.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function doDetach($entity, array &$visited)
|
2009-07-25 20:33:29 +04:00
|
|
|
{
|
|
|
|
$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:
|
2010-12-10 23:55:48 +03:00
|
|
|
if ($this->isInIdentityMap($entity)) {
|
|
|
|
$this->removeFromIdentityMap($entity);
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
unset($this->entityInsertions[$oid], $this->entityUpdates[$oid],
|
|
|
|
$this->entityDeletions[$oid], $this->entityIdentifiers[$oid],
|
|
|
|
$this->entityStates[$oid], $this->originalEntityData[$oid]);
|
2009-07-25 20:33:29 +04:00
|
|
|
break;
|
|
|
|
case self::STATE_NEW:
|
|
|
|
case self::STATE_DETACHED:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->cascadeDetach($entity, $visited);
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
|
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();
|
2010-07-08 02:20:54 +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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function doRefresh($entity, array &$visited)
|
2009-07-21 19:53:58 +04:00
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
|
|
|
if (isset($visited[$oid])) {
|
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
|
|
|
$visited[$oid] = $entity; // mark visited
|
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-10-28 13:31:47 +03:00
|
|
|
if ($this->getEntityState($entity) == self::STATE_MANAGED) {
|
|
|
|
$this->getEntityPersister($class->name)->refresh(
|
2010-07-08 02:20:54 +04:00
|
|
|
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
|
2009-10-28 13:31:47 +03:00
|
|
|
$entity
|
|
|
|
);
|
|
|
|
} else {
|
2010-07-08 02:20:54 +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
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->cascadeRefresh($entity, $visited);
|
2009-07-21 19:53:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades a refresh operation to associated entities.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function cascadeRefresh($entity, array &$visited)
|
2009-07-21 19:53:58 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-11-13 22:34:10 +03:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! $assoc['isCascadeRefresh']) {
|
2009-07-21 19:53:58 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection) {
|
2009-11-13 22:34:10 +03:00
|
|
|
if ($relatedEntities instanceof PersistentCollection) {
|
|
|
|
// Unwrap so that foreach() does not initialize
|
|
|
|
$relatedEntities = $relatedEntities->unwrap();
|
|
|
|
}
|
2009-07-21 19:53:58 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doRefresh($relatedEntity, $visited);
|
2009-07-21 19:53:58 +04:00
|
|
|
}
|
|
|
|
} else if ($relatedEntities !== null) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doRefresh($relatedEntities, $visited);
|
2009-07-21 19:53:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades a detach operation to associated entities.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $visited
|
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function cascadeDetach($entity, array &$visited)
|
2009-07-25 20:33:29 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-11-13 22:34:10 +03:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! $assoc['isCascadeDetach']) {
|
2009-07-25 20:33:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection) {
|
2009-11-07 14:54:47 +03:00
|
|
|
if ($relatedEntities instanceof PersistentCollection) {
|
|
|
|
// Unwrap so that foreach() does not initialize
|
|
|
|
$relatedEntities = $relatedEntities->unwrap();
|
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doDetach($relatedEntity, $visited);
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
} else if ($relatedEntities !== null) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doDetach($relatedEntities, $visited);
|
2009-07-25 20:33:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function cascadeMerge($entity, $managedCopy, array &$visited)
|
2009-05-11 14:43:27 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-11-13 22:34:10 +03:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! $assoc['isCascadeMerge']) {
|
2009-05-11 14:43:27 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection) {
|
2009-11-07 14:54:47 +03:00
|
|
|
if ($relatedEntities instanceof PersistentCollection) {
|
|
|
|
// Unwrap so that foreach() does not initialize
|
|
|
|
$relatedEntities = $relatedEntities->unwrap();
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doMerge($relatedEntity, $visited, $managedCopy, $assoc);
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2009-07-02 15:48:44 +04:00
|
|
|
} else if ($relatedEntities !== null) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->doMerge($relatedEntities, $visited, $managedCopy, $assoc);
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function cascadePersist($entity, array &$visited)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-11-13 22:34:10 +03:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! $assoc['isCascadePersist']) {
|
2008-07-21 00:13:24 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if (($relatedEntities instanceof Collection || is_array($relatedEntities))) {
|
2009-11-07 14:54:47 +03:00
|
|
|
if ($relatedEntities instanceof PersistentCollection) {
|
|
|
|
// Unwrap so that foreach() does not initialize
|
|
|
|
$relatedEntities = $relatedEntities->unwrap();
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2010-07-08 02:20:54 +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) {
|
2010-07-08 02:20:54 +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
|
|
|
*/
|
2010-07-08 02:20:54 +04:00
|
|
|
private function cascadeRemove($entity, array &$visited)
|
2008-07-10 21:17:58 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-11-13 22:34:10 +03:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! $assoc['isCascadeRemove']) {
|
2009-01-04 19:15:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-15 20:19:00 +03:00
|
|
|
//TODO: If $entity instanceof Proxy => Initialize ?
|
2010-08-09 15:13:21 +04:00
|
|
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
|
2009-07-29 16:00:08 +04:00
|
|
|
if ($relatedEntities instanceof Collection || is_array($relatedEntities)) {
|
2009-11-13 22:34:10 +03:00
|
|
|
// If its a PersistentCollection initialization is intended! No unwrap!
|
2009-01-04 19:15:32 +03:00
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
2010-07-08 02:20:54 +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) {
|
2010-07-08 02:20:54 +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
|
|
|
|
2010-04-09 00:50:06 +04:00
|
|
|
/**
|
|
|
|
* Acquire a lock on the given entity.
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param int $lockMode
|
|
|
|
* @param int $lockVersion
|
|
|
|
*/
|
|
|
|
public function lock($entity, $lockMode, $lockVersion = null)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->getEntityState($entity) != self::STATE_MANAGED) {
|
|
|
|
throw new InvalidArgumentException("Entity is not MANAGED.");
|
2010-04-11 18:43:33 +04:00
|
|
|
}
|
|
|
|
|
2010-04-09 00:50:06 +04:00
|
|
|
$entityName = get_class($entity);
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata($entityName);
|
2010-04-09 00:50:06 +04:00
|
|
|
|
2010-05-15 13:48:20 +04:00
|
|
|
if ($lockMode == \Doctrine\DBAL\LockMode::OPTIMISTIC) {
|
2010-04-09 00:50:06 +04:00
|
|
|
if (!$class->isVersioned) {
|
|
|
|
throw OptimisticLockException::notVersioned($entityName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lockVersion != null) {
|
|
|
|
$entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
|
|
|
|
if ($entityVersion != $lockVersion) {
|
2010-05-15 11:58:39 +04:00
|
|
|
throw OptimisticLockException::lockFailedVersionMissmatch($entity, $lockVersion, $entityVersion);
|
2010-04-09 00:50:06 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-15 13:48:20 +04:00
|
|
|
} else if (in_array($lockMode, array(\Doctrine\DBAL\LockMode::PESSIMISTIC_READ, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE))) {
|
2010-04-09 00:50:06 +04:00
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
if (!$this->em->getConnection()->isTransactionActive()) {
|
2010-04-09 00:50:06 +04:00
|
|
|
throw TransactionRequiredException::transactionRequired();
|
|
|
|
}
|
|
|
|
|
2010-04-11 18:43:33 +04:00
|
|
|
$oid = spl_object_hash($entity);
|
2010-04-09 00:50:06 +04:00
|
|
|
|
2010-04-11 18:43:33 +04:00
|
|
|
$this->getEntityPersister($class->name)->lock(
|
2010-07-10 21:23:38 +04:00
|
|
|
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
|
2010-04-11 18:43:33 +04:00
|
|
|
$lockMode
|
|
|
|
);
|
2010-04-09 00:50:06 +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()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->commitOrderCalculator === null) {
|
|
|
|
$this->commitOrderCalculator = new Internal\CommitOrderCalculator;
|
2009-12-21 14:06:27 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->commitOrderCalculator;
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->identityMap =
|
|
|
|
$this->entityIdentifiers =
|
|
|
|
$this->originalEntityData =
|
|
|
|
$this->entityChangeSets =
|
|
|
|
$this->entityStates =
|
|
|
|
$this->scheduledForDirtyCheck =
|
|
|
|
$this->entityInsertions =
|
|
|
|
$this->entityUpdates =
|
|
|
|
$this->entityDeletions =
|
|
|
|
$this->collectionDeletions =
|
|
|
|
$this->collectionUpdates =
|
|
|
|
$this->extraUpdates =
|
|
|
|
$this->orphanRemovals = array();
|
|
|
|
if ($this->commitOrderCalculator !== null) {
|
|
|
|
$this->commitOrderCalculator->clear();
|
2009-12-21 14:06:27 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
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.
|
|
|
|
*
|
2009-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
2009-07-23 13:52:16 +04:00
|
|
|
* @param object $entity
|
|
|
|
*/
|
|
|
|
public function scheduleOrphanRemoval($entity)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->orphanRemovals[spl_object_hash($entity)] = $entity;
|
2009-07-23 13:52:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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?
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->collectionDeletions[] = $coll;
|
2008-08-16 23:40:59 +04:00
|
|
|
}
|
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
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return in_array($coll, $this->collectionsDeletions, true);
|
2008-08-16 23:40:59 +04:00
|
|
|
}
|
2009-07-03 21:36:41 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
2009-05-13 19:19:27 +04:00
|
|
|
* INTERNAL:
|
2010-08-08 15:10:53 +04:00
|
|
|
* Creates an entity. Used for reconstitution of persistent entities.
|
2008-07-10 21:17:58 +04:00
|
|
|
*
|
2009-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
2009-12-16 00:06:32 +03:00
|
|
|
* @param string $className The name of the entity class.
|
|
|
|
* @param array $data The data for the entity.
|
|
|
|
* @param array $hints Any hints to account for during reconstitution/lookup of the entity.
|
2010-08-08 15:10:53 +04:00
|
|
|
* @return object The managed entity instance.
|
2009-10-09 18:27:35 +04:00
|
|
|
* @internal Highly performance-sensitive method.
|
2009-12-16 00:06:32 +03:00
|
|
|
*
|
2009-10-28 13:31:47 +03:00
|
|
|
* @todo Rename: getOrCreateEntity
|
2008-07-10 21:17:58 +04:00
|
|
|
*/
|
2009-10-22 16:50:58 +04:00
|
|
|
public function createEntity($className, array $data, &$hints = array())
|
2008-12-18 17:08:11 +03:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata($className);
|
2009-12-16 00:06:32 +03:00
|
|
|
//$isReadOnly = isset($hints[Query::HINT_READ_ONLY]);
|
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) {
|
2010-08-07 21:33:54 +04:00
|
|
|
if (isset($class->associationMappings[$fieldName])) {
|
2010-08-14 01:23:11 +04:00
|
|
|
$id[$fieldName] = $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']];
|
2010-08-07 21:33:54 +04:00
|
|
|
} else {
|
|
|
|
$id[$fieldName] = $data[$fieldName];
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
$idHash = implode(' ', $id);
|
2009-01-03 22:50:13 +03:00
|
|
|
} else {
|
2010-08-07 22:07:10 +04:00
|
|
|
if (isset($class->associationMappings[$class->identifier[0]])) {
|
2010-08-14 01:23:11 +04:00
|
|
|
$idHash = $data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']];
|
2010-08-07 22:07:10 +04:00
|
|
|
} else {
|
|
|
|
$idHash = $data[$class->identifier[0]];
|
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
$id = array($class->identifier[0] => $idHash);
|
2009-01-03 22:50:13 +03:00
|
|
|
}
|
2009-06-22 22:48:42 +04:00
|
|
|
|
2010-07-08 02:20:54 +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);
|
2010-01-05 14:45:38 +03:00
|
|
|
if ($entity instanceof Proxy && ! $entity->__isInitialized__) {
|
|
|
|
$entity->__isInitialized__ = true;
|
|
|
|
$overrideLocalValues = true;
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid] = $data;
|
2010-05-15 23:32:34 +04:00
|
|
|
if ($entity instanceof NotifyPropertyChanged) {
|
|
|
|
$entity->addPropertyChangedListener($this);
|
|
|
|
}
|
2010-01-05 14:45:38 +03:00
|
|
|
} else {
|
|
|
|
$overrideLocalValues = isset($hints[Query::HINT_REFRESH]);
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
} else {
|
2010-02-10 13:47:42 +03:00
|
|
|
$entity = $class->newInstance();
|
2009-01-03 22:50:13 +03:00
|
|
|
$oid = spl_object_hash($entity);
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityIdentifiers[$oid] = $id;
|
|
|
|
$this->entityStates[$oid] = self::STATE_MANAGED;
|
|
|
|
$this->originalEntityData[$oid] = $data;
|
|
|
|
$this->identityMap[$class->rootEntityName][$idHash] = $entity;
|
2009-10-07 16:39:46 +04:00
|
|
|
if ($entity instanceof NotifyPropertyChanged) {
|
2009-07-18 15:41:37 +04:00
|
|
|
$entity->addPropertyChangedListener($this);
|
|
|
|
}
|
2009-10-01 16:00:14 +04:00
|
|
|
$overrideLocalValues = true;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
2009-10-01 16:00:14 +04:00
|
|
|
if ($overrideLocalValues) {
|
2010-08-08 14:52:46 +04:00
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
if (isset($class->fieldMappings[$field])) {
|
|
|
|
$class->reflFields[$field]->setValue($entity, $value);
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-10-22 16:50:58 +04:00
|
|
|
|
|
|
|
// Properly initialize any unfetched associations, if partial objects are not allowed.
|
|
|
|
if ( ! isset($hints[Query::HINT_FORCE_PARTIAL_LOAD])) {
|
|
|
|
foreach ($class->associationMappings as $field => $assoc) {
|
2009-10-28 13:31:47 +03:00
|
|
|
// Check if the association is not among the fetch-joined associations already.
|
2010-01-30 00:24:29 +03:00
|
|
|
if (isset($hints['fetched'][$className][$field])) {
|
2009-10-28 18:32:55 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-30 00:24:29 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
|
2009-11-07 14:54:47 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] & ClassMetadata::TO_ONE) {
|
|
|
|
if ($assoc['isOwningSide']) {
|
2009-10-28 18:32:55 +03:00
|
|
|
$associatedId = array();
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) {
|
2010-09-13 23:48:25 +04:00
|
|
|
$joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null;
|
2009-10-28 18:32:55 +03:00
|
|
|
if ($joinColumnValue !== null) {
|
2011-01-01 20:17:19 +03:00
|
|
|
if ($targetClass->containsForeignIdentifier) {
|
|
|
|
$associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue;
|
|
|
|
} else {
|
|
|
|
$associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue;
|
|
|
|
}
|
2009-10-28 13:31:47 +03:00
|
|
|
}
|
2009-10-28 18:32:55 +03:00
|
|
|
}
|
|
|
|
if ( ! $associatedId) {
|
2009-12-16 00:06:32 +03:00
|
|
|
// Foreign key is NULL
|
2009-10-28 18:32:55 +03:00
|
|
|
$class->reflFields[$field]->setValue($entity, null);
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid][$field] = null;
|
2009-10-28 18:32:55 +03:00
|
|
|
} else {
|
2009-12-16 00:06:32 +03:00
|
|
|
// Foreign key is set
|
2009-10-28 18:32:55 +03:00
|
|
|
// Check identity map first
|
|
|
|
// FIXME: Can break easily with composite keys if join column values are in
|
|
|
|
// wrong order. The correct order is the one in ClassMetadata#identifier.
|
|
|
|
$relatedIdHash = implode(' ', $associatedId);
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->identityMap[$targetClass->rootEntityName][$relatedIdHash])) {
|
|
|
|
$newValue = $this->identityMap[$targetClass->rootEntityName][$relatedIdHash];
|
2011-03-09 00:22:54 +03:00
|
|
|
|
|
|
|
// if this is an uninitialized proxy, we are deferring eager loads,
|
|
|
|
// this association is marked as eager fetch, and its an uninitialized proxy (wtf!)
|
|
|
|
// then we cann append this entity for eager loading!
|
|
|
|
if (isset($hints['fetchEager'][$class->name][$field]) &&
|
|
|
|
isset($hints['deferEagerLoad']) &&
|
|
|
|
!$targetClass->isIdentifierComposite &&
|
|
|
|
$newValue instanceof Proxy &&
|
|
|
|
$newValue->__isInitialized__ === false) {
|
|
|
|
|
|
|
|
$this->eagerLoadingEntities[$assoc['targetEntity']][] = $relatedIdHash;
|
|
|
|
}
|
2009-10-28 13:31:47 +03:00
|
|
|
} else {
|
2009-11-03 21:30:21 +03:00
|
|
|
if ($targetClass->subClasses) {
|
|
|
|
// If it might be a subtype, it can not be lazy
|
2010-08-09 15:13:21 +04:00
|
|
|
$newValue = $this->getEntityPersister($assoc['targetEntity'])
|
|
|
|
->loadOneToOneEntity($assoc, $entity, null, $associatedId);
|
2009-11-03 21:30:21 +03:00
|
|
|
} else {
|
2010-12-31 15:11:01 +03:00
|
|
|
// Deferred eager load only works for single identifier classes
|
2011-03-09 00:22:54 +03:00
|
|
|
if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER || isset($hints['eagerFetch'][$class->name][$field])) {
|
2010-12-31 15:11:01 +03:00
|
|
|
if (isset($hints['deferEagerLoad']) && !$targetClass->isIdentifierComposite) {
|
|
|
|
// TODO: Is there a faster approach?
|
|
|
|
$this->eagerLoadingEntities[$assoc['targetEntity']][] = current($id);
|
|
|
|
|
|
|
|
$newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId);
|
|
|
|
} else {
|
|
|
|
// TODO: This is very imperformant, ignore it?
|
|
|
|
$newValue = $this->em->find($assoc['targetEntity'], $associatedId);
|
2010-12-31 12:54:20 +03:00
|
|
|
}
|
2010-08-08 13:05:30 +04:00
|
|
|
} else {
|
2010-08-09 15:13:21 +04:00
|
|
|
$newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId);
|
2010-08-08 13:05:30 +04:00
|
|
|
}
|
2010-03-01 01:48:50 +03:00
|
|
|
// PERF: Inlined & optimized code from UnitOfWork#registerManaged()
|
|
|
|
$newValueOid = spl_object_hash($newValue);
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityIdentifiers[$newValueOid] = $associatedId;
|
|
|
|
$this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue;
|
|
|
|
$this->entityStates[$newValueOid] = self::STATE_MANAGED;
|
|
|
|
// make sure that when an proxy is then finally loaded, $this->originalEntityData is set also!
|
2009-11-03 21:30:21 +03:00
|
|
|
}
|
2009-10-22 16:50:58 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid][$field] = $newValue;
|
2009-10-28 18:32:55 +03:00
|
|
|
$class->reflFields[$field]->setValue($entity, $newValue);
|
2011-03-06 23:26:54 +03:00
|
|
|
|
|
|
|
if ($assoc['inversedBy'] && $assoc['type'] & ClassMetadata::ONE_TO_ONE) {
|
|
|
|
$inverseAssoc = $targetClass->associationMappings[$assoc['inversedBy']];
|
|
|
|
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($newValue, $entity);
|
|
|
|
}
|
2009-10-22 16:50:58 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-12-16 00:06:32 +03:00
|
|
|
// Inverse side of x-to-one can never be lazy
|
2010-08-09 15:13:21 +04:00
|
|
|
$class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])
|
|
|
|
->loadOneToOneEntity($assoc, $entity, null));
|
2009-10-28 18:32:55 +03:00
|
|
|
}
|
|
|
|
} else {
|
2011-03-06 23:49:02 +03:00
|
|
|
// Inject collection
|
|
|
|
$pColl = new PersistentCollection($this->em, $targetClass, new ArrayCollection);
|
|
|
|
$pColl->setOwner($entity, $assoc);
|
2011-03-06 23:26:54 +03:00
|
|
|
|
2011-03-06 23:49:02 +03:00
|
|
|
$reflField = $class->reflFields[$field];
|
|
|
|
$reflField->setValue($entity, $pColl);
|
2011-03-06 23:26:54 +03:00
|
|
|
|
2011-03-06 23:49:02 +03:00
|
|
|
if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER) {
|
|
|
|
$this->loadCollection($pColl);
|
|
|
|
$pColl->takeSnapshot();
|
|
|
|
} else {
|
|
|
|
$pColl->setInitialized(false);
|
2009-10-22 16:50:58 +04:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid][$field] = $pColl;
|
2009-10-22 16:50:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
2009-07-18 15:41:37 +04:00
|
|
|
|
2009-10-22 16:50:58 +04:00
|
|
|
//TODO: These should be invoked later, after hydration, because associations may not yet be loaded here.
|
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);
|
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
if ($this->evm->hasListeners(Events::postLoad)) {
|
|
|
|
$this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->em));
|
2009-07-18 15:41:37 +04:00
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
return $entity;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
|
2010-08-09 23:44:52 +04:00
|
|
|
/**
|
2010-12-31 12:54:20 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function triggerEagerLoads()
|
|
|
|
{
|
|
|
|
if (!$this->eagerLoadingEntities) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-06 13:15:56 +03:00
|
|
|
// avoid infinite recursion
|
2010-12-31 12:54:20 +03:00
|
|
|
$eagerLoadingEntities = $this->eagerLoadingEntities;
|
|
|
|
$this->eagerLoadingEntities = array();
|
|
|
|
|
|
|
|
foreach ($eagerLoadingEntities AS $entityName => $ids) {
|
2010-12-31 15:11:01 +03:00
|
|
|
$class = $this->em->getClassMetadata($entityName);
|
|
|
|
$this->getEntityPersister($entityName)->loadAll(array_combine($class->identifier, array($ids)));
|
2010-12-31 12:54:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-10 00:27:34 +04:00
|
|
|
* Initializes (loads) an uninitialized persistent collection of an entity.
|
2010-08-09 23:44:52 +04:00
|
|
|
*
|
2010-08-10 00:27:34 +04:00
|
|
|
* @param PeristentCollection $collection The collection to initialize.
|
2010-08-09 23:44:52 +04:00
|
|
|
* @todo Maybe later move to EntityManager#initialize($proxyOrCollection). See DDC-733.
|
|
|
|
*/
|
2010-08-10 00:27:34 +04:00
|
|
|
public function loadCollection(PersistentCollection $collection)
|
2010-08-09 15:13:21 +04:00
|
|
|
{
|
2010-08-10 00:27:34 +04:00
|
|
|
$assoc = $collection->getMapping();
|
2010-08-09 15:13:21 +04:00
|
|
|
switch ($assoc['type']) {
|
|
|
|
case ClassMetadata::ONE_TO_MANY:
|
|
|
|
$this->getEntityPersister($assoc['targetEntity'])->loadOneToManyCollection(
|
2010-08-10 00:27:34 +04:00
|
|
|
$assoc, $collection->getOwner(), $collection);
|
2010-08-09 15:13:21 +04:00
|
|
|
break;
|
|
|
|
case ClassMetadata::MANY_TO_MANY:
|
|
|
|
$this->getEntityPersister($assoc['targetEntity'])->loadManyToManyCollection(
|
2010-08-10 00:27:34 +04:00
|
|
|
$assoc, $collection->getOwner(), $collection);
|
2010-08-09 15:13:21 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
/**
|
|
|
|
* Gets the identity map of the UnitOfWork.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIdentityMap()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->identityMap;
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
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);
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->originalEntityData[$oid])) {
|
|
|
|
return $this->originalEntityData[$oid];
|
2009-01-03 22:50:13 +03:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
2009-10-28 13:31:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
public function setOriginalEntityData($entity, array $data)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[spl_object_hash($entity)] = $data;
|
2009-10-28 13:31:47 +03:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2009-10-07 16:39:46 +04:00
|
|
|
* @ignore
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param string $oid
|
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setOriginalEntityProperty($oid, $property, $value)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->originalEntityData[$oid][$property] = $value;
|
2009-01-03 22:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->entityIdentifiers[spl_object_hash($entity)];
|
2009-01-03 22:50:13 +03:00
|
|
|
}
|
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-08-13 14:13:06 +04:00
|
|
|
$idHash = implode(' ', (array) $id);
|
2010-07-08 02:20:54 +04:00
|
|
|
if (isset($this->identityMap[$rootClassName][$idHash])) {
|
|
|
|
return $this->identityMap[$rootClassName][$idHash];
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
|
|
|
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.
|
2010-07-08 02:20:54 +04:00
|
|
|
* @todo Rename: scheduleForSynchronization
|
2009-04-09 22:12:48 +04:00
|
|
|
*/
|
2009-01-29 20:00:44 +03:00
|
|
|
public function scheduleForDirtyCheck($entity)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
$rootClassName = $this->em->getClassMetadata(get_class($entity))->rootEntityName;
|
2010-07-15 17:52:42 +04:00
|
|
|
$this->scheduledForDirtyCheck[$rootClassName][spl_object_hash($entity)] = $entity;
|
2009-01-29 20:00:44 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return ! empty($this->entityInsertions);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
|
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;
|
2010-07-08 02:20:54 +04:00
|
|
|
foreach ($this->identityMap as $entitySet) {
|
2009-07-15 10:46:43 +04:00
|
|
|
$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.
|
2010-12-04 21:44:10 +03:00
|
|
|
* @return Doctrine\ORM\Persisters\AbstractEntityPersister
|
2009-02-02 14:55:50 +03:00
|
|
|
*/
|
|
|
|
public function getEntityPersister($entityName)
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->persisters[$entityName])) {
|
|
|
|
$class = $this->em->getClassMetadata($entityName);
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($class->isInheritanceTypeNone()) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$persister = new Persisters\BasicEntityPersister($this->em, $class);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($class->isInheritanceTypeSingleTable()) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$persister = new Persisters\SingleTablePersister($this->em, $class);
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($class->isInheritanceTypeJoined()) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$persister = new Persisters\JoinedSubclassPersister($this->em, $class);
|
2009-02-02 14:55:50 +03:00
|
|
|
} else {
|
2010-07-08 02:20:54 +04:00
|
|
|
$persister = new Persisters\UnionSubclassPersister($this->em, $class);
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->persisters[$entityName] = $persister;
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->persisters[$entityName];
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
/**
|
|
|
|
* Gets a collection persister for a collection-valued association.
|
|
|
|
*
|
|
|
|
* @param AssociationMapping $association
|
|
|
|
* @return AbstractCollectionPersister
|
|
|
|
*/
|
2010-08-09 15:13:21 +04:00
|
|
|
public function getCollectionPersister(array $association)
|
2009-02-02 14:55:50 +03:00
|
|
|
{
|
2010-08-09 15:13:21 +04:00
|
|
|
$type = $association['type'];
|
2010-07-08 02:20:54 +04:00
|
|
|
if ( ! isset($this->collectionPersisters[$type])) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($type == ClassMetadata::ONE_TO_MANY) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$persister = new Persisters\OneToManyPersister($this->em);
|
2010-08-09 15:13:21 +04:00
|
|
|
} else if ($type == ClassMetadata::MANY_TO_MANY) {
|
2010-07-08 02:20:54 +04:00
|
|
|
$persister = new Persisters\ManyToManyPersister($this->em);
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->collectionPersisters[$type] = $persister;
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->collectionPersisters[$type];
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2009-10-07 16:39:46 +04:00
|
|
|
public function registerManaged($entity, array $id, array $data)
|
2009-05-13 19:19:27 +04:00
|
|
|
{
|
|
|
|
$oid = spl_object_hash($entity);
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityIdentifiers[$oid] = $id;
|
|
|
|
$this->entityStates[$oid] = self::STATE_MANAGED;
|
|
|
|
$this->originalEntityData[$oid] = $data;
|
2009-05-13 19:19:27 +04:00
|
|
|
$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)
|
|
|
|
{
|
2010-07-08 02:20:54 +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);
|
2010-07-08 02:20:54 +04:00
|
|
|
$class = $this->em->getClassMetadata(get_class($entity));
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2010-05-01 14:14:16 +04:00
|
|
|
$isAssocField = isset($class->associationMappings[$propertyName]);
|
|
|
|
|
|
|
|
if ( ! $isAssocField && ! isset($class->fieldMappings[$propertyName])) {
|
|
|
|
return; // ignore non-persistent fields
|
|
|
|
}
|
|
|
|
|
2010-07-15 17:52:42 +04:00
|
|
|
// Update changeset and mark entity for synchronization
|
2010-07-08 02:20:54 +04:00
|
|
|
$this->entityChangeSets[$oid][$propertyName] = array($oldValue, $newValue);
|
2010-07-15 17:52:42 +04:00
|
|
|
if ( ! isset($this->scheduledForDirtyCheck[$class->rootEntityName][$oid])) {
|
|
|
|
$this->scheduleForDirtyCheck($entity);
|
2009-07-23 13:52:16 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-07-15 17:52:42 +04:00
|
|
|
|
2010-02-24 22:19:04 +03:00
|
|
|
/**
|
|
|
|
* Gets the currently scheduled entity insertions in this UnitOfWork.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getScheduledEntityInsertions()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->entityInsertions;
|
2010-02-24 22:19:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currently scheduled entity updates in this UnitOfWork.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getScheduledEntityUpdates()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->entityUpdates;
|
2010-02-24 22:19:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currently scheduled entity deletions in this UnitOfWork.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getScheduledEntityDeletions()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->entityDeletions;
|
2010-02-24 22:19:04 +03:00
|
|
|
}
|
2010-03-13 12:19:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the currently scheduled complete collection deletions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getScheduledCollectionDeletions()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->collectionDeletions;
|
2010-03-13 12:19:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currently scheduled collection inserts, updates and deletes.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getScheduledCollectionUpdates()
|
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return $this->collectionUpdates;
|
2010-07-04 20:55:49 +04:00
|
|
|
}
|
|
|
|
|
2010-07-08 02:20:54 +04:00
|
|
|
private static function objToStr($obj)
|
2010-07-04 20:55:49 +04:00
|
|
|
{
|
2010-07-08 02:20:54 +04:00
|
|
|
return method_exists($obj, '__toString') ? (string)$obj : get_class($obj).'@'.spl_object_hash($obj);
|
2010-07-04 20:55:49 +04:00
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|