2007-10-23 01:47:05 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
|
2008-05-30 16:09:24 +04:00
|
|
|
#namespace Doctrine::ORM::Internal;
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
#use Doctrine::ORM::Entity;
|
|
|
|
#use Doctrine::ORM::EntityManager;
|
|
|
|
#use Doctrine::ORM::Exceptions::UnitOfWorkException;
|
|
|
|
|
2007-10-23 01:47:05 +04:00
|
|
|
/**
|
2008-07-04 20:32:19 +04:00
|
|
|
* The UnitOfWork is responsible for tracking changes to objects during an
|
2008-07-21 00:13:24 +04:00
|
|
|
* "object-level" transaction and for writing out changes to the database
|
2008-07-04 20:32:19 +04:00
|
|
|
* in the correct order.
|
2007-10-23 01:47:05 +04:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2008-05-01 13:41:47 +04:00
|
|
|
* @since 2.0
|
2007-10-23 01:47:05 +04:00
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2008-02-24 01:04:39 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2008-07-21 00:13:24 +04:00
|
|
|
* @todo Rename: Doctrine::ORM::(Internal::)UnitOfWork.
|
|
|
|
* @todo Turn connection exceptions into UnitOfWorkExceptions.
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2008-07-04 20:32:19 +04:00
|
|
|
class Doctrine_Connection_UnitOfWork
|
2008-07-21 00:13:24 +04: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
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
protected $_identityMap = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* A list of all new entities that need to be INSERTed.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @todo Index by class name.
|
|
|
|
* @todo Rename to _inserts?
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2008-04-13 00:11:11 +04:00
|
|
|
protected $_newEntities = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2008-04-13 00:11:11 +04:00
|
|
|
* A list of all dirty entities.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @todo Rename to _updates?
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2008-04-13 00:11:11 +04:00
|
|
|
protected $_dirtyEntities = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* A list of all deleted entities.
|
|
|
|
* Removed entities are entities that are "scheduled for removal" but have
|
|
|
|
* not yet been removed from the database.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @todo Rename to _deletions?
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
protected $_deletedEntities = array();
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All collection deletions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_collectionDeletions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All collection creations.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_collectionCreations = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All collection updates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_collectionUpdates = array();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* The EntityManager the UnitOfWork belongs to.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @var Doctrine::ORM::EntityManager
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
|
|
|
protected $_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-07-21 00:13:24 +04:00
|
|
|
* @var Doctrine::ORM::Internal::CommitOrderCalculator
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
2008-06-05 23:01:58 +04:00
|
|
|
protected $_commitOrderCalculator;
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-04 20:32:19 +04:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2008-07-10 21:17:58 +04:00
|
|
|
* Creates a new UnitOfWork.
|
2008-07-04 20:32:19 +04:00
|
|
|
*
|
|
|
|
* @param Doctrine_EntityManager $em
|
|
|
|
*/
|
|
|
|
public function __construct(Doctrine_EntityManager $em)
|
|
|
|
{
|
|
|
|
$this->_em = $em;
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: any benefit with lazy init?
|
|
|
|
$this->_commitOrderCalculator = new Doctrine_Internal_CommitOrderCalculator();
|
2008-07-04 20:32:19 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
2008-04-13 00:11:11 +04:00
|
|
|
* Commits the unit of work, executing all operations that have been postponed
|
2008-02-24 01:04:39 +03:00
|
|
|
* up to this point.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2008-07-10 21:17:58 +04:00
|
|
|
* @todo Impl
|
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
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
// Detect changes in managed entities (mark dirty)
|
2008-08-16 23:40:59 +04:00
|
|
|
//TODO: Consider using registerDirty() in Entity#_set() instead if its
|
|
|
|
// more performant (SEE THERE).
|
|
|
|
/*foreach ($this->_identityMap as $entities) {
|
2008-07-21 00:13:24 +04:00
|
|
|
foreach ($entities as $entity) {
|
|
|
|
if ($entity->_state() == Doctrine_Entity::STATE_MANAGED
|
|
|
|
&& $entity->isModified()) {
|
|
|
|
$this->registerDirty($entity);
|
|
|
|
}
|
|
|
|
}
|
2008-08-16 23:40:59 +04:00
|
|
|
}*/
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
if (empty($this->_newEntities) &&
|
|
|
|
empty($this->_deletedEntities) &&
|
|
|
|
empty($this->_dirtyEntities)) {
|
|
|
|
return; // Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we need a commit order to maintain referential integrity
|
|
|
|
$commitOrder = $this->_getCommitOrder();
|
|
|
|
|
|
|
|
//TODO: begin transaction here?
|
|
|
|
|
|
|
|
foreach ($commitOrder as $class) {
|
|
|
|
$this->_executeInserts($class);
|
2008-08-22 13:05:14 +04:00
|
|
|
}
|
|
|
|
foreach ($commitOrder as $class) {
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_executeUpdates($class);
|
|
|
|
}
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
//TODO: collection deletions
|
|
|
|
//TODO: collection updates (deleteRows, updateRows, insertRows)
|
|
|
|
//TODO: collection recreations
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
// Entity deletions come last and need to be in reverse commit order
|
2008-07-21 00:13:24 +04:00
|
|
|
for ($count = count($commitOrder), $i = $count - 1; $i >= 0; $i--) {
|
|
|
|
$this->_executeDeletions($commitOrder[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: commit transaction here?
|
|
|
|
|
|
|
|
// clear lists
|
|
|
|
$this->_newEntities = array();
|
|
|
|
$this->_dirtyEntities = array();
|
|
|
|
$this->_deletedEntities = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
private function _executeInserts($class)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: Maybe $persister->addInsert($entity) in the loop and
|
|
|
|
// $persister->executeInserts() at the end to allow easy prepared
|
|
|
|
// statement reuse and maybe bulk operations in the persister.
|
|
|
|
// Same for update/delete.
|
|
|
|
$className = $class->getClassName();
|
|
|
|
$persister = $this->_em->getEntityPersister($className);
|
|
|
|
foreach ($this->_newEntities as $entity) {
|
|
|
|
if ($entity->getClass()->getClassName() == $className) {
|
|
|
|
$persister->insert($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
private function _executeUpdates($class)
|
|
|
|
{
|
|
|
|
$className = $class->getClassName();
|
|
|
|
$persister = $this->_em->getEntityPersister($className);
|
|
|
|
foreach ($this->_dirtyEntities as $entity) {
|
|
|
|
if ($entity->getClass()->getClassName() == $className) {
|
|
|
|
$persister->update($entity);
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
private function _executeDeletions($class)
|
|
|
|
{
|
|
|
|
$className = $class->getClassName();
|
|
|
|
$persister = $this->_em->getEntityPersister($className);
|
|
|
|
foreach ($this->_deletedEntities as $entity) {
|
|
|
|
if ($entity->getClass()->getClassName() == $className) {
|
|
|
|
$persister->delete($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the commit order.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function _getCommitOrder()
|
|
|
|
{
|
|
|
|
//TODO: Once these 3 arrays are indexed by classname we can do this:
|
|
|
|
// Either way... do we need to care about duplicates?
|
|
|
|
/*$classesInChangeSet = array_merge(
|
|
|
|
array_keys($this->_newEntities),
|
|
|
|
array_keys($this->_dirtyEntities),
|
|
|
|
array_keys($this->_deletedEntities)
|
|
|
|
);*/
|
|
|
|
|
|
|
|
$entityChangeSet = array_merge($this->_newEntities, $this->_dirtyEntities, $this->_deletedEntities);
|
|
|
|
|
|
|
|
/* if (count($entityChangeSet) == 1) {
|
|
|
|
* return array($entityChangeSet[0]->getClass());
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
// See if there are any new classes in the changeset, that are not in the
|
|
|
|
// commit order graph yet (dont have a node).
|
|
|
|
$newNodes = array();
|
|
|
|
foreach ($entityChangeSet as $entity) {
|
|
|
|
if ( ! $this->_commitOrderCalculator->hasNodeWithKey($entity->getClass()->getClassName())) {
|
|
|
|
$this->_commitOrderCalculator->addNodeWithItem(
|
|
|
|
$entity->getClass()->getClassName(), // index/key
|
|
|
|
$entity->getClass() // item
|
|
|
|
);
|
|
|
|
$newNodes[] = $this->_commitOrderCalculator->getNodeForKey($entity->getClass()->getClassName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate dependencies for new nodes
|
|
|
|
foreach ($newNodes as $node) {
|
|
|
|
foreach ($node->getClass()->getAssociationMappings() as $assocMapping) {
|
|
|
|
//TODO: should skip target classes that are not in the changeset.
|
|
|
|
if ($assocMapping->isOwningSide()) {
|
2008-08-16 23:40:59 +04:00
|
|
|
$targetClass = $this->_em->getClassMetadata($assocMapping->getTargetEntityName());
|
2008-07-21 00:13:24 +04:00
|
|
|
$targetClassName = $targetClass->getClassName();
|
|
|
|
// if the target class does not yet have a node, create it
|
|
|
|
if ( ! $this->_commitOrderCalculator->hasNodeWithKey($targetClassName)) {
|
|
|
|
$this->_commitOrderCalculator->addNodeWithItem(
|
|
|
|
$targetClassName, // index/key
|
|
|
|
$targetClass // item
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// add dependency
|
|
|
|
$otherNode = $this->_commitOrderCalculator->getNodeForKey($targetClassName);
|
2008-08-22 13:05:14 +04:00
|
|
|
$otherNode->before($node);
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_commitOrderCalculator->getCommitOrder();
|
|
|
|
}
|
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
|
|
|
* Register a new entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @todo Rename to scheduleForInsert().
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function registerNew(Doctrine_Entity $entity)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-05-06 17:41:22 +04:00
|
|
|
$oid = $entity->getOid();
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/*if ( ! $entity->_identifier()) {
|
|
|
|
throw new Doctrine_Connection_Exception("Entity without identity cant be registered as new.");
|
|
|
|
}*/
|
2008-05-06 17:41:22 +04:00
|
|
|
if (isset($this->_dirtyEntities[$oid])) {
|
2008-04-13 00:11:11 +04:00
|
|
|
throw new Doctrine_Connection_Exception("Dirty object can't be registered as new.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
if (isset($this->_deletedEntities[$oid])) {
|
2008-04-13 00:11:11 +04:00
|
|
|
throw new Doctrine_Connection_Exception("Removed object can't be registered as new.");
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
if (isset($this->_newEntities[$oid])) {
|
2008-05-06 17:41:22 +04:00
|
|
|
throw new Doctrine_Connection_Exception("Object already registered as new. Can't register twice.");
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_newEntities[$oid] = $entity;
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($entity->_identifier()) {
|
|
|
|
$this->addToIdentityMap($entity);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether an entity is registered as new on the unit of work.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
* @return boolean
|
|
|
|
* @todo Rename to isScheduledForInsert().
|
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function isRegisteredNew(Doctrine_Entity $entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
{
|
|
|
|
return isset($this->_newEntities[$entity->getOid()]);
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
|
|
|
* Registers a clean entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
* The entity is simply put into the identity map.
|
|
|
|
*
|
|
|
|
* @param Doctrine::ORM::Entity $entity
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function registerClean(Doctrine_Entity $entity)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
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
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
/**
|
|
|
|
* Registers a dirty entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @param Doctrine::ORM::Entity $entity
|
|
|
|
* @todo Rename to scheduleForUpdate().
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function registerDirty(Doctrine_Entity $entity)
|
2008-04-13 00:11:11 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
$oid = $entity->getOid();
|
|
|
|
if ( ! $entity->_identifier()) {
|
2008-05-06 17:41:22 +04:00
|
|
|
throw new Doctrine_Connection_Exception("Entity without identity "
|
|
|
|
. "can't be registered as dirty.");
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
if (isset($this->_deletedEntities[$oid])) {
|
2008-04-13 00:11:11 +04:00
|
|
|
throw new Doctrine_Connection_Exception("Removed object can't be registered as dirty.");
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
if ( ! isset($this->_dirtyEntities[$oid]) && ! isset($this->_newEntities[$oid])) {
|
|
|
|
$this->_dirtyEntities[$oid] = $entity;
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether an entity is registered as dirty in the unit of work.
|
|
|
|
* Note: Is not very useful currently as dirty entities are only registered
|
|
|
|
* at commit time.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
* @return boolean
|
|
|
|
* @todo Rename to isScheduledForUpdate().
|
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function isRegisteredDirty(Doctrine_Entity $entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
{
|
|
|
|
return isset($this->_dirtyEntities[$entity->getOid()]);
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
2008-04-13 00:11:11 +04:00
|
|
|
* Registers a deleted entity.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
|
|
|
* @todo Rename to scheduleForDelete().
|
2008-04-13 00:11:11 +04:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function registerDeleted(Doctrine_Entity $entity)
|
2008-04-13 00:11:11 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
$oid = $entity->getOid();
|
|
|
|
if ( ! $this->isInIdentityMap($entity)) {
|
2008-05-06 17:41:22 +04:00
|
|
|
return;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
$this->removeFromIdentityMap($entity);
|
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
if (isset($this->_newEntities[$oid])) {
|
|
|
|
unset($this->_newEntities[$oid]);
|
2008-07-21 00:13:24 +04:00
|
|
|
return; // entity has not been persisted yet, so nothing more to do.
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
/* Seems unnecessary since _dirtyEntities is filled & cleared on commit, not earlier
|
|
|
|
if (isset($this->_dirtyEntities[$oid])) {
|
|
|
|
unset($this->_dirtyEntities[$oid]);
|
|
|
|
}*/
|
|
|
|
if ( ! isset($this->_deletedEntities[$oid])) {
|
|
|
|
$this->_deletedEntities[$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
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param Doctrine::ORM::Entity $entity
|
|
|
|
* @return boolean
|
|
|
|
* @todo Rename to isScheduledForDelete().
|
2007-10-23 01:47:05 +04:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function isRegisteredRemoved(Doctrine_Entity $entity)
|
2007-10-23 01:47:05 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
return isset($this->_deletedEntities[$entity->getOid()]);
|
2007-10-23 01:47:05 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Detaches an entity from the persistence management. It's persistence will
|
|
|
|
* no longer be managed by Doctrine.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param integer $oid object identifier
|
|
|
|
* @return boolean whether ot not the operation was successful
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function detach(Doctrine_Entity $entity)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
if ($this->isInIdentityMap($entity)) {
|
|
|
|
$this->removeFromIdentityMap($entity);
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
* @return unknown
|
|
|
|
* @todo Rename to isScheduled()
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function isEntityRegistered(Doctrine_Entity $entity)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
|
|
|
$oid = $entity->getOid();
|
2008-07-21 00:13:24 +04:00
|
|
|
return isset($this->_newEntities[$oid]) ||
|
|
|
|
//isset($this->_dirtyEntities[$oid]) ||
|
|
|
|
isset($this->_deletedEntities[$oid]) ||
|
|
|
|
$this->isInIdentityMap($entity);
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
/**
|
2008-03-05 14:24:33 +03:00
|
|
|
* Detaches all currently managed entities.
|
2008-07-21 00:13:24 +04:00
|
|
|
* Alternatively, if an entity class name is given, all entities of that type
|
|
|
|
* (or subtypes) are detached. Don't forget that entities are registered in
|
|
|
|
* the identity map with the name of the root entity class. So calling detachAll()
|
|
|
|
* with a class name that is not the name of a root entity has no effect.
|
2008-03-05 14:24:33 +03:00
|
|
|
*
|
|
|
|
* @return integer The number of detached entities.
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function detachAll($entityName = null)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
//TODO: what do do with new/dirty/removed lists?
|
|
|
|
$numDetached = 0;
|
|
|
|
if ($entityName !== null && isset($this->_identityMap[$entityName])) {
|
|
|
|
$numDetached = count($this->_identityMap[$entityName]);
|
|
|
|
$this->_identityMap[$entityName] = array();
|
|
|
|
} else {
|
|
|
|
$numDetached = count($this->_identityMap);
|
|
|
|
$this->_identityMap = array();
|
|
|
|
}
|
|
|
|
|
2008-03-05 14:24:33 +03:00
|
|
|
return $numDetached;
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
/**
|
2008-03-05 14:24:33 +03:00
|
|
|
* Registers an entity in the identity map.
|
2008-07-21 00:13:24 +04:00
|
|
|
* Note that entities in a hierarchy are registered with the class name of
|
|
|
|
* the root entity.
|
|
|
|
*
|
|
|
|
* @param Doctrine::ORM::Entity $entity The entity to register.
|
2008-03-05 14:24:33 +03:00
|
|
|
* @return boolean TRUE if the registration was successful, FALSE if the identity of
|
|
|
|
* the entity in question is already managed.
|
2008-02-28 18:30:55 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function addToIdentityMap(Doctrine_Entity $entity)
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
$idHash = $this->getIdentifierHash($entity->_identifier());
|
|
|
|
if ($idHash === '') {
|
2008-03-05 14:24:33 +03:00
|
|
|
throw new Doctrine_Connection_Exception("Entity with oid '" . $entity->getOid()
|
2008-05-01 13:41:47 +04:00
|
|
|
. "' has no identity and therefore can't be added to the identity map.");
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
$className = $entity->getClass()->getRootClassName();
|
2008-05-01 13:41:47 +04:00
|
|
|
if (isset($this->_identityMap[$className][$idHash])) {
|
2008-02-28 18:30:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-01 13:41:47 +04:00
|
|
|
$this->_identityMap[$className][$idHash] = $entity;
|
2008-07-21 00:13:24 +04:00
|
|
|
$entity->_state(Doctrine_Entity::STATE_MANAGED);
|
2008-02-28 18:30:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
|
|
|
* Removes an entity from the identity map.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function removeFromIdentityMap(Doctrine_Entity $entity)
|
2008-03-05 14:24:33 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
$idHash = $this->getIdentifierHash($entity->_identifier());
|
|
|
|
if ($idHash === '') {
|
2008-03-05 14:24:33 +03:00
|
|
|
throw new Doctrine_Connection_Exception("Entity with oid '" . $entity->getOid()
|
2008-05-01 13:41:47 +04:00
|
|
|
. "' has no identity and therefore can't be removed from the identity map.");
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
$className = $entity->getClass()->getRootClassName();
|
2008-05-01 13:41:47 +04:00
|
|
|
if (isset($this->_identityMap[$className][$idHash])) {
|
|
|
|
unset($this->_identityMap[$className][$idHash]);
|
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
|
|
|
/**
|
|
|
|
* Finds an entity in the identity map by its identifier hash.
|
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param string $idHash
|
|
|
|
* @param string $rootClassName
|
|
|
|
* @return Doctrine::ORM::Entity
|
2008-05-17 16:22:24 +04:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function getByIdHash($idHash, $rootClassName)
|
2008-03-05 14:24:33 +03:00
|
|
|
{
|
2008-05-01 13:41:47 +04:00
|
|
|
return $this->_identityMap[$rootClassName][$idHash];
|
|
|
|
}
|
|
|
|
public function tryGetByIdHash($idHash, $rootClassName)
|
|
|
|
{
|
|
|
|
if ($this->containsIdHash($idHash, $rootClassName)) {
|
|
|
|
return $this->getByIdHash($idHash, $rootClassName);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
|
|
|
* Gets the identifier hash for a set of identifier values.
|
2008-07-21 00:13:24 +04:00
|
|
|
* The hash is just a concatenation of the identifier values.
|
|
|
|
* The identifiers are concatenated with a space.
|
|
|
|
*
|
|
|
|
* Note that this method always returns a string. If the given array is
|
|
|
|
* empty, an empty string is returned.
|
2008-05-17 16:22:24 +04:00
|
|
|
*
|
|
|
|
* @param array $id
|
2008-07-21 00:13:24 +04:00
|
|
|
* @return string The hash.
|
2008-05-17 16:22:24 +04:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function getIdentifierHash(array $id)
|
|
|
|
{
|
|
|
|
return implode(' ', $id);
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-01 13:41:47 +04:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Checks whether an entity is registered in the identity map of the
|
|
|
|
* UnitOfWork.
|
2008-05-01 13:41:47 +04:00
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* @param Doctrine_Entity $entity
|
2008-05-01 13:41:47 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function isInIdentityMap(Doctrine_Entity $entity)
|
2008-05-01 13:41:47 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
$idHash = $this->getIdentifierHash($entity->_identifier());
|
|
|
|
if ($idHash === '') {
|
2008-05-01 13:41:47 +04:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
return isset($this->_identityMap
|
2008-07-21 00:13:24 +04:00
|
|
|
[$entity->getClass()->getRootClassName()]
|
|
|
|
[$idHash]
|
|
|
|
);
|
2008-04-13 00:11:11 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
/**
|
|
|
|
* Checks whether an identifier hash exists in the identity map.
|
|
|
|
*
|
|
|
|
* @param string $idHash
|
|
|
|
* @param string $rootClassName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
public function containsIdHash($idHash, $rootClassName)
|
2008-04-13 00:11:11 +04:00
|
|
|
{
|
2008-05-01 13:41:47 +04:00
|
|
|
return isset($this->_identityMap[$rootClassName][$idHash]);
|
2008-03-05 14:24:33 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves an entity as part of the current unit of work.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity The entity to save.
|
|
|
|
*/
|
2008-07-10 21:17:58 +04:00
|
|
|
public function save(Doctrine_Entity $entity)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
$insertNow = array();
|
|
|
|
$visited = array();
|
|
|
|
$this->_doSave($entity, $visited, $insertNow);
|
|
|
|
if ( ! empty($insertNow)) {
|
|
|
|
// We have no choice. This means that there are either new entities
|
|
|
|
// with an IDENTITY key generation or with a natural identifier.
|
|
|
|
// In both cases we must commit the inserts instantly.
|
|
|
|
//TODO: Isnt it enough to only execute the inserts instead of full flush?
|
|
|
|
$this->commit();
|
2008-07-27 23:38:56 +04:00
|
|
|
/* The following may be better:
|
|
|
|
$commitOrder = $this->_getCommitOrder($insertNow);
|
|
|
|
foreach ($commitOrder as $class) {
|
|
|
|
$this->_executeInserts($class);
|
|
|
|
}
|
|
|
|
//... remove them from _newEntities, or dont store them there in the first place
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves an entity as part of the current unit of work.
|
|
|
|
* This method is internally called during save() cascades as it tracks
|
|
|
|
* the already visited entities to prevent infinite recursions.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity The entity to save.
|
|
|
|
* @param array $visited The already visited entities.
|
|
|
|
*/
|
|
|
|
private function _doSave(Doctrine_Entity $entity, array &$visited, array &$insertNow)
|
|
|
|
{
|
|
|
|
if (isset($visited[$entity->getOid()])) {
|
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
|
|
|
$visited[$entity->getOid()] = $entity; // mark visited
|
|
|
|
|
|
|
|
$class = $entity->getClass();
|
2008-07-10 21:17:58 +04:00
|
|
|
switch ($entity->_state()) {
|
2008-07-21 00:13:24 +04:00
|
|
|
case Doctrine_Entity::STATE_MANAGED:
|
|
|
|
// nothing to do for $entity
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-07-21 00:13:24 +04:00
|
|
|
case Doctrine_Entity::STATE_NEW:
|
|
|
|
if ($class->isIdGeneratorIdentity()) {
|
|
|
|
$insertNow[$entity->getOid()] = $entity;
|
|
|
|
$this->_newEntities[$entity->getOid()] = $entity;
|
|
|
|
} else if ( ! $class->usesIdGenerator()) {
|
|
|
|
$insertNow[$entity->getOid()] = $entity;
|
2008-07-27 23:38:56 +04:00
|
|
|
$this->_newEntities[$entity->getOid()] = $entity;
|
2008-07-21 00:13:24 +04:00
|
|
|
//...
|
|
|
|
} else if ($class->isIdGeneratorSequence()) {
|
|
|
|
// Get the next sequence number
|
|
|
|
//TODO: sequence name?
|
2008-07-27 23:38:56 +04:00
|
|
|
$id = $this->_em->getConnection()->getSequenceManager()->nextId("foo");
|
2008-07-21 00:13:24 +04:00
|
|
|
$entity->set($class->getSingleIdentifierFieldName(), $id);
|
|
|
|
$this->registerNew($entity);
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Exception("Unable to handle ID generation of new entity.");
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-07-21 00:13:24 +04:00
|
|
|
case Doctrine_Entity::STATE_DETACHED:
|
|
|
|
//exception?
|
|
|
|
throw new Doctrine_Exception("Behavior of save() for a detached entity "
|
|
|
|
. "is not yet defined.");
|
|
|
|
case Doctrine_Entity::STATE_DELETED:
|
|
|
|
// $entity becomes managed again
|
|
|
|
if ($this->isRegisteredRemoved($entity)) {
|
|
|
|
//TODO: better a method for this?
|
|
|
|
unset($this->_deletedEntities[$entity->getOid()]);
|
|
|
|
} else {
|
|
|
|
//FIXME: There's more to think of here...
|
|
|
|
$this->registerNew($entity);
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
break;
|
2008-07-21 00:13:24 +04:00
|
|
|
default:
|
|
|
|
//TODO: throw UnitOfWorkException::invalidEntityState()
|
|
|
|
throw new Doctrine_Exception("Encountered invalid entity state.");
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
$this->_cascadeSave($entity, $visited, $insertNow);
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an entity as part of the current unit of work.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
*/
|
|
|
|
public function delete(Doctrine_Entity $entity)
|
2008-07-10 21:17:58 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_doDelete($entity, array());
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
|
|
|
private function _doDelete(Doctrine_Entity $entity, array &$visited)
|
|
|
|
{
|
|
|
|
if (isset($visited[$entity->getOid()])) {
|
|
|
|
return; // Prevent infinite recursion
|
|
|
|
}
|
|
|
|
|
|
|
|
$visited[$entity->getOid()] = $entity; // mark visited
|
|
|
|
|
|
|
|
$class = $entity->getClass();
|
|
|
|
switch ($entity->_state()) {
|
|
|
|
case Doctrine_Entity::STATE_NEW:
|
|
|
|
case Doctrine_Entity::STATE_DELETED:
|
|
|
|
// nothing to do for $entity
|
|
|
|
break;
|
|
|
|
case Doctrine_Entity::STATE_MANAGED:
|
|
|
|
$this->registerDeleted($entity);
|
|
|
|
break;
|
|
|
|
case Doctrine_Entity::STATE_DETACHED:
|
|
|
|
//exception?
|
|
|
|
throw new Doctrine_Exception("A detached entity can't be deleted.");
|
|
|
|
default:
|
|
|
|
//TODO: throw UnitOfWorkException::invalidEntityState()
|
|
|
|
throw new Doctrine_Exception("Encountered invalid entity state.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_cascadeDelete($entity, $visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades the save operation to associated entities.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Entity $entity
|
|
|
|
* @param array $visited
|
|
|
|
*/
|
|
|
|
private function _cascadeSave(Doctrine_Entity $entity, array &$visited, array &$insertNow)
|
|
|
|
{
|
|
|
|
foreach ($entity->getClass()->getAssociationMappings() as $assocMapping) {
|
|
|
|
if ( ! $assocMapping->isCascadeSave()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$relatedEntities = $entity->get($assocMapping->getSourceFieldName());
|
|
|
|
if ($relatedEntities instanceof Doctrine_Entity) {
|
|
|
|
$this->_doSave($relatedEntities, $visited, $insertNow);
|
|
|
|
} else if ($relatedEntities instanceof Doctrine_Collection &&
|
|
|
|
count($relatedEntities) > 0) {
|
|
|
|
foreach ($relatedEntities as $relatedEntity) {
|
|
|
|
$this->_doSave($relatedEntity, $visited, $insertNow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
private function _cascadeDelete(Doctrine_Entity $entity)
|
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
public function getCommitOrderCalculator()
|
|
|
|
{
|
|
|
|
return $this->_commitOrderCalculator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
$this->_commitOrderCalculator->clear();
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
public function scheduleCollectionUpdate(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
$this->_collectionUpdates[] = $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isCollectionScheduledForUpdate(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scheduleCollectionDeletion(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
//TODO: if $coll is already scheduled for recreation ... what to do?
|
|
|
|
// Just remove $coll from the scheduled recreations?
|
|
|
|
$this->_collectionDeletions[] = $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isCollectionScheduledForDeletion(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scheduleCollectionRecreation(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
$this->_collectionRecreations[] = $coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isCollectionScheduledForRecreation(Doctrine_Collection $coll)
|
|
|
|
{
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
// Stuff from 0.11/1.0 that we will need later (need to modify it though)
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
|
|
|
* Collects all records that need to be deleted by applying defined
|
|
|
|
* application-level delete cascades.
|
|
|
|
*
|
|
|
|
* @param array $deletions Map of the records to delete. Keys=Oids Values=Records.
|
|
|
|
*/
|
|
|
|
/*private function _collectDeletions(Doctrine_Record $record, array &$deletions)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
if ( ! $record->exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deletions[$record->getOid()] = $record;
|
|
|
|
$this->_cascadeDelete($record, $deletions);
|
|
|
|
}*/
|
2008-07-10 21:17:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cascades an ongoing delete operation to related objects. Applies only on relations
|
|
|
|
* that have 'delete' in their cascade options.
|
|
|
|
* This is an application-level cascade. Related objects that participate in the
|
|
|
|
* cascade and are not yet loaded are fetched from the database.
|
|
|
|
* Exception: many-valued relations are always (re-)fetched from the database to
|
|
|
|
* make sure we have all of them.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record The record for which the delete operation will be cascaded.
|
|
|
|
* @throws PDOException If something went wrong at database level
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
/*protected function _cascadeDelete(Doctrine_Record $record, array &$deletions)
|
2008-07-10 21:17:58 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
foreach ($record->getTable()->getRelations() as $relation) {
|
|
|
|
if ($relation->isCascadeDelete()) {
|
|
|
|
$fieldName = $relation->getAlias();
|
|
|
|
// if it's a xToOne relation and the related object is already loaded
|
|
|
|
// we don't need to refresh.
|
|
|
|
if ( ! ($relation->getType() == Doctrine_Relation::ONE && isset($record->$fieldName))) {
|
|
|
|
$record->refreshRelated($relation->getAlias());
|
|
|
|
}
|
|
|
|
$relatedObjects = $record->get($relation->getAlias());
|
|
|
|
if ($relatedObjects instanceof Doctrine_Record && $relatedObjects->exists()
|
|
|
|
&& ! isset($deletions[$relatedObjects->getOid()])) {
|
|
|
|
$this->_collectDeletions($relatedObjects, $deletions);
|
|
|
|
} else if ($relatedObjects instanceof Doctrine_Collection && count($relatedObjects) > 0) {
|
|
|
|
// cascade the delete to the other objects
|
|
|
|
foreach ($relatedObjects as $object) {
|
|
|
|
if ( ! isset($deletions[$object->getOid()])) {
|
|
|
|
$this->_collectDeletions($object, $deletions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
}*/
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
|
|
|
* Executes the deletions for all collected records during a delete operation
|
|
|
|
* (usually triggered through $record->delete()).
|
|
|
|
*
|
|
|
|
* @param array $deletions Map of the records to delete. Keys=Oids Values=Records.
|
|
|
|
*/
|
|
|
|
/*private function _executeDeletions(array $deletions)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
// collect class names
|
|
|
|
$classNames = array();
|
|
|
|
foreach ($deletions as $record) {
|
|
|
|
$classNames[] = $record->getTable()->getComponentName();
|
|
|
|
}
|
|
|
|
$classNames = array_unique($classNames);
|
|
|
|
|
|
|
|
// order deletes
|
|
|
|
$executionOrder = $this->buildFlushTree($classNames);
|
|
|
|
|
|
|
|
// execute
|
|
|
|
try {
|
|
|
|
$this->conn->beginInternalTransaction();
|
|
|
|
|
|
|
|
for ($i = count($executionOrder) - 1; $i >= 0; $i--) {
|
|
|
|
$className = $executionOrder[$i];
|
|
|
|
$table = $this->conn->getTable($className);
|
|
|
|
|
|
|
|
// collect identifiers
|
|
|
|
$identifierMaps = array();
|
|
|
|
$deletedRecords = array();
|
|
|
|
foreach ($deletions as $oid => $record) {
|
|
|
|
if ($record->getTable()->getComponentName() == $className) {
|
|
|
|
$veto = $this->_preDelete($record);
|
|
|
|
if ( ! $veto) {
|
|
|
|
$identifierMaps[] = $record->identifier();
|
|
|
|
$deletedRecords[] = $record;
|
|
|
|
unset($deletions[$oid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($deletedRecords) < 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract query parameters (only the identifier values are of interest)
|
|
|
|
$params = array();
|
|
|
|
$columnNames = array();
|
|
|
|
foreach ($identifierMaps as $idMap) {
|
|
|
|
while (list($fieldName, $value) = each($idMap)) {
|
|
|
|
$params[] = $value;
|
|
|
|
$columnNames[] = $table->getColumnName($fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$columnNames = array_unique($columnNames);
|
|
|
|
|
|
|
|
// delete
|
|
|
|
$tableName = $table->getTableName();
|
|
|
|
$sql = "DELETE FROM " . $this->conn->quoteIdentifier($tableName) . " WHERE ";
|
|
|
|
|
|
|
|
if ($table->isIdentifierComposite()) {
|
|
|
|
$sql .= $this->_buildSqlCompositeKeyCondition($columnNames, count($identifierMaps));
|
|
|
|
$this->conn->exec($sql, $params);
|
|
|
|
} else {
|
|
|
|
$sql .= $this->_buildSqlSingleKeyCondition($columnNames, count($params));
|
|
|
|
$this->conn->exec($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust state, remove from identity map and inform postDelete listeners
|
|
|
|
foreach ($deletedRecords as $record) {
|
|
|
|
// currently just for bc!
|
|
|
|
$this->_deleteCTIParents($table, $record);
|
|
|
|
//--
|
|
|
|
$record->state(Doctrine_Record::STATE_TCLEAN);
|
|
|
|
$record->getTable()->removeRecord($record);
|
|
|
|
$this->_postDelete($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->conn->commit();
|
|
|
|
// trigger postDelete for records skipped during the deletion (veto!)
|
|
|
|
foreach ($deletions as $skippedRecord) {
|
|
|
|
$this->_postDelete($skippedRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->conn->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}*/
|
2008-07-10 21:17:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the SQL condition to target multiple records who have a single-column
|
|
|
|
* primary key.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Table $table The table from which the records are going to be deleted.
|
|
|
|
* @param integer $numRecords The number of records that are going to be deleted.
|
|
|
|
* @return string The SQL condition "pk = ? OR pk = ? OR pk = ? ..."
|
|
|
|
*/
|
|
|
|
/*private function _buildSqlSingleKeyCondition($columnNames, $numRecords)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
$idColumn = $this->conn->quoteIdentifier($columnNames[0]);
|
|
|
|
return implode(' OR ', array_fill(0, $numRecords, "$idColumn = ?"));
|
|
|
|
}*/
|
2008-07-10 21:17:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the SQL condition to target multiple records who have a composite primary key.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Table $table The table from which the records are going to be deleted.
|
|
|
|
* @param integer $numRecords The number of records that are going to be deleted.
|
|
|
|
* @return string The SQL condition "(pk1 = ? AND pk2 = ?) OR (pk1 = ? AND pk2 = ?) ..."
|
|
|
|
*/
|
|
|
|
/*private function _buildSqlCompositeKeyCondition($columnNames, $numRecords)
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
|
|
|
$singleCondition = "";
|
|
|
|
foreach ($columnNames as $columnName) {
|
|
|
|
$columnName = $this->conn->quoteIdentifier($columnName);
|
|
|
|
if ($singleCondition === "") {
|
|
|
|
$singleCondition .= "($columnName = ?";
|
|
|
|
} else {
|
|
|
|
$singleCondition .= " AND $columnName = ?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$singleCondition .= ")";
|
|
|
|
$fullCondition = implode(' OR ', array_fill(0, $numRecords, $singleCondition));
|
|
|
|
|
|
|
|
return $fullCondition;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
public function getIdentityMap()
|
|
|
|
{
|
|
|
|
return $this->_identityMap;
|
|
|
|
}
|
2007-10-23 01:47:05 +04:00
|
|
|
}
|
2008-07-10 21:17:58 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|