1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/lib/Doctrine/Collection.php

1037 lines
28 KiB
PHP
Raw Normal View History

2007-01-02 18:24:43 +03: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
* <http://www.phpdoctrine.org>.
2007-01-02 18:24:43 +03:00
*/
#namespace Doctrine::ORM;
2007-01-02 18:24:43 +03:00
/**
* A persistent collection.
2008-08-16 23:40:59 +04:00
*
* A collection object is strongly typed in the sense that it can only contain
2008-08-16 23:40:59 +04:00
* entities of a specific type or one of it's subtypes. A collection object is
* basically a wrapper around an ordinary php array and just like a php array
* it can have List or Map semantics.
*
* A collection of entities represents only the associations (links) to those entities.
* That means, if the collection is part of a many-many mapping and you remove
* entities from the collection, only the links in the xref 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
* mapping this will only result in the nulling out of the foreign keys on flush
2008-08-16 23:40:59 +04:00
* (or removal of the links in the xref table if the one-many is mapped through an
* xref table). If you want entities in a one-many collection to be removed when
* 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
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Roman Borschel <roman@code-factory.org>
* @todo Add more typical Collection methods.
2007-01-02 18:24:43 +03:00
*/
2008-08-16 23:40:59 +04:00
class Doctrine_Collection implements Countable, IteratorAggregate, Serializable, ArrayAccess
{
2008-08-16 23:40:59 +04:00
/**
* The base type of the collection.
*
* @var string
*/
2008-05-17 16:22:24 +04:00
protected $_entityBaseType;
2007-01-02 18:24:43 +03:00
/**
2008-08-16 23:40:59 +04:00
* An array containing the entries of this collection.
* This is the wrapped php array.
*
2008-08-16 23:40:59 +04:00
* @var array
2007-01-02 18:24:43 +03:00
*/
protected $_data = array();
2007-05-16 23:20:55 +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.
*
* @var array
2007-05-16 23:20:55 +04:00
*/
protected $_snapshot = array();
2007-01-02 18:24:43 +03:00
/**
* This entity that owns this collection.
*
* @var Doctrine::ORM::Entity
2007-01-02 18:24:43 +03:00
*/
protected $_owner;
2007-01-02 18:24:43 +03:00
/**
* The association mapping the collection belongs to.
* This is currently either a OneToManyMapping or a ManyToManyMapping.
*
* @var Doctrine::ORM::Mapping::AssociationMapping
2007-01-02 18:24:43 +03:00
*/
protected $_association;
2007-01-02 18:24:43 +03:00
/**
* The name of the field that is used for collection key mapping.
*
* @var string
2007-01-02 18:24:43 +03:00
*/
2008-03-23 14:30:29 +03:00
protected $_keyField;
2007-01-02 18:24:43 +03:00
/**
* Helper variable. Used for fast null value testing.
*
* @var Doctrine_Null
2007-01-02 18:24:43 +03:00
*/
//protected static $null;
/**
* The EntityManager that manages the persistence of the collection.
*
* @var Doctrine::ORM::EntityManager
*/
protected $_em;
/**
* The name of the field on the target entities that points to the owner
* of the collection. This is only set if the association is bidirectional.
*
* @var string
*/
protected $_backRefFieldName;
/**
* Hydration flag.
*
* @var boolean
* @see _setHydrationFlag()
*/
protected $_hydrationFlag;
2007-01-02 18:24:43 +03:00
/**
* Constructor.
* Creates a new persistent collection.
2007-01-02 18:24:43 +03:00
*/
2008-05-17 16:22:24 +04:00
public function __construct($entityBaseType, $keyField = null)
2007-01-02 18:24:43 +03:00
{
$this->_entityBaseType = $entityBaseType;
2008-09-07 17:48:40 +04:00
$this->_em = Doctrine_EntityManager::getActiveEntityManager();
2008-03-23 14:30:29 +03:00
if ($keyField !== null) {
if ( ! $this->_em->getClassMetadata($entityBaseType)->hasField($keyField)) {
throw new Doctrine_Exception("Invalid field '$keyField' can't be uses as key.");
2008-03-23 14:30:29 +03:00
}
$this->_keyField = $keyField;
2007-01-02 18:24:43 +03:00
}
}
2007-05-22 22:09:54 +04:00
/**
* setData
*
* @param array $data
* @todo Remove?
2007-05-22 22:09:54 +04:00
*/
public function setData(array $data)
{
$this->_data = $data;
2007-05-22 22:09:54 +04:00
}
2007-01-02 18:24:43 +03: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-05-16 23:20:55 +04:00
return $this;
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03: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
}
2007-01-02 18:24:43 +03:00
/**
* returns all the records as an array
*
* @return array
*/
public function unwrap()
2007-01-02 18:24:43 +03:00
{
return $this->_data;
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
* returns the first record in the collection
*
* @return mixed
*/
public function getFirst()
{
return reset($this->_data);
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
* returns the last record in the collection
*
* @return mixed
*/
public function getLast()
{
return end($this->_data);
2007-01-02 18:24:43 +03:00
}
/**
* returns the last record in the collection
*
* @return mixed
*/
public function end()
{
return end($this->_data);
}
/**
* returns the current key
*
* @return mixed
*/
public function key()
{
return key($this->_data);
}
2007-01-02 18:24:43 +03:00
/**
* INTERNAL:
* Sets the collection owner. Used (only?) during hydration.
2007-01-02 18:24:43 +03:00
*
* @return void
*/
2008-09-12 13:26:43 +04:00
public function _setOwner(Doctrine_Entity $entity, Doctrine_ORM_Mapping_AssociationMapping $relation)
2007-01-02 18:24:43 +03:00
{
$this->_owner = $entity;
$this->_association = $relation;
if ($relation->isInverseSide()) {
// for sure bidirectional
$this->_backRefFieldName = $relation->getMappedByFieldName();
} else {
$targetClass = $this->_em->getClassMetadata($relation->getTargetEntityName());
if ($targetClass->hasInverseAssociationMapping($relation->getSourceFieldName())) {
// bidirectional
$this->_backRefFieldName = $targetClass->getInverseAssociationMapping(
$relation->getSourceFieldName())->getSourceFieldName();
2007-01-02 18:24:43 +03:00
}
}
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
* INTERNAL:
2007-01-02 18:24:43 +03:00
* getReference
*
* @return mixed
*/
public function _getOwner()
2007-01-02 18:24:43 +03:00
{
return $this->_owner;
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
* Removes an entity from the collection.
2007-01-02 18:24:43 +03:00
*
* @param mixed $key
* @return boolean
*/
public function remove($key)
{
$removed = $this->_data[$key];
unset($this->_data[$key]);
2008-08-16 23:40:59 +04:00
//TODO: Register collection as dirty with the UoW if necessary
//$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this);
//TODO: delete entity if shouldDeleteOrphans
/*if ($this->_association->isOneToMany() && $this->_association->shouldDeleteOrphans()) {
$this->_em->delete($removed);
}*/
2007-01-02 18:24:43 +03:00
return $removed;
}
2008-08-16 23:40:59 +04:00
/**
* __isset()
*
* @param string $name
* @return boolean whether or not this object contains $name
*/
public function __isset($key)
{
return $this->containsKey($key);
}
/**
* __unset()
*
* @param string $name
* @since 1.0
* @return mixed
2008-08-16 23:40:59 +04:00
*/
public function __unset($key)
{
return $this->remove($key);
}
/**
* Check if an offsetExists.
*
* Part of the ArrayAccess implementation.
*
* @param mixed $offset
* @return boolean whether or not this object contains $offset
*/
public function offsetExists($offset)
{
return $this->containsKey($offset);
}
/**
* offsetGet an alias of get()
*
* Part of the ArrayAccess implementation.
*
* @see get, __get
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Part of the ArrayAccess implementation.
*
* sets $offset to $value
* @see set, __set
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
if ( ! isset($offset)) {
return $this->add($value);
}
return $this->set($offset, $value);
}
/**
* Part of the ArrayAccess implementation.
*
* unset a given offset
* @see set, offsetSet, __set
* @param mixed $offset
*/
public function offsetUnset($offset)
{
return $this->remove($offset);
}
2007-01-02 18:24:43 +03:00
/**
* Checks whether the collection contains an entity.
2007-01-02 18:24:43 +03:00
*
* @param mixed $key the key of the element
* @return boolean
*/
2008-08-16 23:40:59 +04:00
public function containsKey($key)
2007-01-02 18:24:43 +03:00
{
return isset($this->_data[$key]);
2007-01-02 18:24:43 +03:00
}
2008-08-16 23:40:59 +04:00
/**
* Enter description here...
*
* @param unknown_type $entity
* @return unknown
*/
public function contains($entity)
{
return in_array($entity, $this->_data, true);
}
/**
* Enter description here...
*
* @param unknown_type $otherColl
* @todo Impl
*/
public function containsAll($otherColl)
{
//...
}
/**
*
*/
public function search(Doctrine_Entity $record)
2007-06-05 02:38:39 +04:00
{
return array_search($record, $this->_data, true);
2007-06-05 02:38:39 +04:00
}
2007-01-02 18:24:43 +03:00
/**
* returns a record for given key
*
* Collection also maps referential information to newly created records
*
* @param mixed $key the key of the element
* @return Doctrine_Entity return a specified record
2007-01-02 18:24:43 +03:00
*/
public function get($key)
{
if (isset($this->_data[$key])) {
return $this->_data[$key];
}
return null;
2007-01-02 18:24:43 +03:00
}
/**
2008-08-16 23:40:59 +04:00
* Gets all keys.
* (Map method)
*
* @return array
2007-01-02 18:24:43 +03:00
*/
2008-08-16 23:40:59 +04:00
public function getKeys()
2007-01-02 18:24:43 +03:00
{
2008-08-16 23:40:59 +04:00
return array_keys($this->_data);
2007-01-02 18:24:43 +03:00
}
2008-08-16 23:40:59 +04:00
2007-01-02 18:24:43 +03:00
/**
2008-08-16 23:40:59 +04:00
* Gets all values.
* (Map method)
*
2007-01-02 18:24:43 +03:00
* @return array
*/
2008-08-16 23:40:59 +04:00
public function getValues()
2007-01-02 18:24:43 +03:00
{
2008-08-16 23:40:59 +04:00
return array_values($this->_data);
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
* Returns the number of records in this collection.
*
* Implementation of the Countable interface.
2007-01-02 18:24:43 +03:00
*
* @return integer The number of records in the collection.
2007-01-02 18:24:43 +03:00
*/
public function count()
{
return count($this->_data);
2007-01-02 18:24:43 +03: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
2007-01-02 18:24:43 +03:00
* @return void
*/
public function set($key, $value)
2007-01-02 18:24:43 +03:00
{
if ( ! $value instanceof Doctrine_Entity) {
throw new Doctrine_Collection_Exception('Value variable in set is not an instance of Doctrine_Entity');
}
$this->_data[$key] = $value;
2008-08-16 23:40:59 +04:00
//TODO: Register collection as dirty with the UoW if necessary
$this->_changed();
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
2008-08-16 23:40:59 +04:00
* Adds an entry to the collection.
*
2008-08-16 23:40:59 +04:00
* @param mixed $value
* @param string $key
2007-01-02 18:24:43 +03:00
* @return boolean
*/
public function add($value, $key = null)
2007-01-02 18:24:43 +03:00
{
//TODO: really only allow entities?
if ( ! $value instanceof Doctrine_Entity) {
throw new Doctrine_Record_Exception('Value variable in collection is not an instance of Doctrine_Entity.');
}
// TODO: Really prohibit duplicates?
2008-08-16 23:40:59 +04:00
if (in_array($value, $this->_data, true)) {
return false;
2007-01-02 18:24:43 +03:00
}
if (isset($key)) {
if (isset($this->_data[$key])) {
2007-01-02 18:24:43 +03:00
return false;
}
$this->_data[$key] = $value;
2008-08-16 23:40:59 +04:00
} else {
$this->_data[] = $value;
2007-01-02 18:24:43 +03:00
}
2008-08-16 23:40:59 +04:00
if ($this->_hydrationFlag) {
if ($this->_backRefFieldName) {
// set back reference to owner
$value->_internalSetReference($this->_backRefFieldName, $this->_owner);
}
} else {
//TODO: Register collection as dirty with the UoW if necessary
$this->_changed();
}
2007-01-02 18:24:43 +03:00
return true;
}
2008-08-16 23:40:59 +04:00
/**
* Adds all entities of the other collection to this collection.
*
* @param unknown_type $otherCollection
* @todo Impl
*/
public function addAll($otherCollection)
{
//...
//TODO: Register collection as dirty with the UoW if necessary
//$this->_changed();
}
2007-01-02 18:24:43 +03:00
/**
* INTERNAL:
2007-01-02 18:24:43 +03:00
* loadRelated
*
* @param mixed $name
* @return boolean
2008-08-16 23:40:59 +04:00
* @todo New implementation & maybe move elsewhere.
2007-01-02 18:24:43 +03:00
*/
/*public function loadRelated($name = null)
2007-01-02 18:24:43 +03:00
{
2007-02-07 19:31:28 +03:00
$list = array();
$query = new Doctrine_Query($this->_mapper->getConnection());
2007-01-02 18:24:43 +03:00
if ( ! isset($name)) {
foreach ($this->_data as $record) {
// FIXME: composite key support
$ids = $record->identifier();
$value = count($ids) > 0 ? array_pop($ids) : null;
2007-01-02 18:24:43 +03:00
if ($value !== null) {
$list[] = $value;
}
}
$query->from($this->_mapper->getComponentName()
. '(' . implode(", ",$this->_mapper->getTable()->getPrimaryKeys()) . ')');
$query->where($this->_mapper->getComponentName()
. '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')');
2007-01-02 18:24:43 +03:00
return $query;
}
$rel = $this->_mapper->getTable()->getRelation($name);
2007-01-02 18:24:43 +03:00
if ($rel instanceof Doctrine_Relation_LocalKey || $rel instanceof Doctrine_Relation_ForeignKey) {
foreach ($this->_data as $record) {
2007-02-17 01:54:59 +03:00
$list[] = $record[$rel->getLocal()];
}
2007-01-02 18:24:43 +03:00
} else {
foreach ($this->_data as $record) {
$ids = $record->identifier();
$value = count($ids) > 0 ? array_pop($ids) : null;
2007-01-02 18:24:43 +03:00
if ($value !== null) {
$list[] = $value;
}
2007-02-17 01:54:59 +03:00
}
2007-01-02 18:24:43 +03:00
}
2007-02-17 01:54:59 +03:00
$dql = $rel->getRelationDql(count($list), 'collection');
$coll = $query->query($dql, $list);
2007-01-02 18:24:43 +03:00
$this->populateRelated($name, $coll);
}*/
2007-01-02 18:24:43 +03:00
/**
* INTERNAL:
2007-01-02 18:24:43 +03:00
* populateRelated
*
* @param string $name
* @param Doctrine_Collection $coll
* @return void
2008-08-16 23:40:59 +04:00
* @todo New implementation & maybe move elsewhere.
2007-01-02 18:24:43 +03:00
*/
/*protected function populateRelated($name, Doctrine_Collection $coll)
2007-01-02 18:24:43 +03:00
{
$rel = $this->_mapper->getTable()->getRelation($name);
2007-01-02 18:24:43 +03:00
$table = $rel->getTable();
$foreign = $rel->getForeign();
$local = $rel->getLocal();
if ($rel instanceof Doctrine_Relation_LocalKey) {
foreach ($this->_data as $key => $record) {
2007-01-02 18:24:43 +03:00
foreach ($coll as $k => $related) {
if ($related[$foreign] == $record[$local]) {
$this->_data[$key]->_setRelated($name, $related);
2007-01-02 18:24:43 +03:00
}
}
}
} else if ($rel instanceof Doctrine_Relation_ForeignKey) {
foreach ($this->_data as $key => $record) {
2007-05-16 23:20:55 +04:00
if ( ! $record->exists()) {
2007-01-02 18:24:43 +03:00
continue;
}
$sub = new Doctrine_Collection($rel->getForeignComponentName());
2007-01-02 18:24:43 +03:00
foreach ($coll as $k => $related) {
if ($related[$foreign] == $record[$local]) {
$sub->add($related);
$coll->remove($k);
}
}
$this->_data[$key]->_setRelated($name, $sub);
2007-01-02 18:24:43 +03:00
}
} else if ($rel instanceof Doctrine_Relation_Association) {
// @TODO composite key support
$identifier = (array)$this->_mapper->getClassMetadata()->getIdentifier();
2007-01-02 18:24:43 +03:00
$asf = $rel->getAssociationFactory();
$name = $table->getComponentName();
foreach ($this->_data as $key => $record) {
2007-05-16 23:20:55 +04:00
if ( ! $record->exists()) {
2007-01-02 18:24:43 +03:00
continue;
}
$sub = new Doctrine_Collection($rel->getForeignComponentName());
2007-01-02 18:24:43 +03:00
foreach ($coll as $k => $related) {
$idField = $identifier[0];
if ($related->get($local) == $record[$idField]) {
2007-01-02 18:24:43 +03:00
$sub->add($related->get($name));
}
}
$this->_data[$key]->_setRelated($name, $sub);
2007-01-02 18:24:43 +03:00
}
}
}*/
/**
* INTERNAL:
* Sets a flag that indicates whether the collection is currently being hydrated.
* This has the following consequences:
* 1) During hydration, bidirectional associations are completed automatically
* by setting the back reference.
* 2) During hydration no change notifications are reported to the UnitOfWork.
* I.e. that means add() etc. do not cause the collection to be scheduled
* for an update.
*
* @param boolean $bool
*/
public function _setHydrationFlag($bool)
{
$this->_hydrationFlag = $bool;
2007-01-02 18:24:43 +03:00
}
2007-01-02 18:24:43 +03:00
/**
2008-08-16 23:40:59 +04:00
* INTERNAL: Takes a snapshot from this collection.
2007-05-16 23:20:55 +04:00
*
2008-08-16 23:40:59 +04:00
* Snapshots are used for diff processing, for example
2007-05-16 23:20:55 +04:00
* when a fetched collection has three elements, then two of those
2008-08-16 23:40:59 +04:00
* are being removed the diff would contain one element.
2007-05-16 23:20:55 +04:00
*
2008-08-16 23:40:59 +04:00
* Collection::save() attaches the diff with the help of last snapshot.
*
* @return void
2007-05-16 23:20:55 +04:00
*/
public function _takeSnapshot()
2007-05-16 23:20:55 +04:00
{
$this->_snapshot = $this->_data;
2007-05-16 23:20:55 +04:00
}
2007-05-16 23:20:55 +04:00
/**
2008-08-16 23:40:59 +04:00
* INTERNAL: Returns the data of the last snapshot.
2007-05-16 23:20:55 +04:00
*
* @return array returns the data in last snapshot
*/
public function _getSnapshot()
2007-05-16 23:20:55 +04:00
{
return $this->_snapshot;
}
2007-05-16 23:20:55 +04:00
/**
* INTERNAL: Processes the difference of the last snapshot and the current data.
2007-05-16 23:20:55 +04:00
*
* an example:
* Snapshot with the objects 1, 2 and 4
* Current data with objects 2, 3 and 5
*
* The process would remove objects 1 and 4
2007-05-16 23:20:55 +04:00
*
* @return Doctrine_Collection
* @todo Move elsewhere
2007-05-16 23:20:55 +04:00
*/
public function processDiff()
{
foreach (array_udiff($this->_snapshot, $this->_data, array($this, "_compareRecords")) as $record) {
2007-05-16 23:20:55 +04:00
$record->delete();
}
return $this;
}
/**
* Creates an array representation of the collection.
*
* @param boolean $deep
* @return array
*/
2007-09-22 05:32:48 +04:00
public function toArray($deep = false, $prefixKey = false)
2007-07-31 02:30:41 +04:00
{
$data = array();
2007-11-08 21:53:30 +03:00
foreach ($this as $key => $record) {
$key = $prefixKey ? get_class($record) . '_' .$key:$key;
$data[$key] = $record->toArray($deep, $prefixKey);
}
return $data;
2007-07-31 02:30:41 +04:00
}
2008-08-16 23:40:59 +04:00
/**
* Checks whether the collection is empty.
*
* @return boolean TRUE if the collection is empty, FALSE otherwise.
*/
public function isEmpty()
{
return $this->count() == 0;
}
2007-11-16 01:45:09 +03:00
/**
2008-08-16 23:40:59 +04:00
* Populate a Doctrine_Collection from an array of data.
2007-11-16 01:45:09 +03:00
*
* @param string $array
* @return void
*/
public function fromArray($array, $deep = true)
2007-09-22 01:40:54 +04:00
{
$data = array();
2007-11-16 01:45:09 +03:00
foreach ($array as $rowKey => $row) {
$this[$rowKey]->fromArray($row, $deep);
2007-09-22 01:40:54 +04:00
}
}
2007-11-16 01:45:09 +03:00
/**
2008-08-16 23:40:59 +04:00
* Synchronizes a Doctrine_Collection with data from an array.
*
* it expects an array representation of a Doctrine_Collection similar to the return
* value of the toArray() method. It will create Dectrine_Records that don't exist
* on the collection, update the ones that do and remove the ones missing in the $array
*
* @param array $array representation of a Doctrine_Collection
*/
2008-01-23 11:04:54 +03:00
public function synchronizeFromArray(array $array)
{
foreach ($this as $key => $record) {
if (isset($array[$key])) {
2008-01-23 11:04:54 +03:00
$record->synchronizeFromArray($array[$key]);
unset($array[$key]);
} else {
// remove records that don't exist in the array
$this->remove($key);
}
}
2008-01-23 11:04:54 +03:00
// create new records for each new row in the array
foreach ($array as $rowKey => $row) {
$this[$rowKey]->fromArray($row);
}
}
2007-11-16 01:45:09 +03:00
/**
* Export a Doctrine_Collection to one of the supported Doctrine_Parser formats
*
* @param string $type
* @param string $deep
* @return void
* @todo Move elsewhere.
2007-11-16 01:45:09 +03:00
*/
2008-08-16 23:40:59 +04:00
/*public function exportTo($type, $deep = false)
{
if ($type == 'array') {
return $this->toArray($deep);
} else {
return Doctrine_Parser::dump($this->toArray($deep, true), $type);
}
2008-08-16 23:40:59 +04:00
}*/
2007-11-16 01:45:09 +03:00
/**
* Import data to a Doctrine_Collection from one of the supported Doctrine_Parser formats
*
* @param string $type
* @param string $data
* @return void
* @todo Move elsewhere.
2007-11-16 01:45:09 +03:00
*/
2008-08-16 23:40:59 +04:00
/*public function importFrom($type, $data)
{
if ($type == 'array') {
return $this->fromArray($data);
} else {
return $this->fromArray(Doctrine_Parser::load($data, $type));
}
2008-08-16 23:40:59 +04:00
}*/
2007-11-16 01:45:09 +03:00
/**
2008-08-16 23:40:59 +04: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()
{
return array_udiff($this->_snapshot, $this->_data, 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()
{
return array_udiff($this->_data, $this->_snapshot, array($this, "_compareRecords"));
}
/**
* Compares two records. To be used on _snapshot diffs using array_udiff.
2008-08-16 23:40:59 +04:00
*
* @return integer
*/
protected function _compareRecords($a, $b)
{
2007-11-16 01:45:09 +03:00
if ($a->getOid() == $b->getOid()) {
return 0;
}
return ($a->getOid() > $b->getOid()) ? 1 : -1;
2007-05-17 01:28:33 +04:00
}
2007-01-02 18:24:43 +03:00
/**
* Saves all records of this collection and processes the
* difference of the last snapshot and the current data.
2007-01-02 18:24:43 +03:00
*
* @param Doctrine_Connection $conn optional connection parameter
2007-05-16 23:20:55 +04:00
* @return Doctrine_Collection
2007-01-02 18:24:43 +03:00
*/
2008-08-16 23:40:59 +04:00
/*public function save()
2007-01-02 18:24:43 +03:00
{
$conn = $this->_mapper->getConnection();
2007-11-16 01:45:09 +03:00
try {
$conn->beginInternalTransaction();
$conn->transaction->addCollection($this);
$this->processDiff();
foreach ($this->getData() as $key => $record) {
$record->save($conn);
}
$conn->commit();
} catch (Exception $e) {
$conn->rollback();
throw $e;
2007-05-16 23:20:55 +04:00
}
2007-01-02 18:24:43 +03:00
2007-05-16 23:20:55 +04:00
return $this;
2008-08-16 23:40:59 +04:00
}*/
2007-01-02 18:24:43 +03:00
/**
* Deletes all records from the collection.
2008-08-16 23:40:59 +04:00
* Shorthand for calling delete() for all entities in the collection.
2007-01-02 18:24:43 +03:00
*
* @return void
2007-01-02 18:24:43 +03:00
*/
2008-08-16 23:40:59 +04:00
/*public function delete()
{
$conn = $this->_mapper->getConnection();
2007-01-02 18:24:43 +03:00
try {
$conn->beginInternalTransaction();
$conn->transaction->addCollection($this);
foreach ($this as $key => $record) {
$record->delete($conn);
}
2007-01-02 18:24:43 +03:00
$conn->commit();
} catch (Exception $e) {
$conn->rollback();
throw $e;
2007-01-02 18:24:43 +03:00
}
2008-08-16 23:40:59 +04:00
$this->clear();
}*/
public function free($deep = false)
{
foreach ($this->getData() as $key => $record) {
if ( ! ($record instanceof Doctrine_Null)) {
$record->free($deep);
}
}
$this->_data = array();
if ($this->_owner) {
$this->_owner->free($deep);
$this->_owner = null;
}
}
2007-01-02 18:24:43 +03:00
/**
* getIterator
*
2007-01-02 18:24:43 +03:00
* @return object ArrayIterator
*/
public function getIterator()
{
$data = $this->_data;
2007-01-02 18:24:43 +03:00
return new ArrayIterator($data);
}
2007-01-02 18:24:43 +03:00
/**
* returns a string representation of this object
*/
public function __toString()
{
return Doctrine_Lib::getCollectionAsString($this);
}
/**
* INTERNAL: Gets the association mapping of the collection.
2008-08-16 23:40:59 +04:00
*
* @return Doctrine::ORM::Mapping::AssociationMapping
*/
2008-08-16 23:40:59 +04:00
public function getMapping()
{
return $this->relation;
}
2008-08-16 23:40:59 +04:00
/**
* @todo Experiment. Waiting for 5.3 closures.
* Example usage:
*
* $map = $coll->mapElements(function($key, $entity) {
* return array($entity->id, $entity->name);
* });
*
* or:
*
* $map = $coll->mapElements(function($key, $entity) {
* return array($entity->name, strtoupper($entity->name));
* });
*
*/
public function mapElements($lambda) {
$result = array();
foreach ($this->_data as $key => $entity) {
list($key, $value) = each($lambda($key, $entity));
2008-08-16 23:40:59 +04:00
$result[$key] = $value;
}
return $result;
}
/**
* Clears the collection.
*
* @return void
*/
public function clear()
{
2008-08-16 23:40:59 +04:00
//TODO: Register collection as dirty with the UoW if necessary
//TODO: If oneToMany() && shouldDeleteOrphan() delete entities
/*if ($this->_association->isOneToMany() && $this->_association->shouldDeleteOrphans()) {
foreach ($this->_data as $entity) {
$this->_em->delete($entity);
}
}*/
$this->_data = array();
2008-08-16 23:40:59 +04:00
}
private function _changed()
{
/*if ( ! $this->_em->getUnitOfWork()->isCollectionScheduledForUpdate($this)) {
$this->_em->getUnitOfWork()->scheduleCollectionUpdate($this);
}*/
}
/* Serializable implementation */
/**
* Serializes the collection.
* This method is automatically called when the Collection is serialized.
*
* Part of the implementation of the Serializable interface.
*
* @return array
*/
public function serialize()
{
$vars = get_object_vars($this);
unset($vars['reference']);
unset($vars['relation']);
unset($vars['expandable']);
unset($vars['expanded']);
unset($vars['generator']);
return serialize($vars);
}
/**
* Reconstitutes the collection object from it's serialized form.
* This method is automatically called everytime the Collection object is unserialized.
*
* Part of the implementation of the Serializable interface.
*
* @param string $serialized The serialized data
*
* @return void
*/
public function unserialize($serialized)
{
2008-09-07 17:48:40 +04:00
$manager = Doctrine_EntityManager::getActiveEntityManager();
$connection = $manager->getConnection();
$array = unserialize($serialized);
foreach ($array as $name => $values) {
$this->$name = $values;
}
$keyColumn = isset($array['keyField']) ? $array['keyField'] : null;
if ($keyColumn !== null) {
$this->_keyField = $keyColumn;
}
}
}