Source for file Collection.php
Documentation is available at Collection.php
* $Id: Collection.php 2282 2007-08-28 16:45:22Z jackbravo $
* 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.com>.
* Collection of Doctrine_Record objects.
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @version $Revision: 2282 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @var array $data an array containing the records of this collection
protected $data =
array();
* @var Doctrine_Table $table each collection has only records of specified table
* @var array $_snapshot a snapshot of the fetched data
* @var Doctrine_Record $reference collection can belong to a record
* @var string $referenceField the reference field of the collection
* @var Doctrine_Relation the record this collection is related to, if any
* @var string $keyColumn the name of the column that is used for collection key mapping
* @var Doctrine_Null $null used for extremely fast null value testing
* @param Doctrine_Table|string$table
$name =
$table->getAttribute(Doctrine::ATTR_COLL_KEY);
* initializes the null object for this collection
* returns the table this collection belongs to
* @return Doctrine_Collection
public function setData(array $data)
* this method is automatically called when this Doctrine_Collection is serialized
public function serialize()
$vars =
get_object_vars($this);
unset
($vars['reference']);
unset
($vars['reference_field']);
unset
($vars['relation']);
unset
($vars['expandable']);
unset
($vars['expanded']);
unset
($vars['generator']);
$vars['_table'] =
$vars['_table']->getComponentName();
* this method is automatically called everytime a Doctrine_Collection object is unserialized
$connection =
$manager->getCurrentConnection();
foreach ($array as $name =>
$values) {
* sets the key column for this collection
* @return Doctrine_Collection
* returns the name of the key column
* returns all the records as an array
* returns the first record in the collection
* returns the last record in the collection
* sets a reference pointer
public function setReference(Doctrine_Record $record, Doctrine_Relation $relation)
$value =
$record->get($relation->getLocal());
foreach ($this->data as $record) {
* removes a specified collection element
$removed =
$this->data[$key];
unset
($this->data[$key]);
* whether or not this collection contains a specified element
* @param mixed $key the key of the element
return isset
($this->data[$key]);
public function search(Doctrine_Record $record)
* returns a record for given key
* There are two special cases:
* 1. if null is given as a key a new record is created and attached
* at the end of the collection
* 2. if given key does not exist, then a new record is create and attached
* Collection also maps referential information to newly created records
* @param mixed $key the key of the element
* @return Doctrine_Record return a specified record
public function get($key)
if ($key ===
null ||
! isset
($this->data[$key])) {
return $this->data[$key];
* @return array an array containing all primary keys
foreach ($this->data as $record) {
if (is_array($record) && isset
($record[$name])) {
$list[] =
$record[$name];
$list[] =
$record->getIncremented();
* this class implements interface countable
* returns the number of records in this collection
* @param Doctrine_Record $record
public function set($key, Doctrine_Record $record)
$this->data[$key] =
$record;
* adds a record to collection
* @param Doctrine_Record $record record to be added
* @param string $key optional key for the record
public function add(Doctrine_Record $record, $key =
null)
* for some weird reason in_array cannot be used here (php bug ?)
* if used it results in fatal error : [ nesting level too deep ]
foreach ($this->data as $val) {
if (isset
($this->data[$key])) {
$this->data[$key] =
$record;
$this->data[$value] =
$record;
foreach ($this->data as $record) {
$value =
$record->getIncremented();
foreach ($this->data as $record) {
$list[] =
$record[$rel->getLocal()];
foreach ($this->data as $record) {
$value =
$record->getIncremented();
$dql =
$rel->getRelationDql(count($list), 'collection');
$coll =
$query->query($dql, $list);
* @param Doctrine_Collection $coll
$table =
$rel->getTable();
$foreign =
$rel->getForeign();
$local =
$rel->getLocal();
foreach ($this->data as $key =>
$record) {
foreach ($coll as $k =>
$related) {
if ($related[$foreign] ==
$record[$local]) {
$this->data[$key]->setRelated($name, $related);
foreach ($this->data as $key =>
$record) {
if ( ! $record->exists()) {
foreach ($coll as $k =>
$related) {
if ($related[$foreign] ==
$record[$local]) {
$this->data[$key]->setRelated($name, $sub);
$asf =
$rel->getAssociationFactory();
$name =
$table->getComponentName();
foreach ($this->data as $key =>
$record) {
if ( ! $record->exists()) {
foreach ($coll as $k =>
$related) {
if ($related->get($local) ==
$record[$identifier]) {
$sub->add($related->get($name));
$this->data[$key]->setRelated($name, $sub);
* returns normal iterator - an iterator that will not expand this collection
* @return Doctrine_Iterator_Normal
* takes a snapshot from this collection
* snapshots are used for diff processing, for example
* when a fetched collection has three elements, then two of those
* are being removed the diff would contain one element
* Doctrine_Collection::save() attaches the diff with the help of last
* @return Doctrine_Collection
* returns the data of the last snapshot
* @return array returns the data in last snapshot
* processes the difference of the last snapshot and the current data
* Snapshot with the objects 1, 2 and 4
* Current data with objects 2, 3 and 5
* The process would remove object 4
* @return Doctrine_Collection
* Mimics the result of a $query->execute(array(), Doctrine::FETCH_ARRAY);
public function toArray($deep =
false)
foreach ($this->data as $key =>
$record) {
$data[$key] =
$record->toArray($deep);
// this is preserved for backwards compatibility
// but could be replaced with above code
* saves all records of this collection and processes the
* difference of the last snapshot and the current data
* @param Doctrine_Connection $conn optional connection parameter
* @return Doctrine_Collection
public function save(Doctrine_Connection $conn =
null)
$conn->beginTransaction();
$conn->transaction->addCollection($this);
foreach ($this->getData() as $key =>
$record) {
* deletes all records from this collection
* and uses only one database query to perform this operation
* @return Doctrine_Collection
public function delete(Doctrine_Connection $conn =
null)
$conn->beginTransaction();
$conn->transaction->addCollection($this);
foreach ($this as $key =>
$record) {
* @return object ArrayIterator
return new ArrayIterator($data);
* returns a string representation of this object