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
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2007-05-16 23:20:55 +04:00
|
|
|
Doctrine::autoload('Doctrine_Access');
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-03-17 16:26:34 +03:00
|
|
|
* A Doctrine_Collection represents a collection of entities.
|
2008-01-12 22:49:11 +03:00
|
|
|
* A collection object is strongly typed in the sense that it can only contain
|
2008-03-17 16:26:34 +03:00
|
|
|
* entities of a specific type or one it's subtypes.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Collection
|
2007-01-02 18:24:43 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-01-02 18:24:43 +03:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
|
|
|
class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate, Serializable
|
|
|
|
{
|
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* An array containing the records of this collection.
|
|
|
|
*
|
|
|
|
* @var array
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
protected $data = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
/**
|
|
|
|
* The mapper object used to map the records of this collection to the database.
|
|
|
|
*
|
|
|
|
* @var Doctrine_Mapper
|
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_mapper;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* A snapshot of the fetched data.
|
|
|
|
*
|
|
|
|
* @var array
|
2007-05-16 23:20:55 +04:00
|
|
|
*/
|
|
|
|
protected $_snapshot = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* This record this collection is attached to, if any.
|
|
|
|
*
|
|
|
|
* @var Doctrine_Record
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
protected $reference;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* The reference field of the collection.
|
|
|
|
*
|
|
|
|
* @var string $referenceField
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2007-05-16 23:20:55 +04:00
|
|
|
protected $referenceField;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* The relation this collection is related to, if any.
|
|
|
|
*
|
|
|
|
* @var Doctrine_Relation
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
protected $relation;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* The name of the column 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-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Helper variable. Used for fast null value testing.
|
|
|
|
*
|
|
|
|
* @var Doctrine_Null
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
protected static $null;
|
|
|
|
|
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Constructor.
|
|
|
|
* Creates a new collection that will hold instances of the type that is
|
|
|
|
* specified through the mapper. That means if the mapper is a mapper for
|
|
|
|
* the entity "User", then the resulting collection will be a collection of
|
|
|
|
* user objects.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
2008-01-12 22:49:11 +03:00
|
|
|
* @param Doctrine_Mapper|string $mapper The mapper used by the collection.
|
|
|
|
* @param string $keyColumn The field name that will be used as the key
|
|
|
|
* in the collection.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2008-03-23 14:30:29 +03:00
|
|
|
public function __construct($mapper, $keyField = null)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
if (is_string($mapper)) {
|
|
|
|
$mapper = Doctrine_Manager::getInstance()->getMapper($mapper);
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_mapper = $mapper;
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2008-03-23 14:30:29 +03:00
|
|
|
if ($keyField === null) {
|
|
|
|
$keyField = $mapper->getClassMetadata()->getBoundQueryPart('indexBy');
|
2007-10-06 01:18:40 +04:00
|
|
|
}
|
|
|
|
|
2008-03-23 14:30:29 +03:00
|
|
|
if ($keyField === null) {
|
|
|
|
$keyField = $mapper->getClassMetadata()->getAttribute(Doctrine::ATTR_COLL_KEY);
|
2007-12-30 23:43:31 +03:00
|
|
|
}
|
|
|
|
|
2008-03-23 14:30:29 +03:00
|
|
|
if ($keyField !== null) {
|
|
|
|
if ( ! $this->_mapper->getClassMetadata()->hasField($keyField)) {
|
|
|
|
throw new Doctrine_Collection_Exception("Invalid field '$keyField' can't be uses as key.");
|
|
|
|
}
|
|
|
|
$this->_keyField = $keyField;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* initNullObject
|
2008-01-12 22:49:11 +03:00
|
|
|
* Initializes the null object for this collection.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function initNullObject(Doctrine_Null $null)
|
|
|
|
{
|
|
|
|
self::$null = $null;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* getTable
|
2008-01-12 22:49:11 +03:00
|
|
|
* Returns the table of the mapper of the collection.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @return Doctrine_Table
|
|
|
|
*/
|
|
|
|
public function getTable()
|
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_mapper->getTable();
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getMapper
|
|
|
|
* Returns the mapper of this collection.
|
|
|
|
*
|
|
|
|
* @return Doctrine_Mapper
|
|
|
|
*/
|
|
|
|
public function getMapper()
|
|
|
|
{
|
|
|
|
return $this->_mapper;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-22 22:09:54 +04:00
|
|
|
/**
|
|
|
|
* setData
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
|
|
|
public function setData(array $data)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$this->data = $data;
|
2007-05-22 22:09:54 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Serializes the collection.
|
|
|
|
* This method is automatically called when this Doctrine_Collection is serialized.
|
|
|
|
*
|
|
|
|
* Part of the implementation of the Serializable interface.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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']);
|
|
|
|
|
2008-02-15 03:57:34 +03:00
|
|
|
$vars['_mapper'] = $vars['_mapper']->getComponentName();
|
2007-01-02 18:24:43 +03:00
|
|
|
|
|
|
|
return serialize($vars);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Reconstitutes the collection object from it's serialized form.
|
|
|
|
* This method is automatically called everytime a Doctrine_Collection object is unserialized.
|
|
|
|
*
|
|
|
|
* Part of the implementation of the Serializable interface.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
2008-02-15 03:57:34 +03:00
|
|
|
* @param string $serialized The serialized data
|
|
|
|
*
|
2007-01-02 18:24:43 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
$manager = Doctrine_Manager::getInstance();
|
|
|
|
$connection = $manager->getCurrentConnection();
|
2007-01-02 18:24:43 +03:00
|
|
|
|
|
|
|
$array = unserialize($serialized);
|
|
|
|
|
|
|
|
foreach ($array as $name => $values) {
|
|
|
|
$this->$name = $values;
|
|
|
|
}
|
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_mapper = $connection->getMapper($this->_mapper);
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2007-11-08 23:52:49 +03:00
|
|
|
$keyColumn = isset($array['keyColumn']) ? $array['keyColumn'] : null;
|
2007-10-06 01:18:40 +04:00
|
|
|
if ($keyColumn === null) {
|
2008-02-20 23:54:20 +03:00
|
|
|
$keyColumn = $this->_mapper->getClassMetadata()->getBoundQueryPart('indexBy');
|
2007-10-06 01:18:40 +04:00
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
if ($keyColumn !== null) {
|
2008-03-23 14:30:29 +03:00
|
|
|
$this->_keyField = $keyColumn;
|
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-03-23 14:30:29 +03:00
|
|
|
* setKeyField
|
2007-05-16 23:20:55 +04:00
|
|
|
* 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-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-03-23 14:30:29 +03:00
|
|
|
* getKeyField
|
2007-01-02 18:24:43 +03:00
|
|
|
* returns the name of the key column
|
|
|
|
*
|
|
|
|
* @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-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
* getData
|
2007-01-02 18:24:43 +03:00
|
|
|
* returns all the records as an array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* getFirst
|
|
|
|
* returns the first record in the collection
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getFirst()
|
|
|
|
{
|
|
|
|
return reset($this->data);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* getLast
|
|
|
|
* returns the last record in the collection
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getLast()
|
|
|
|
{
|
|
|
|
return end($this->data);
|
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2007-10-22 20:38:03 +04:00
|
|
|
/**
|
|
|
|
* returns the last record in the collection
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function end()
|
|
|
|
{
|
|
|
|
return end($this->data);
|
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2007-10-22 20:38:03 +04:00
|
|
|
/**
|
|
|
|
* returns the current key
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return key($this->data);
|
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* setReference
|
|
|
|
* sets a reference pointer
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-17 01:54:59 +03:00
|
|
|
public function setReference(Doctrine_Record $record, Doctrine_Relation $relation)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2007-12-01 14:18:43 +03:00
|
|
|
$this->reference = $record;
|
|
|
|
$this->relation = $relation;
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
if ($relation instanceof Doctrine_Relation_ForeignKey ||
|
2007-12-01 14:18:43 +03:00
|
|
|
$relation instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
$this->referenceField = $relation->getForeignFieldName();
|
|
|
|
$value = $record->get($relation->getLocalFieldName());
|
2007-05-24 00:33:47 +04:00
|
|
|
foreach ($this->data as $record) {
|
2007-01-02 18:24:43 +03:00
|
|
|
if ($value !== null) {
|
2007-05-16 23:20:55 +04:00
|
|
|
$record->set($this->referenceField, $value, false);
|
2007-01-02 18:24:43 +03:00
|
|
|
} else {
|
2007-05-16 23:20:55 +04:00
|
|
|
$record->set($this->referenceField, $this->reference, false);
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
} else if ($relation instanceof Doctrine_Relation_Association) {
|
2007-01-02 18:24:43 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* getReference
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getReference()
|
|
|
|
{
|
|
|
|
return $this->reference;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* remove
|
|
|
|
* removes a specified collection element
|
|
|
|
*
|
|
|
|
* @param mixed $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function remove($key)
|
|
|
|
{
|
|
|
|
$removed = $this->data[$key];
|
|
|
|
unset($this->data[$key]);
|
|
|
|
return $removed;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* contains
|
|
|
|
* whether or not this collection contains a specified element
|
|
|
|
*
|
|
|
|
* @param mixed $key the key of the element
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function contains($key)
|
|
|
|
{
|
|
|
|
return isset($this->data[$key]);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2007-06-05 02:38:39 +04:00
|
|
|
public function search(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
return array_search($record, $this->data, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* get
|
|
|
|
* 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
|
|
|
|
* to the given key
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
2007-09-07 00:38:22 +04:00
|
|
|
if ( ! isset($this->data[$key])) {
|
2008-01-05 22:55:56 +03:00
|
|
|
$record = $this->_mapper->create();
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if (isset($this->referenceField)) {
|
2007-12-01 14:18:43 +03:00
|
|
|
$value = $this->reference->get($this->relation->getLocalFieldName());
|
2007-05-24 00:33:47 +04:00
|
|
|
|
|
|
|
if ($value !== null) {
|
|
|
|
$record->set($this->referenceField, $value, false);
|
|
|
|
} else {
|
|
|
|
$record->set($this->referenceField, $this->reference, false);
|
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2007-09-07 00:38:22 +04:00
|
|
|
if ($key === null) {
|
|
|
|
$this->data[] = $record;
|
|
|
|
} else {
|
|
|
|
$this->data[$key] = $record;
|
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
|
2008-03-23 14:30:29 +03:00
|
|
|
if (isset($this->_keyField)) {
|
|
|
|
$record->set($this->_keyField, $key);
|
2007-10-06 01:18:40 +04:00
|
|
|
}
|
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
return $record;
|
2007-09-03 18:57:18 +04:00
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
|
|
|
|
return $this->data[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Returns the primary keys of all records in the collection.
|
|
|
|
*
|
|
|
|
* @return array An array containing all primary keys.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
public function getPrimaryKeys()
|
|
|
|
{
|
|
|
|
$list = array();
|
2008-02-24 23:31:49 +03:00
|
|
|
$idFieldNames = (array)$this->_mapper->getClassMetadata()->getIdentifier();
|
2007-01-02 18:24:43 +03:00
|
|
|
|
|
|
|
foreach ($this->data as $record) {
|
2008-02-24 23:31:49 +03:00
|
|
|
if (is_array($record)) {
|
|
|
|
if (count($idFieldNames) > 1) {
|
|
|
|
$id = array();
|
|
|
|
foreach ($idFieldNames as $fieldName) {
|
|
|
|
if (isset($record[$fieldName])) {
|
|
|
|
$id[] = $record[$fieldName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$list[] = $id;
|
|
|
|
} else {
|
|
|
|
$idField = $idFieldNames[0];
|
|
|
|
if (isset($record[$idField])) {
|
|
|
|
$list[] = $record[$idField];
|
|
|
|
}
|
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
} else {
|
2008-02-24 23:31:49 +03:00
|
|
|
// @todo does not take composite keys into account
|
2007-01-02 18:24:43 +03:00
|
|
|
$list[] = $record->getIncremented();
|
|
|
|
}
|
2007-05-16 23:20:55 +04:00
|
|
|
}
|
2008-02-24 23:31:49 +03:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
return $list;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* returns all keys
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getKeys()
|
|
|
|
{
|
|
|
|
return array_keys($this->data);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Returns the number of records in this collection.
|
|
|
|
*
|
|
|
|
* Implementation of the Countable interface.
|
2007-01-02 18:24:43 +03:00
|
|
|
*
|
2008-01-12 22:49:11 +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-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* set
|
|
|
|
* @param integer $key
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
* @return void
|
2008-03-05 14:24:33 +03:00
|
|
|
* @internal Can't type-hint the second parameter to Doctrine_Record because we need
|
|
|
|
* to adhere to the Doctrine_Access::set() signature.
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
2008-02-14 23:20:35 +03:00
|
|
|
public function set($key, $record)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-02-28 18:30:55 +03:00
|
|
|
if ( ! $record instanceOf Doctrine_Record) {
|
2008-03-05 14:24:33 +03:00
|
|
|
throw new Doctrine_Collection_Exception('Value variable in set is not an instance of Doctrine_Record');
|
2008-02-14 23:20:35 +03:00
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if (isset($this->referenceField)) {
|
|
|
|
$record->set($this->referenceField, $this->reference, false);
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
|
|
|
$this->data[$key] = $record;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* adds a record to collection
|
|
|
|
* @param Doctrine_Record $record record to be added
|
|
|
|
* @param string $key optional key for the record
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-02-14 23:20:35 +03:00
|
|
|
public function add($record, $key = null)
|
2007-01-02 18:24:43 +03:00
|
|
|
{
|
2008-02-24 19:54:02 +03:00
|
|
|
if ( ! $record instanceof Doctrine_Record) {
|
|
|
|
throw new Doctrine_Record_Exception('Value variable in set is not an instance of Doctrine_Record.');
|
2008-02-14 23:20:35 +03:00
|
|
|
}
|
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
if (isset($this->referenceField)) {
|
2007-12-01 14:18:43 +03:00
|
|
|
$value = $this->reference->get($this->relation->getLocalFieldName());
|
2007-05-24 00:33:47 +04:00
|
|
|
|
|
|
|
if ($value !== null) {
|
|
|
|
$record->set($this->referenceField, $value, false);
|
|
|
|
} else {
|
|
|
|
$record->set($this->referenceField, $this->reference, false);
|
|
|
|
}
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
/*
|
2007-01-02 18:24:43 +03:00
|
|
|
* 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 ($val === $record) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($key)) {
|
|
|
|
if (isset($this->data[$key])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->data[$key] = $record;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-15 03:57:34 +03:00
|
|
|
// why is this not checked when the keyColumn is set?
|
2008-03-23 14:30:29 +03:00
|
|
|
if (isset($this->_keyField)) {
|
|
|
|
$value = $record->get($this->_keyField);
|
2007-01-02 18:24:43 +03:00
|
|
|
if ($value === null) {
|
2008-03-23 14:30:29 +03:00
|
|
|
throw new Doctrine_Collection_Exception("Couldn't create collection index. Record field '".$this->_keyField."' was null.");
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
|
|
|
$this->data[$value] = $record;
|
|
|
|
} else {
|
|
|
|
$this->data[] = $record;
|
|
|
|
}
|
2008-02-24 19:54:02 +03:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* loadRelated
|
|
|
|
*
|
|
|
|
* @param mixed $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function loadRelated($name = null)
|
|
|
|
{
|
2007-02-07 19:31:28 +03:00
|
|
|
$list = array();
|
2008-01-12 22:49:11 +03:00
|
|
|
$query = new Doctrine_Query($this->_mapper->getConnection());
|
2007-01-02 18:24:43 +03:00
|
|
|
|
|
|
|
if ( ! isset($name)) {
|
|
|
|
foreach ($this->data as $record) {
|
|
|
|
$value = $record->getIncremented();
|
|
|
|
if ($value !== null) {
|
|
|
|
$list[] = $value;
|
|
|
|
}
|
2007-05-23 00:47:27 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
$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) {
|
|
|
|
$value = $record->getIncremented();
|
|
|
|
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
|
|
|
|
2008-01-12 22:49:11 +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-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* populateRelated
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param Doctrine_Collection $coll
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function populateRelated($name, Doctrine_Collection $coll)
|
|
|
|
{
|
2008-01-05 22:55:56 +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) {
|
|
|
|
foreach ($coll as $k => $related) {
|
|
|
|
if ($related[$foreign] == $record[$local]) {
|
|
|
|
$this->data[$key]->setRelated($name, $related);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
} else if ($rel instanceof Doctrine_Relation_ForeignKey) {
|
2007-01-02 18:24:43 +03:00
|
|
|
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;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
$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);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
} else if ($rel instanceof Doctrine_Relation_Association) {
|
2008-02-24 23:31:49 +03:00
|
|
|
// @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;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
$sub = new Doctrine_Collection($rel->getForeignComponentName());
|
2007-01-02 18:24:43 +03:00
|
|
|
foreach ($coll as $k => $related) {
|
2008-02-24 23:31:49 +03:00
|
|
|
$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-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* getNormalIterator
|
|
|
|
* returns normal iterator - an iterator that will not expand this collection
|
|
|
|
*
|
|
|
|
* @return Doctrine_Iterator_Normal
|
|
|
|
*/
|
|
|
|
public function getNormalIterator()
|
|
|
|
{
|
|
|
|
return new Doctrine_Collection_Iterator_Normal($this);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* takeSnapshot
|
|
|
|
* 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
|
|
|
|
* snapshot.
|
|
|
|
*
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
|
|
|
public function takeSnapshot()
|
|
|
|
{
|
|
|
|
$this->_snapshot = $this->data;
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* getSnapshot
|
|
|
|
* returns the data of the last snapshot
|
|
|
|
*
|
|
|
|
* @return array returns the data in last snapshot
|
|
|
|
*/
|
|
|
|
public function getSnapshot()
|
|
|
|
{
|
|
|
|
return $this->_snapshot;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-16 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* processDiff
|
|
|
|
* processes the difference of the last snapshot and the current data
|
|
|
|
*
|
|
|
|
* an example:
|
|
|
|
* Snapshot with the objects 1, 2 and 4
|
|
|
|
* Current data with objects 2, 3 and 5
|
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* The process would remove objects 1 and 4
|
2007-05-16 23:20:55 +04:00
|
|
|
*
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
|
|
|
public function processDiff()
|
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
foreach (array_udiff($this->_snapshot, $this->data, array($this, "_compareRecords")) as $record) {
|
2007-05-16 23:20:55 +04:00
|
|
|
$record->delete();
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-08-28 20:45:22 +04:00
|
|
|
/**
|
|
|
|
* toArray
|
|
|
|
* Mimics the result of a $query->execute(array(), Doctrine::FETCH_ARRAY);
|
|
|
|
*
|
|
|
|
* @param boolean $deep
|
|
|
|
*/
|
2007-09-22 05:32:48 +04:00
|
|
|
public function toArray($deep = false, $prefixKey = false)
|
2007-07-31 02:30:41 +04:00
|
|
|
{
|
2007-09-27 02:36:32 +04:00
|
|
|
$data = array();
|
2007-11-08 21:53:30 +03:00
|
|
|
foreach ($this as $key => $record) {
|
2007-09-27 02:36:32 +04:00
|
|
|
$key = $prefixKey ? get_class($record) . '_' .$key:$key;
|
|
|
|
$data[$key] = $record->toArray($deep, $prefixKey);
|
2007-08-28 20:45:22 +04:00
|
|
|
}
|
2007-09-27 02:36:32 +04:00
|
|
|
|
|
|
|
return $data;
|
2007-07-31 02:30:41 +04:00
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fromArray
|
|
|
|
*
|
|
|
|
* Populate a Doctrine_Collection from an array of data
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-11-27 21:23:13 +03:00
|
|
|
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) {
|
2007-11-27 21:23:13 +03:00
|
|
|
$this[$rowKey]->fromArray($row, $deep);
|
2007-09-22 01:40:54 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
2007-12-09 04:56:53 +03:00
|
|
|
/**
|
2008-01-23 11:04:54 +03:00
|
|
|
* synchronizeFromArray
|
2007-12-09 04:56:53 +03: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)
|
2007-12-09 04:56:53 +03:00
|
|
|
{
|
|
|
|
foreach ($this as $key => $record) {
|
|
|
|
if (isset($array[$key])) {
|
2008-01-23 11:04:54 +03:00
|
|
|
$record->synchronizeFromArray($array[$key]);
|
2007-12-09 04:56:53 +03:00
|
|
|
unset($array[$key]);
|
|
|
|
} else {
|
|
|
|
// remove records that don't exist in the array
|
|
|
|
$this->remove($key);
|
|
|
|
}
|
|
|
|
}
|
2008-01-23 11:04:54 +03:00
|
|
|
|
2007-12-09 04:56:53 +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
|
|
|
/**
|
|
|
|
* exportTo
|
|
|
|
*
|
|
|
|
* Export a Doctrine_Collection to one of the supported Doctrine_Parser formats
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $deep
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-27 02:36:32 +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);
|
|
|
|
}
|
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* importFrom
|
|
|
|
*
|
|
|
|
* Import data to a Doctrine_Collection from one of the supported Doctrine_Parser formats
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-27 02:36:32 +04:00
|
|
|
public function importFrom($type, $data)
|
|
|
|
{
|
|
|
|
if ($type == 'array') {
|
|
|
|
return $this->fromArray($data);
|
|
|
|
} else {
|
|
|
|
return $this->fromArray(Doctrine_Parser::load($data, $type));
|
|
|
|
}
|
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getDeleteDiff
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-17 01:28:33 +04:00
|
|
|
public function getDeleteDiff()
|
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* getInsertDiff
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-17 01:28:33 +04:00
|
|
|
public function getInsertDiff()
|
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
return array_udiff($this->data, $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
|
|
|
* _compareRecords
|
|
|
|
* Compares two records. To be used on _snapshot diffs using array_udiff.
|
2007-09-10 20:16:24 +04:00
|
|
|
*/
|
2008-01-12 22:49:11 +03:00
|
|
|
protected function _compareRecords($a, $b)
|
2007-09-10 20:16:24 +04:00
|
|
|
{
|
2007-11-16 01:45:09 +03:00
|
|
|
if ($a->getOid() == $b->getOid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-10 20:16:24 +04:00
|
|
|
return ($a->getOid() > $b->getOid()) ? 1 : -1;
|
2007-05-17 01:28:33 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* save
|
2008-01-12 22:49:11 +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
|
|
|
*
|
2007-05-23 00:47:27 +04: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
|
|
|
*/
|
|
|
|
public function save(Doctrine_Connection $conn = null)
|
|
|
|
{
|
|
|
|
if ($conn == null) {
|
2008-01-05 22:55:56 +03:00
|
|
|
$conn = $this->_mapper->getConnection();
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-11-16 01:45:09 +03:00
|
|
|
|
2008-01-05 22:55:56 +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;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
2007-05-16 23:20:55 +04:00
|
|
|
* delete
|
2007-01-02 18:24:43 +03:00
|
|
|
* single shot delete
|
|
|
|
* deletes all records from this collection
|
|
|
|
* and uses only one database query to perform this operation
|
|
|
|
*
|
2007-05-16 23:20:55 +04:00
|
|
|
* @return Doctrine_Collection
|
2007-01-02 18:24:43 +03:00
|
|
|
*/
|
|
|
|
public function delete(Doctrine_Connection $conn = null)
|
|
|
|
{
|
|
|
|
if ($conn == null) {
|
2008-01-05 22:55:56 +03:00
|
|
|
$conn = $this->_mapper->getConnection();
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
|
|
|
|
2008-01-05 22:55:56 +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
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
$conn->commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$conn->rollback();
|
|
|
|
throw $e;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->data = array();
|
2007-05-16 23:20:55 +04:00
|
|
|
return $this;
|
2007-01-02 18:24:43 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2008-02-29 22:33:31 +03:00
|
|
|
|
|
|
|
public function free($deep = false)
|
|
|
|
{
|
|
|
|
foreach ($this->getData() as $key => $record) {
|
|
|
|
if ( ! ($record instanceof Doctrine_Null)) {
|
|
|
|
$record->free($deep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data = array();
|
|
|
|
|
|
|
|
if ($this->reference) {
|
|
|
|
$this->reference->free($deep);
|
|
|
|
$this->reference = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* getIterator
|
|
|
|
* @return object ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
$data = $this->data;
|
|
|
|
return new ArrayIterator($data);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-02 18:24:43 +03:00
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return Doctrine_Lib::getCollectionAsString($this);
|
|
|
|
}
|
2007-11-05 21:05:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the relation object
|
|
|
|
* @return object Doctrine_Relation
|
|
|
|
*/
|
|
|
|
public function getRelation()
|
|
|
|
{
|
|
|
|
return $this->relation;
|
|
|
|
}
|
2007-10-06 01:18:40 +04:00
|
|
|
}
|