2006-05-30 12:42:10 +04:00
|
|
|
<?php
|
2006-07-27 21:51:19 +04:00
|
|
|
/*
|
2006-07-26 21:09:00 +04:00
|
|
|
* $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.com>.
|
|
|
|
*/
|
|
|
|
Doctrine::autoload("Access");
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Collection
|
|
|
|
* Collection of Doctrine_Record objects.
|
|
|
|
*
|
|
|
|
* @package Doctrine ORM
|
|
|
|
* @url www.phpdoctrine.com
|
|
|
|
* @license LGPL
|
|
|
|
*/
|
2006-06-05 13:57:53 +04:00
|
|
|
class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate, Serializable {
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @var array $data an array containing the data access objects of this collection
|
|
|
|
*/
|
|
|
|
protected $data = array();
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Table $table each collection has only records of specified table
|
|
|
|
*/
|
|
|
|
protected $table;
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Record $reference collection can belong to a record
|
|
|
|
*/
|
|
|
|
protected $reference;
|
|
|
|
/**
|
|
|
|
* @var string $reference_field the reference field of the collection
|
|
|
|
*/
|
|
|
|
protected $reference_field;
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Relation the record this collection is related to, if any
|
|
|
|
*/
|
|
|
|
protected $relation;
|
|
|
|
/**
|
|
|
|
* @var boolean $expandable whether or not this collection has been expanded
|
|
|
|
*/
|
2006-07-22 05:19:51 +04:00
|
|
|
protected $expandable = true;
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @var array $expanded
|
|
|
|
*/
|
|
|
|
protected $expanded = array();
|
|
|
|
/**
|
|
|
|
* @var mixed $generator
|
|
|
|
*/
|
|
|
|
protected $generator;
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Null $null used for extremely fast SQL null value testing
|
|
|
|
*/
|
|
|
|
protected static $null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*/
|
|
|
|
public function __construct(Doctrine_Table $table) {
|
|
|
|
$this->table = $table;
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$name = $table->getAttribute(Doctrine::ATTR_COLL_KEY);
|
|
|
|
if($name !== null) {
|
|
|
|
$this->generator = new Doctrine_IndexGenerator($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* initNullObject
|
|
|
|
*/
|
|
|
|
public static function initNullObject(Doctrine_Null $null) {
|
|
|
|
self::$null = $null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return object Doctrine_Table
|
|
|
|
*/
|
|
|
|
public function getTable() {
|
|
|
|
return $this->table;
|
|
|
|
}
|
2006-06-05 13:57:53 +04:00
|
|
|
/**
|
|
|
|
* this method is automatically called when this Doctrine_Collection is serialized
|
|
|
|
*
|
|
|
|
* @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']);
|
|
|
|
|
|
|
|
$vars['table'] = $vars['table']->getComponentName();
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-06-05 13:57:53 +04:00
|
|
|
return serialize($vars);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* unseralize
|
|
|
|
* this method is automatically called everytime a Doctrine_Collection object is unserialized
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function unserialize($serialized) {
|
|
|
|
$manager = Doctrine_Manager::getInstance();
|
|
|
|
$session = $manager->getCurrentSession();
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-06-05 13:57:53 +04:00
|
|
|
$array = unserialize($serialized);
|
|
|
|
|
|
|
|
foreach($array as $name => $values) {
|
|
|
|
$this->$name = $values;
|
|
|
|
}
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-06-08 14:25:41 +04:00
|
|
|
$this->table = $session->getTable($this->table);
|
2006-06-05 13:57:53 +04:00
|
|
|
|
|
|
|
$this->expanded = array();
|
|
|
|
$this->expandable = true;
|
|
|
|
|
2006-06-08 14:25:41 +04:00
|
|
|
$name = $this->table->getAttribute(Doctrine::ATTR_COLL_KEY);
|
2006-06-05 13:57:53 +04:00
|
|
|
if($name !== null) {
|
|
|
|
$this->generator = new Doctrine_IndexGenerator($name);
|
|
|
|
}
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* whether or not an offset batch has been expanded
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isExpanded($offset) {
|
|
|
|
return isset($this->expanded[$offset]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* whether or not this collection is expandable
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isExpandable() {
|
|
|
|
return $this->expandable;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Doctrine_IndexGenerator $generator
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setGenerator($generator) {
|
|
|
|
$this->generator = $generator;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return Doctrine_IndexGenerator
|
|
|
|
*/
|
|
|
|
public function getGenerator() {
|
|
|
|
return $this->generator;
|
|
|
|
}
|
2006-07-22 05:19:51 +04:00
|
|
|
/**
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getData() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function addData(array $data) {
|
|
|
|
$this->data[] = $data;
|
|
|
|
}
|
2006-07-22 05:19:51 +04:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getFirst() {
|
2006-07-22 10:39:46 +04:00
|
|
|
return reset($this->data);
|
2006-07-22 05:19:51 +04:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getLast() {
|
|
|
|
return end($this->data);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setReference(Doctrine_Record $record,Doctrine_Relation $relation) {
|
|
|
|
$this->reference = $record;
|
|
|
|
$this->relation = $relation;
|
|
|
|
|
|
|
|
if($relation instanceof Doctrine_ForeignKey ||
|
|
|
|
$relation instanceof Doctrine_LocalKey) {
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->reference_field = $relation->getForeign();
|
|
|
|
|
|
|
|
$value = $record->get($relation->getLocal());
|
|
|
|
|
|
|
|
foreach($this->getNormalIterator() as $record) {
|
|
|
|
if($value !== null) {
|
|
|
|
$record->rawSet($this->reference_field, $value);
|
|
|
|
} else {
|
|
|
|
$record->rawSet($this->reference_field, $this->reference);
|
|
|
|
}
|
|
|
|
}
|
2006-06-08 14:20:30 +04:00
|
|
|
} elseif($relation instanceof Doctrine_Association) {
|
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getReference() {
|
|
|
|
return $this->reference;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function expand($key) {
|
|
|
|
$where = array();
|
|
|
|
$params = array();
|
|
|
|
$limit = null;
|
|
|
|
$offset = null;
|
|
|
|
|
|
|
|
switch(get_class($this)):
|
|
|
|
case "Doctrine_Collection_Offset":
|
|
|
|
$limit = $this->getLimit();
|
|
|
|
$offset = (floor($key / $limit) * $limit);
|
|
|
|
|
|
|
|
if( ! $this->expandable && isset($this->expanded[$offset]))
|
|
|
|
return false;
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$fields = implode(", ",$this->table->getColumnNames());
|
|
|
|
break;
|
2006-07-22 05:19:51 +04:00
|
|
|
default:
|
2006-05-30 12:42:10 +04:00
|
|
|
if( ! $this->expandable)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( ! isset($this->reference))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$id = $this->reference->getID();
|
|
|
|
|
|
|
|
if(empty($id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch(get_class($this)):
|
|
|
|
case "Doctrine_Collection_Immediate":
|
|
|
|
$fields = implode(", ",$this->table->getColumnNames());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$fields = implode(", ",$this->table->getPrimaryKeys());
|
|
|
|
endswitch;
|
|
|
|
|
|
|
|
|
|
|
|
endswitch;
|
|
|
|
|
|
|
|
if(isset($this->relation)) {
|
|
|
|
if($this->relation instanceof Doctrine_ForeignKey) {
|
2006-07-10 13:18:09 +04:00
|
|
|
$params[] = $this->reference->getIncremented();
|
2006-05-30 12:42:10 +04:00
|
|
|
$where[] = $this->reference_field." = ?";
|
|
|
|
|
|
|
|
if( ! isset($offset)) {
|
|
|
|
$ids = $this->getPrimaryKeys();
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
if( ! empty($ids)) {
|
|
|
|
$where[] = $this->table->getIdentifier()." NOT IN (".substr(str_repeat("?, ",count($ids)),0,-2).")";
|
|
|
|
$params = array_merge($params,$ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->expandable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} elseif($this->relation instanceof Doctrine_Association) {
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-06-08 14:20:30 +04:00
|
|
|
$asf = $this->relation->getAssociationFactory();
|
2006-07-10 13:18:09 +04:00
|
|
|
$query = "SELECT ".$foreign." FROM ".$asf->getTableName()." WHERE ".$local."=".$this->getIncremented();
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$table = $fk->getTable();
|
2006-07-27 21:51:19 +04:00
|
|
|
$graph = new Doctrine_Query($table->getSession());
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$q = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$table->getIdentifier()." IN ($query)";
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = "SELECT ".$fields." FROM ".$this->table->getTableName();
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
// apply column aggregation inheritance
|
|
|
|
foreach($this->table->getInheritanceMap() as $k => $v) {
|
|
|
|
$where[] = $k." = ?";
|
|
|
|
$params[] = $v;
|
|
|
|
}
|
|
|
|
if( ! empty($where)) {
|
|
|
|
$query .= " WHERE ".implode(" AND ",$where);
|
|
|
|
}
|
|
|
|
|
|
|
|
$coll = $this->table->execute($query, $params, $limit, $offset);
|
|
|
|
|
|
|
|
if( ! isset($offset)) {
|
|
|
|
foreach($coll as $record) {
|
|
|
|
if(isset($this->reference_field))
|
|
|
|
$record->rawSet($this->reference_field,$this->reference);
|
|
|
|
|
2006-06-26 22:55:42 +04:00
|
|
|
$this->reference->addReference($record, $this->relation);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$i = $offset;
|
|
|
|
|
|
|
|
foreach($coll as $record) {
|
|
|
|
if(isset($this->reference)) {
|
2006-06-26 22:55:42 +04:00
|
|
|
$this->reference->addReference($record, $this->relation, $i);
|
2006-05-30 12:42:10 +04:00
|
|
|
} else
|
|
|
|
$this->data[$i] = $record;
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->expanded[$offset] = true;
|
|
|
|
|
|
|
|
// check if the fetched collection's record count is smaller
|
|
|
|
// than the query limit, if so this collection has been expanded to its max size
|
|
|
|
|
|
|
|
if(count($coll) < $limit) {
|
|
|
|
$this->expandable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $coll;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function remove($key) {
|
2006-06-20 01:31:22 +04:00
|
|
|
if( ! isset($this->data[$key])) {
|
|
|
|
$this->expand($key);
|
2006-05-30 12:42:10 +04:00
|
|
|
throw new InvalidKeyException();
|
2006-06-20 01:31:22 +04:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
|
|
|
|
$removed = $this->data[$key];
|
2006-07-22 05:19:51 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
unset($this->data[$key]);
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function contains($key) {
|
|
|
|
return isset($this->data[$key]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed $key
|
|
|
|
* @return object Doctrine_Record return a specified record
|
|
|
|
*/
|
|
|
|
public function get($key) {
|
|
|
|
if( ! isset($this->data[$key])) {
|
|
|
|
$this->expand($key);
|
|
|
|
|
|
|
|
if( ! isset($this->data[$key]))
|
|
|
|
$this->data[$key] = $this->table->create();
|
|
|
|
|
|
|
|
if(isset($this->reference_field)) {
|
|
|
|
$value = $this->reference->get($this->relation->getLocal());
|
|
|
|
|
|
|
|
if($value !== null) {
|
|
|
|
$this->data[$key]->rawSet($this->reference_field, $value);
|
|
|
|
} else {
|
2006-07-10 13:18:09 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->data[$key]->rawSet($this->reference_field, $this->reference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->data[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array an array containing all primary keys
|
|
|
|
*/
|
|
|
|
public function getPrimaryKeys() {
|
|
|
|
$list = array();
|
|
|
|
$name = $this->table->getIdentifier();
|
|
|
|
|
|
|
|
foreach($this->data as $record):
|
|
|
|
if(is_array($record) && isset($record[$name])) {
|
|
|
|
$list[] = $record[$name];
|
|
|
|
} else {
|
2006-07-10 13:18:09 +04:00
|
|
|
$list[] = $record->getIncremented();
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
endforeach;
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns all keys
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getKeys() {
|
|
|
|
return array_keys($this->data);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* count
|
|
|
|
* this class implements interface countable
|
2006-06-14 23:17:38 +04:00
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return integer number of records in this collection
|
|
|
|
*/
|
|
|
|
public function count() {
|
|
|
|
return count($this->data);
|
|
|
|
}
|
|
|
|
/**
|
2006-07-22 05:19:51 +04:00
|
|
|
* set
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param integer $key
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set($key,Doctrine_Record $record) {
|
|
|
|
if(isset($this->reference_field))
|
|
|
|
$record->rawSet($this->reference_field,$this->reference);
|
|
|
|
|
|
|
|
$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
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function add(Doctrine_Record $record,$key = null) {
|
|
|
|
if(isset($this->reference_field))
|
|
|
|
$record->rawSet($this->reference_field,$this->reference);
|
|
|
|
|
2006-07-10 13:18:09 +04:00
|
|
|
if(in_array($record,$this->data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
if(isset($key)) {
|
|
|
|
if(isset($this->data[$key]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$this->data[$key] = $record;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($this->generator)) {
|
|
|
|
$key = $this->generator->getIndex($record);
|
|
|
|
$this->data[$key] = $record;
|
|
|
|
} else
|
|
|
|
$this->data[] = $record;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
2006-06-18 02:46:03 +04:00
|
|
|
* populate
|
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param Doctrine_Query $query
|
2006-07-22 05:19:51 +04:00
|
|
|
* @param integer $key
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-07-22 03:22:15 +04:00
|
|
|
public function populate(Doctrine_Hydrate $query) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$name = $this->table->getComponentName();
|
|
|
|
|
|
|
|
if($this instanceof Doctrine_Collection_Immediate ||
|
|
|
|
$this instanceof Doctrine_Collection_Offset) {
|
|
|
|
|
|
|
|
$data = $query->getData($name);
|
|
|
|
if(is_array($data)) {
|
|
|
|
foreach($data as $k=>$v):
|
|
|
|
$this->table->setData($v);
|
|
|
|
$this->add($this->table->getRecord());
|
|
|
|
endforeach;
|
|
|
|
}
|
|
|
|
} elseif($this instanceof Doctrine_Collection_Batch) {
|
|
|
|
$this->data = $query->getData($name);
|
|
|
|
|
|
|
|
if(isset($this->generator)) {
|
|
|
|
foreach($this->data as $k => $v) {
|
|
|
|
$record = $this->get($k);
|
|
|
|
$i = $this->generator->getIndex($record);
|
|
|
|
$this->data[$i] = $record;
|
|
|
|
unset($this->data[$k]);
|
|
|
|
}
|
2006-07-22 05:19:51 +04:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
}
|
2006-07-27 21:51:19 +04:00
|
|
|
/**
|
|
|
|
* loadRelated
|
|
|
|
*
|
2006-08-16 01:33:18 +04:00
|
|
|
* @param mixed $name
|
2006-07-27 21:51:19 +04:00
|
|
|
*/
|
2006-08-16 01:33:18 +04:00
|
|
|
public function loadRelated($name = null) {
|
|
|
|
$query = new Doctrine_Query($this->table->getSession());
|
2006-07-31 01:37:07 +04:00
|
|
|
|
2006-08-16 01:33:18 +04:00
|
|
|
if( ! isset($name))
|
|
|
|
return $query;
|
|
|
|
|
|
|
|
$rel = $this->table->getForeignKey($name);
|
|
|
|
$table = $rel->getTable();
|
2006-07-27 21:51:19 +04:00
|
|
|
$foreign = $rel->getForeign();
|
|
|
|
$local = $rel->getLocal();
|
|
|
|
|
|
|
|
$list = array();
|
|
|
|
if($rel instanceof Doctrine_LocalKey || $rel instanceof Doctrine_ForeignKey) {
|
|
|
|
foreach($this->data as $record):
|
|
|
|
$list[] = $record[$local];
|
|
|
|
endforeach;
|
|
|
|
} else {
|
|
|
|
foreach($this->data as $record):
|
|
|
|
$value = $record->getIncremented();
|
|
|
|
if($value !== null)
|
|
|
|
$list[] = $value;
|
|
|
|
endforeach;
|
|
|
|
}
|
2006-08-16 01:33:18 +04:00
|
|
|
$dql = $rel->getRelationDql(count($list), 'collection');
|
2006-07-27 21:51:19 +04:00
|
|
|
$coll = $query->query($dql, $list);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($rel instanceof Doctrine_LocalKey) {
|
|
|
|
foreach($this->data as $key => $record) {
|
|
|
|
foreach($coll as $k => $related) {
|
|
|
|
if($related[$foreign] == $record[$local]) {
|
|
|
|
$this->data[$key]->setRelated($name, $related);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif($rel instanceof Doctrine_ForeignKey) {
|
|
|
|
foreach($this->data as $key => $record) {
|
|
|
|
if($record->getState() == Doctrine_Record::STATE_TCLEAN ||
|
|
|
|
$record->getState() == Doctrine_Record::STATE_TDIRTY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$sub = new Doctrine_Collection($table);
|
|
|
|
|
|
|
|
foreach($coll as $k => $related) {
|
|
|
|
if($related[$foreign] == $record[$local]) {
|
|
|
|
$sub->add($related);
|
|
|
|
$coll->remove($k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data[$key]->setRelated($name, $sub);
|
|
|
|
}
|
|
|
|
} elseif($rel instanceof Doctrine_Association) {
|
|
|
|
$identifier = $this->table->getIdentifier();
|
2006-08-16 01:33:18 +04:00
|
|
|
$asf = $rel->getAssociationFactory();
|
2006-07-27 21:51:19 +04:00
|
|
|
|
|
|
|
foreach($this->data as $key => $record) {
|
|
|
|
if($record->getState() == Doctrine_Record::STATE_TCLEAN ||
|
|
|
|
$record->getState() == Doctrine_Record::STATE_TDIRTY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$sub = new Doctrine_Collection($table);
|
|
|
|
$association = $asf->getComponentName();
|
|
|
|
|
|
|
|
foreach($coll as $k => $related) {
|
|
|
|
if($related[$association][0]->get($local) == $record[$identifier]) {
|
|
|
|
$sub->add($related);
|
|
|
|
$coll->remove($k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data[$key]->setRelated($name, $sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-06-18 02:46:03 +04:00
|
|
|
* getNormalIterator
|
|
|
|
* returns normal iterator - an iterator that will not expand this collection
|
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return Doctrine_Iterator_Normal
|
|
|
|
*/
|
|
|
|
public function getNormalIterator() {
|
|
|
|
return new Doctrine_Iterator_Normal($this);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* save
|
|
|
|
* saves all records
|
2006-06-18 02:46:03 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
public function save() {
|
|
|
|
$this->table->getSession()->saveCollection($this);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* single shot delete
|
|
|
|
* deletes all records from this collection
|
|
|
|
* uses only one database query to perform this operation
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function delete() {
|
|
|
|
$ids = $this->table->getSession()->deleteCollection($this);
|
|
|
|
$this->data = array();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getIterator
|
|
|
|
* @return object ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
|
|
|
$data = $this->data;
|
|
|
|
return new ArrayIterator($data);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return Doctrine_Lib::getCollectionAsString($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|