2007-01-02 18:24:43 +03: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-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
|
|
|
|
2010-02-09 20:13:49 +03:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping,
|
2010-03-29 17:50:57 +04:00
|
|
|
Doctrine\Common\Collections\Collection,
|
|
|
|
Closure;
|
2009-01-29 20:00:44 +03:00
|
|
|
|
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.
|
2009-07-02 15:48:44 +04:00
|
|
|
*
|
2009-01-29 20:00:44 +03:00
|
|
|
* 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
|
2009-06-23 21:50:13 +04:00
|
|
|
* mapping this will only result in the nulling out of the foreign keys on flush.
|
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>
|
2009-07-28 15:43:42 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2010-03-29 17:50:57 +04:00
|
|
|
final class PersistentCollection implements Collection
|
2009-07-02 15:48:44 +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.
|
2009-07-02 15:48:44 +04: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
|
|
|
|
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;
|
2009-07-02 15:48:44 +04:00
|
|
|
|
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;
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
/**
|
2009-08-31 20:21:29 +04:00
|
|
|
* The class descriptor of the collection's entity type.
|
2009-01-29 20:00:44 +03:00
|
|
|
*/
|
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-07-02 15:48:44 +04:00
|
|
|
|
2009-07-29 15:57:27 +04:00
|
|
|
/**
|
|
|
|
* Whether the collection has already been initialized.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-07-28 20:36:24 +04:00
|
|
|
private $_initialized = true;
|
2009-07-29 15:57:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The wrapped Collection instance.
|
|
|
|
*
|
|
|
|
* @var Collection
|
|
|
|
*/
|
|
|
|
private $_coll;
|
2009-05-17 23:27:12 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-08-31 22:27:16 +04:00
|
|
|
* Creates a new persistent collection.
|
2009-07-29 15:57:27 +04:00
|
|
|
*
|
|
|
|
* @param EntityManager $em The EntityManager the collection will be associated with.
|
|
|
|
* @param ClassMetadata $class The class descriptor of the entity type of this collection.
|
|
|
|
* @param array The collection elements.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2009-07-29 15:57:27 +04:00
|
|
|
public function __construct(EntityManager $em, $class, $coll)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_coll = $coll;
|
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-07-04 20:32:19 +04:00
|
|
|
* INTERNAL:
|
2009-08-31 20:21:29 +04:00
|
|
|
* Sets the collection's owning entity together with the AssociationMapping that
|
|
|
|
* describes the association between the owner and the elements of the collection.
|
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;
|
2010-04-10 02:00:36 +04:00
|
|
|
$this->_backRefFieldName = $assoc->inversedBy ?: $assoc->mappedBy;
|
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
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
|
|
|
|
public function getTypeClass()
|
|
|
|
{
|
|
|
|
return $this->_typeClass;
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2009-07-02 15:48:44 +04:00
|
|
|
/**
|
|
|
|
* INTERNAL:
|
2009-08-31 20:21:29 +04:00
|
|
|
* Adds an element to a collection during hydration. This will automatically
|
2010-02-27 20:48:18 +03:00
|
|
|
* complete bidirectional associations in the case of a one-to-many association.
|
2009-07-02 15:48:44 +04:00
|
|
|
*
|
2009-07-28 15:43:42 +04:00
|
|
|
* @param mixed $element The element to add.
|
2009-07-02 15:48:44 +04:00
|
|
|
*/
|
2009-07-28 20:36:24 +04:00
|
|
|
public function hydrateAdd($element)
|
2009-07-02 15:48:44 +04:00
|
|
|
{
|
2009-09-06 12:48:44 +04:00
|
|
|
$this->_coll->add($element);
|
2010-04-10 02:00:36 +04:00
|
|
|
// If _backRefFieldName is set and its a one-to-many association,
|
|
|
|
// we need to set the back reference.
|
2010-02-27 20:48:18 +03:00
|
|
|
if ($this->_backRefFieldName && $this->_association->isOneToMany()) {
|
2009-07-02 15:48:44 +04:00
|
|
|
// Set back reference to owner
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->_typeClass->reflFields[$this->_backRefFieldName]
|
|
|
|
->setValue($element, $this->_owner);
|
|
|
|
$this->_em->getUnitOfWork()->setOriginalEntityProperty(
|
|
|
|
spl_object_hash($element),
|
|
|
|
$this->_backRefFieldName,
|
|
|
|
$this->_owner);
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-08-16 23:40:59 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-02 15:48:44 +04:00
|
|
|
* INTERNAL:
|
|
|
|
* Sets a keyed element in the collection during hydration.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
2009-07-02 15:48:44 +04:00
|
|
|
* @param mixed $key The key to set.
|
|
|
|
* $param mixed $value The element to set.
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
2009-08-31 20:21:29 +04:00
|
|
|
public function hydrateSet($key, $element)
|
2008-08-16 23:40:59 +04:00
|
|
|
{
|
2009-09-06 12:48:44 +04:00
|
|
|
$this->_coll->set($key, $element);
|
2009-08-31 20:21:29 +04:00
|
|
|
// If _backRefFieldName is set, then the association is bidirectional
|
|
|
|
// and we need to set the back reference.
|
2010-02-27 20:48:18 +03:00
|
|
|
if ($this->_backRefFieldName && $this->_association->isOneToMany()) {
|
2009-08-31 20:21:29 +04:00
|
|
|
// Set back reference to owner
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->_typeClass->reflFields[$this->_backRefFieldName]
|
|
|
|
->setValue($element, $this->_owner);
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
2008-08-16 23:40:59 +04:00
|
|
|
}
|
2009-05-07 17:54:01 +04:00
|
|
|
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* Initializes the collection by loading its contents from the database
|
|
|
|
* if the collection is not yet initialized.
|
2009-07-21 19:53:58 +04:00
|
|
|
*/
|
2009-05-17 23:27:12 +04:00
|
|
|
private function _initialize()
|
|
|
|
{
|
2009-07-28 20:36:24 +04:00
|
|
|
if ( ! $this->_initialized) {
|
2009-11-08 14:07:49 +03:00
|
|
|
if ($this->_isDirty) {
|
|
|
|
// Has NEW objects added through add(). Remember them.
|
|
|
|
$newObjects = $this->_coll->toArray();
|
|
|
|
}
|
2009-10-15 18:39:43 +04:00
|
|
|
$this->_coll->clear();
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_association->load($this->_owner, $this, $this->_em);
|
2009-11-19 16:26:27 +03:00
|
|
|
$this->takeSnapshot();
|
2009-11-08 14:07:49 +03:00
|
|
|
// Reattach NEW objects added through add(), if any.
|
|
|
|
if (isset($newObjects)) {
|
|
|
|
foreach ($newObjects as $obj) {
|
|
|
|
$this->_coll->add($obj);
|
|
|
|
}
|
2009-11-19 16:29:55 +03:00
|
|
|
$this->_isDirty = true;
|
2009-11-08 14:07:49 +03:00
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_initialized = true;
|
|
|
|
}
|
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-08-03 17:25:56 +04:00
|
|
|
$this->_snapshot = $this->_coll->toArray();
|
2009-11-08 14:07:49 +03:00
|
|
|
$this->_isDirty = false;
|
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-12-15 21:38:15 +03:00
|
|
|
return array_udiff_assoc($this->_snapshot, $this->_coll->toArray(),
|
|
|
|
function($a, $b) {return $a === $b ? 0 : 1;});
|
2007-05-17 01:28:33 +04:00
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
|
|
|
/**
|
2009-11-08 14:07:49 +03: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-12-15 21:38:15 +03:00
|
|
|
return array_udiff_assoc($this->_coll->toArray(), $this->_snapshot,
|
|
|
|
function($a, $b) {return $a === $b ? 0 : 1;});
|
2007-05-17 01:28:33 +04:00
|
|
|
}
|
2009-07-02 15:48:44 +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.
|
2009-07-02 15:48:44 +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
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
|
|
|
* Marks this collection as changed/dirty.
|
|
|
|
*/
|
2008-08-16 23:40:59 +04:00
|
|
|
private function _changed()
|
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
if ( ! $this->_isDirty) {
|
|
|
|
$this->_isDirty = true;
|
|
|
|
//if ($this->_isNotifyRequired) {
|
|
|
|
//$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this);
|
|
|
|
//}
|
|
|
|
}
|
2009-02-02 14:55:50 +03:00
|
|
|
}
|
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-07-25 20:33:29 +04:00
|
|
|
|
|
|
|
/**
|
2009-08-31 20:21:29 +04:00
|
|
|
* Sets the initialized flag of the collection, forcing it into that state.
|
2009-07-25 20:33:29 +04:00
|
|
|
*
|
2009-08-31 20:21:29 +04:00
|
|
|
* @param boolean $bool
|
2009-07-25 20:33:29 +04:00
|
|
|
*/
|
|
|
|
public function setInitialized($bool)
|
|
|
|
{
|
|
|
|
$this->_initialized = $bool;
|
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
2009-05-07 21:56:09 +04:00
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* Checks whether this collection has been initialized.
|
2009-05-07 21:56:09 +04:00
|
|
|
*
|
2009-07-28 20:36:24 +04:00
|
|
|
* @return boolean
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
2009-07-28 20:36:24 +04:00
|
|
|
public function isInitialized()
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
2009-07-28 20:36:24 +04:00
|
|
|
return $this->_initialized;
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/** {@inheritdoc} */
|
2009-07-28 15:43:42 +04:00
|
|
|
public function first()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->first();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/** {@inheritdoc} */
|
2009-07-28 15:43:42 +04:00
|
|
|
public function last()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->last();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
public function remove($key)
|
|
|
|
{
|
2009-11-19 16:12:00 +03:00
|
|
|
// TODO: If the keys are persistent as well (not yet implemented)
|
|
|
|
// and the collection is not initialized and orphanRemoval is
|
|
|
|
// not used we can issue a straight SQL delete/update on the
|
|
|
|
// association (table). Without initializing the collection.
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
$removed = $this->_coll->remove($key);
|
2009-07-28 15:43:42 +04:00
|
|
|
if ($removed) {
|
|
|
|
$this->_changed();
|
2009-10-28 18:32:55 +03:00
|
|
|
if ($this->_association !== null && $this->_association->isOneToMany() &&
|
|
|
|
$this->_association->orphanRemoval) {
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_em->getUnitOfWork()->scheduleOrphanRemoval($removed);
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 19:30:27 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function removeElement($element)
|
|
|
|
{
|
2009-11-19 16:12:00 +03:00
|
|
|
// TODO: Assuming the identity of entities in a collection is always based
|
|
|
|
// on their primary key (there is no equals/hashCode in PHP),
|
|
|
|
// if the collection is not initialized, we could issue a straight
|
|
|
|
// SQL DELETE/UPDATE on the association (table) without initializing
|
|
|
|
// the collection.
|
|
|
|
/*if ( ! $this->_initialized) {
|
|
|
|
$this->_em->getUnitOfWork()->getCollectionPersister($this->_association)
|
|
|
|
->deleteRows($this, $element);
|
|
|
|
}*/
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
$result = $this->_coll->removeElement($element);
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_changed();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function containsKey($key)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->containsKey($key);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
public function contains($element)
|
|
|
|
{
|
2009-11-21 21:52:02 +03:00
|
|
|
/* DRAFT
|
|
|
|
if ($this->_initialized) {
|
|
|
|
return $this->_coll->contains($element);
|
|
|
|
} else {
|
|
|
|
if ($element is MANAGED) {
|
|
|
|
if ($this->_coll->contains($element)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$exists = check db for existence;
|
|
|
|
if ($exists) {
|
|
|
|
$this->_coll->add($element);
|
|
|
|
}
|
|
|
|
return $exists;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}*/
|
2009-11-19 16:26:27 +03:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->contains($element);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function exists(Closure $p)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->exists($p);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2010-03-29 17:50:57 +04:00
|
|
|
public function indexOf($element)
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
|
|
|
$this->_initialize();
|
2010-03-29 17:50:57 +04:00
|
|
|
return $this->_coll->indexOf($element);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->get($key);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function getKeys()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->getKeys();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-29 15:57:27 +04:00
|
|
|
public function getValues()
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->getValues();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->count();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
2009-11-08 14:07:49 +03:00
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_coll->set($key, $value);
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
public function add($value)
|
|
|
|
{
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_coll->add($value);
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_changed();
|
2009-07-28 20:36:24 +04:00
|
|
|
return true;
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->isEmpty();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->getIterator();
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function map(Closure $func)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2010-03-15 20:19:00 +03:00
|
|
|
return $this->_coll->map($func);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function filter(Closure $p)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->filter($p);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function forAll(Closure $p)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->forAll($p);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function partition(Closure $p)
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
return $this->_coll->partition($p);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2010-01-05 14:45:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
|
|
|
return $this->_coll->toArray();
|
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-28 20:36:24 +04:00
|
|
|
* {@inheritdoc}
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->_initialize();
|
2009-07-29 15:57:27 +04:00
|
|
|
$result = $this->_coll->clear();
|
2009-07-28 15:43:42 +04:00
|
|
|
if ($this->_association->isOwningSide) {
|
|
|
|
$this->_changed();
|
|
|
|
$this->_em->getUnitOfWork()->scheduleCollectionDeletion($this);
|
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
return $result;
|
2009-05-07 21:56:09 +04:00
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2009-07-29 15:57:27 +04:00
|
|
|
return array('_coll');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ArrayAccess implementation */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see containsKey()
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return $this->containsKey($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see get()
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
return $this->get($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see add()
|
|
|
|
* @see set()
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
if ( ! isset($offset)) {
|
|
|
|
return $this->add($value);
|
|
|
|
}
|
|
|
|
return $this->set($offset, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see remove()
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
return $this->remove($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return $this->_coll->key();
|
|
|
|
}
|
|
|
|
|
2009-08-03 17:25:56 +04:00
|
|
|
/**
|
|
|
|
* Gets the element of the collection at the current iterator position.
|
|
|
|
*/
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return $this->_coll->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the internal iterator position to the next element.
|
|
|
|
*/
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
return $this->_coll->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the wrapped Collection instance.
|
|
|
|
*/
|
2009-07-29 15:57:27 +04:00
|
|
|
public function unwrap()
|
|
|
|
{
|
|
|
|
return $this->_coll;
|
2009-07-28 20:36:24 +04:00
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|