2007-01-02 18:24:43 +03:00
|
|
|
<?php
|
|
|
|
/*
|
2009-02-04 19:35:36 +03:00
|
|
|
* $Id$
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2009-02-04 19:35:36 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM;
|
2008-05-30 16:09:24 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
use Doctrine\Common\DoctrineException;
|
2009-01-29 20:00:44 +03:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2009-01-29 20:00:44 +03:00
|
|
|
* A PersistentCollection represents a collection of elements that have persistent state.
|
|
|
|
* Collections of entities represent only the associations (links) to those entities.
|
2008-08-16 23:40:59 +04:00
|
|
|
* That means, if the collection is part of a many-many mapping and you remove
|
2009-02-06 20:16:39 +03:00
|
|
|
* entities from the collection, only the links in the relation table are removed (on flush).
|
2008-08-16 23:40:59 +04:00
|
|
|
* Similarly, if you remove entities from a collection that is part of a one-many
|
2008-08-31 22:27:16 +04:00
|
|
|
* mapping this will only result in the nulling out of the foreign keys on flush
|
2009-02-06 20:16:39 +03:00
|
|
|
* (or removal of the links in the relation table if the one-many is mapped through a
|
|
|
|
* relation table). If you want entities in a one-many collection to be removed when
|
2008-08-16 23:40:59 +04:00
|
|
|
* they're removed from the collection, use deleteOrphans => true on the one-many
|
|
|
|
* mapping.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
2008-08-16 23:40:59 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2009-01-04 19:15:32 +03:00
|
|
|
* @since 2.0
|
2008-09-12 16:34:46 +04:00
|
|
|
* @version $Revision: 4930 $
|
2008-08-16 23:40:59 +04:00
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
final class PersistentCollection extends \Doctrine\Common\Collections\Collection
|
2008-07-21 00:13:24 +04:00
|
|
|
{
|
2008-08-16 23:40:59 +04:00
|
|
|
/**
|
|
|
|
* The base type of the collection.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
private $_type;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* A snapshot of the collection at the moment it was fetched from the database.
|
|
|
|
* This is used to create a diff of the collection at commit time.
|
2008-01-12 22:49:11 +03:00
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @var array
|
2007-05-16 23:20:55 +04:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_snapshot = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* The entity that owns this collection.
|
2008-01-12 22:49:11 +03:00
|
|
|
*
|
2009-01-14 00:56:43 +03:00
|
|
|
* @var object
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_owner;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* The association mapping the collection belongs to.
|
|
|
|
* This is currently either a OneToManyMapping or a ManyToManyMapping.
|
2008-01-12 22:49:11 +03:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\ORM\Mapping\AssociationMapping
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_association;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2009-05-03 22:07:57 +04:00
|
|
|
* The name of the field that is used for collection indexing.
|
2008-01-12 22:49:11 +03:00
|
|
|
*
|
|
|
|
* @var string
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_keyField;
|
2008-07-04 20:32:19 +04:00
|
|
|
|
|
|
|
/**
|
2008-08-31 22:27:16 +04:00
|
|
|
* The EntityManager that manages the persistence of the collection.
|
2008-07-04 20:32:19 +04:00
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* @var Doctrine\ORM\EntityManager
|
2008-07-04 20:32:19 +04:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_em;
|
2008-08-31 22:27:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the field on the target entities that points to the owner
|
2009-05-03 22:07:57 +04:00
|
|
|
* of the collection. This is only set if the association is bi-directional.
|
2008-08-31 22:27:16 +04:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_backRefFieldName;
|
2008-08-31 22:27:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hydration flag.
|
|
|
|
*
|
|
|
|
* @var boolean
|
2009-02-07 20:02:13 +03:00
|
|
|
* @see setHydrationFlag()
|
2008-08-31 22:27:16 +04:00
|
|
|
*/
|
2009-05-03 22:07:57 +04:00
|
|
|
private $_hydrationFlag = false;
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
/**
|
|
|
|
* The class descriptor of the owning entity.
|
|
|
|
*/
|
2009-05-17 23:27:12 +04:00
|
|
|
private $_typeClass;
|
2009-01-15 16:30:44 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
|
|
|
* Whether the collection is dirty and needs to be synchronized with the database
|
|
|
|
* when the UnitOfWork that manages its persistent state commits.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private $_isDirty = false;
|
2009-05-21 23:18:14 +04:00
|
|
|
|
|
|
|
/** Whether the collection has already been initialized. */
|
2009-05-17 23:27:12 +04:00
|
|
|
private $_initialized = false;
|
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-08-31 22:27:16 +04:00
|
|
|
* Creates a new persistent collection.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-05-13 19:19:27 +04:00
|
|
|
public function __construct(EntityManager $em, $class, array $data = array())
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2009-01-29 20:00:44 +03:00
|
|
|
parent::__construct($data);
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->_type = $class->name;
|
2009-01-04 19:15:32 +03:00
|
|
|
$this->_em = $em;
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_typeClass = $class;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-08-31 22:27:16 +04:00
|
|
|
* INTERNAL: Sets the key column for this collection
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @param string $column
|
2007-05-16 23:20:55 +04:00
|
|
|
* @return Doctrine_Collection
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2008-03-23 14:30:29 +03:00
|
|
|
public function setKeyField($fieldName)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-03-23 14:30:29 +03:00
|
|
|
$this->_keyField = $fieldName;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-08-31 22:27:16 +04:00
|
|
|
* INTERNAL: returns the name of the key column
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-03-23 14:30:29 +03:00
|
|
|
public function getKeyField()
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-03-23 14:30:29 +03:00
|
|
|
return $this->_keyField;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-07-04 20:32:19 +04:00
|
|
|
* INTERNAL:
|
2008-08-31 22:27:16 +04:00
|
|
|
* Sets the collection owner. Used (only?) during hydration.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
2009-01-14 00:56:43 +03:00
|
|
|
* @param object $entity
|
2009-02-02 14:55:50 +03:00
|
|
|
* @param AssociationMapping $assoc
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
public function setOwner($entity, AssociationMapping $assoc)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-07-04 20:32:19 +04:00
|
|
|
$this->_owner = $entity;
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_association = $assoc;
|
|
|
|
if ($assoc->isInverseSide()) {
|
2009-05-17 23:27:12 +04:00
|
|
|
// For sure bi-directional
|
|
|
|
$this->_backRefFieldName = $assoc->mappedByFieldName;
|
2008-08-31 22:27:16 +04:00
|
|
|
} else {
|
2009-05-17 23:27:12 +04:00
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
|
|
|
if (isset($targetClass->inverseMappings[$assoc->sourceFieldName])) {
|
|
|
|
// Bi-directional
|
|
|
|
$this->_backRefFieldName = $targetClass->inverseMappings[$assoc->sourceFieldName]
|
|
|
|
->sourceFieldName;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-07-04 20:32:19 +04:00
|
|
|
* INTERNAL:
|
2008-12-18 17:08:11 +03:00
|
|
|
* Gets the collection owner.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
2009-01-14 00:56:43 +03:00
|
|
|
* @return object
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-02-04 19:35:36 +03:00
|
|
|
public function getOwner()
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-07-04 20:32:19 +04:00
|
|
|
return $this->_owner;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
|
|
|
* Gets the class descriptor for the owning entity class.
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Mapping\ClassMetadata
|
|
|
|
*/
|
|
|
|
public function getOwnerClass()
|
|
|
|
{
|
2009-05-17 23:27:12 +04:00
|
|
|
return $this->_typeClass;
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2009-01-29 20:00:44 +03:00
|
|
|
* Removes an element from the collection.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @param mixed $key
|
|
|
|
* @return boolean
|
2009-01-14 00:56:43 +03:00
|
|
|
* @override
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
public function remove($key)
|
|
|
|
{
|
2008-08-22 13:05:14 +04:00
|
|
|
//TODO: delete entity if shouldDeleteOrphans
|
|
|
|
/*if ($this->_association->isOneToMany() && $this->_association->shouldDeleteOrphans()) {
|
|
|
|
$this->_em->delete($removed);
|
|
|
|
}*/
|
2009-01-14 00:56:43 +03:00
|
|
|
$removed = parent::remove($key);
|
2009-05-17 23:27:12 +04:00
|
|
|
if ($removed) {
|
|
|
|
$this->_changed();
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
return $removed;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-08-16 23:40:59 +04:00
|
|
|
* When the collection is a Map this is like put(key,value)/add(key,value).
|
|
|
|
* When the collection is a List this is like add(position,value).
|
|
|
|
*
|
2007-01-02 18:24:43 +03:00
|
|
|
* @param integer $key
|
2008-08-16 23:40:59 +04:00
|
|
|
* @param mixed $value
|
2009-01-14 00:56:43 +03:00
|
|
|
* @override
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
public function set($key, $value)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
parent::set($key, $value);
|
2009-05-17 23:27:12 +04:00
|
|
|
if ( ! $this->_hydrationFlag) {
|
|
|
|
$this->_changed();
|
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2009-01-29 20:00:44 +03:00
|
|
|
* Adds an element to the collection.
|
2008-07-21 00:13:24 +04:00
|
|
|
*
|
2008-08-16 23:40:59 +04:00
|
|
|
* @param mixed $value
|
|
|
|
* @param string $key
|
2009-05-03 14:58:16 +04:00
|
|
|
* @return boolean Always TRUE.
|
2009-01-14 00:56:43 +03:00
|
|
|
* @override
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-01-14 00:56:43 +03:00
|
|
|
public function add($value)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2009-05-17 23:27:12 +04:00
|
|
|
parent::add($value);
|
2009-01-29 20:00:44 +03:00
|
|
|
|
2008-08-31 22:27:16 +04:00
|
|
|
if ($this->_hydrationFlag) {
|
|
|
|
if ($this->_backRefFieldName) {
|
2009-05-07 20:36:27 +04:00
|
|
|
// Set back reference to owner
|
|
|
|
if ($this->_association->isOneToMany()) {
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_typeClass->getReflectionProperty($this->_backRefFieldName)
|
2009-05-07 20:36:27 +04:00
|
|
|
->setValue($value, $this->_owner);
|
|
|
|
} else {
|
|
|
|
// ManyToMany
|
2009-05-17 23:27:12 +04:00
|
|
|
$this->_typeClass->getReflectionProperty($this->_backRefFieldName)
|
2009-05-07 20:36:27 +04:00
|
|
|
->getValue($value)->add($this->_owner);
|
|
|
|
}
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->_changed();
|
|
|
|
}
|
2008-02-24 19:54:02 +03:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
/**
|
2009-05-03 14:58:16 +04:00
|
|
|
* Adds all elements of the other collection to this collection.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
2009-02-04 19:35:36 +03:00
|
|
|
* @param object $otherCollection
|
2008-08-16 23:40:59 +04:00
|
|
|
* @todo Impl
|
2009-02-04 19:35:36 +03:00
|
|
|
* @override
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
|
|
|
public function addAll($otherCollection)
|
|
|
|
{
|
2009-01-04 19:15:32 +03:00
|
|
|
parent::addAll($otherCollection);
|
2008-08-16 23:40:59 +04:00
|
|
|
//...
|
|
|
|
//TODO: Register collection as dirty with the UoW if necessary
|
|
|
|
//$this->_changed();
|
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
/**
|
|
|
|
* Checks whether an element is contained in the collection.
|
|
|
|
* This is an O(n) operation.
|
|
|
|
*/
|
2009-05-07 17:54:01 +04:00
|
|
|
public function contains($element)
|
|
|
|
{
|
2009-05-17 23:27:12 +04:00
|
|
|
|
|
|
|
if ( ! $this->_initialized) {
|
|
|
|
//TODO: Probably need to hit the database here...?
|
|
|
|
//return $this->_checkElementExistence($element);
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
|
|
|
return parent::contains($element);
|
|
|
|
}
|
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
if ( ! $this->_initialized) {
|
|
|
|
//TODO: Initialize
|
|
|
|
}
|
|
|
|
return parent::count();
|
|
|
|
}
|
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
private function _checkElementExistence($element)
|
|
|
|
{
|
|
|
|
|
2009-05-17 23:27:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _initialize()
|
|
|
|
{
|
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
}
|
2008-08-31 22:27:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
* Sets a flag that indicates whether the collection is currently being hydrated.
|
2009-05-03 14:58:16 +04:00
|
|
|
*
|
|
|
|
* If the flag is set to TRUE, this has the following consequences:
|
|
|
|
*
|
2008-08-31 22:27:16 +04:00
|
|
|
* 1) During hydration, bidirectional associations are completed automatically
|
|
|
|
* by setting the back reference.
|
|
|
|
* 2) During hydration no change notifications are reported to the UnitOfWork.
|
2009-05-03 14:58:16 +04:00
|
|
|
* That means add() etc. do not cause the collection to be scheduled
|
2008-08-31 22:27:16 +04:00
|
|
|
* for an update.
|
|
|
|
*
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
public function setHydrationFlag($bool)
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
|
|
|
$this->_hydrationFlag = $bool;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2009-05-03 14:58:16 +04:00
|
|
|
* INTERNAL:
|
|
|
|
* Tells this collection to take a snapshot of its current state.
|
2007-05-16 23:20:55 +04:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
public function takeSnapshot()
|
2007-05-16 23:20:55 +04:00
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_snapshot = $this->_elements;
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* INTERNAL:
|
2009-01-14 00:56:43 +03:00
|
|
|
* Returns the last snapshot of the elements in the collection.
|
2007-05-16 23:20:55 +04:00
|
|
|
*
|
2009-01-14 00:56:43 +03:00
|
|
|
* @return array The last snapshot of the elements.
|
2007-05-16 23:20:55 +04:00
|
|
|
*/
|
2009-02-07 20:02:13 +03:00
|
|
|
public function getSnapshot()
|
2007-05-16 23:20:55 +04:00
|
|
|
{
|
|
|
|
return $this->_snapshot;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-11-16 01:45:09 +03:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* INTERNAL:
|
|
|
|
* getDeleteDiff
|
2007-11-16 01:45:09 +03:00
|
|
|
*
|
2008-08-16 23:40:59 +04:00
|
|
|
* @return array
|
2007-11-16 01:45:09 +03:00
|
|
|
*/
|
2007-05-17 01:28:33 +04:00
|
|
|
public function getDeleteDiff()
|
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
return array_udiff($this->_snapshot, $this->_elements, array($this, '_compareRecords'));
|
2007-05-17 01:28:33 +04:00
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
|
|
|
/**
|
2008-08-16 23:40:59 +04:00
|
|
|
* INTERNAL getInsertDiff
|
2007-11-16 01:45:09 +03:00
|
|
|
*
|
2008-08-16 23:40:59 +04:00
|
|
|
* @return array
|
2007-11-16 01:45:09 +03:00
|
|
|
*/
|
2007-05-17 01:28:33 +04:00
|
|
|
public function getInsertDiff()
|
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
return array_udiff($this->_elements, $this->_snapshot, array($this, '_compareRecords'));
|
2007-09-10 20:16:24 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-10 20:16:24 +04:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Compares two records. To be used on _snapshot diffs using array_udiff.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
|
|
|
* @return integer
|
2007-09-10 20:16:24 +04:00
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
private function _compareRecords($a, $b)
|
2007-09-10 20:16:24 +04:00
|
|
|
{
|
2009-05-17 23:27:12 +04:00
|
|
|
return $a === $b ? 0 : 1;
|
2007-05-17 01:28:33 +04:00
|
|
|
}
|
2007-11-05 21:05:17 +03:00
|
|
|
|
|
|
|
/**
|
2008-08-31 22:27:16 +04:00
|
|
|
* INTERNAL: Gets the association mapping of the collection.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
2009-01-14 00:56:43 +03:00
|
|
|
* @return Doctrine\ORM\Mapping\AssociationMapping
|
2007-11-05 21:05:17 +03:00
|
|
|
*/
|
2008-08-16 23:40:59 +04:00
|
|
|
public function getMapping()
|
2007-11-05 21:05:17 +03:00
|
|
|
{
|
2009-02-02 14:55:50 +03:00
|
|
|
return $this->_association;
|
2007-11-05 21:05:17 +03:00
|
|
|
}
|
2008-06-15 19:56:28 +04:00
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
/**
|
|
|
|
* Clears the collection.
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
public function clear()
|
|
|
|
{
|
2008-08-16 23:40:59 +04:00
|
|
|
//TODO: Register collection as dirty with the UoW if necessary
|
2008-08-22 13:05:14 +04:00
|
|
|
//TODO: If oneToMany() && shouldDeleteOrphan() delete entities
|
|
|
|
/*if ($this->_association->isOneToMany() && $this->_association->shouldDeleteOrphans()) {
|
|
|
|
foreach ($this->_data as $entity) {
|
|
|
|
$this->_em->delete($entity);
|
|
|
|
}
|
|
|
|
}*/
|
2009-01-04 19:15:32 +03:00
|
|
|
parent::clear();
|
2009-05-07 20:36:27 +04:00
|
|
|
$this->_changed();
|
2008-08-16 23:40:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _changed()
|
|
|
|
{
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_isDirty = true;
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a boolean flag indicating whether this colleciton is dirty which means
|
|
|
|
* its state needs to be synchronized with the database.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE if the collection is dirty, FALSE otherwise.
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
public function isDirty()
|
|
|
|
{
|
|
|
|
return $this->_isDirty;
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a boolean flag, indicating whether this collection is dirty.
|
|
|
|
*
|
|
|
|
* @param boolean $dirty Whether the collection should be marked dirty or not.
|
|
|
|
*/
|
2009-02-02 14:55:50 +03:00
|
|
|
public function setDirty($dirty)
|
|
|
|
{
|
|
|
|
$this->_isDirty = $dirty;
|
2008-06-15 19:56:28 +04:00
|
|
|
}
|
2009-05-07 21:56:09 +04:00
|
|
|
|
|
|
|
/* Serializable implementation */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by PHP when this collection is serialized. Ensures that only the
|
|
|
|
* elements are properly serialized.
|
|
|
|
*
|
|
|
|
* @internal Tried to implement Serializable first but that did not work well
|
|
|
|
* with circular references. This solution seems simpler and works well.
|
|
|
|
*/
|
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
return array('_elements');
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|