This commit is contained in:
parent
4b5731baf5
commit
b3cf15a1dd
766
draft/new-core/Collection.php
Normal file
766
draft/new-core/Collection.php
Normal file
@ -0,0 +1,766 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id: Collection.php 1207 2007-03-31 19:49:23Z zYne $
|
||||
*
|
||||
* 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("Doctrine_Access");
|
||||
/**
|
||||
* Doctrine_Collection
|
||||
* Collection of Doctrine_Record objects.
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision: 1207 $
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate, Serializable
|
||||
{
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
protected $expandable = true;
|
||||
/**
|
||||
* @var array $expanded
|
||||
*/
|
||||
protected $expanded = array();
|
||||
/**
|
||||
* @var string $keyColumn the name of the column that is used for collection key mapping
|
||||
*/
|
||||
protected $keyColumn;
|
||||
/**
|
||||
* @var Doctrine_Null $null used for extremely fast null value testing
|
||||
*/
|
||||
protected static $null;
|
||||
|
||||
protected $aggregateValues = array();
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param Doctrine_Table|string $table
|
||||
*/
|
||||
public function __construct($table)
|
||||
{
|
||||
if ( ! ($table instanceof Doctrine_Table)) {
|
||||
$table = Doctrine_Manager::getInstance()
|
||||
->getTable($table);
|
||||
}
|
||||
$this->table = $table;
|
||||
|
||||
$name = $table->getAttribute(Doctrine::ATTR_COLL_KEY);
|
||||
if ($name !== null) {
|
||||
$this->keyColumn = $name;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* initNullObject
|
||||
* initializes the null object for this collection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function initNullObject(Doctrine_Null $null)
|
||||
{
|
||||
self::$null = $null;
|
||||
}
|
||||
/**
|
||||
* getTable
|
||||
* returns the table this collection belongs to
|
||||
*
|
||||
* @return Doctrine_Table
|
||||
*/
|
||||
public function getTable()
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
/**
|
||||
* setAggregateValue
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return void
|
||||
*/
|
||||
public function setAggregateValue($name, $value)
|
||||
{
|
||||
$this->aggregateValues[$name] = $value;
|
||||
}
|
||||
/**
|
||||
* getAggregateValue
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAggregateValue($name)
|
||||
{
|
||||
return $this->aggregateValues[$name];
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
|
||||
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();
|
||||
$connection = $manager->getCurrentConnection();
|
||||
|
||||
$array = unserialize($serialized);
|
||||
|
||||
foreach ($array as $name => $values) {
|
||||
$this->$name = $values;
|
||||
}
|
||||
|
||||
$this->table = $connection->getTable($this->table);
|
||||
|
||||
$this->expanded = array();
|
||||
$this->expandable = true;
|
||||
|
||||
$name = $this->table->getAttribute(Doctrine::ATTR_COLL_KEY);
|
||||
if ($name !== null) {
|
||||
$this->keyColumn = $name;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* isExpanded
|
||||
*
|
||||
* whether or not an offset batch has been expanded
|
||||
* @return boolean
|
||||
*/
|
||||
public function isExpanded($offset)
|
||||
{
|
||||
return isset($this->expanded[$offset]);
|
||||
}
|
||||
/**
|
||||
* isExpandable
|
||||
*
|
||||
* whether or not this collection is expandable
|
||||
* @return boolean
|
||||
*/
|
||||
public function isExpandable()
|
||||
{
|
||||
return $this->expandable;
|
||||
}
|
||||
/**
|
||||
* setKeyColumn
|
||||
*
|
||||
* @param string $column
|
||||
* @return void
|
||||
*/
|
||||
public function setKeyColumn($column)
|
||||
{
|
||||
$this->keyColumn = $column;
|
||||
}
|
||||
/**
|
||||
* getKeyColumn
|
||||
* returns the name of the key column
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getKeyColumn()
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
/**
|
||||
* returns all the records as an array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
public function addData(array $data)
|
||||
{
|
||||
$this->data[] = $data;
|
||||
}
|
||||
/**
|
||||
* getFirst
|
||||
* returns the first record in the collection
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFirst()
|
||||
{
|
||||
return reset($this->data);
|
||||
}
|
||||
/**
|
||||
* getLast
|
||||
* returns the last record in the collection
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLast()
|
||||
{
|
||||
return end($this->data);
|
||||
}
|
||||
/**
|
||||
* setReference
|
||||
* sets a reference pointer
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setReference(Doctrine_Record $record, Doctrine_Relation $relation)
|
||||
{
|
||||
$this->reference = $record;
|
||||
$this->relation = $relation;
|
||||
|
||||
if ($relation instanceof Doctrine_Relation_ForeignKey
|
||||
|| $relation instanceof Doctrine_Relation_LocalKey
|
||||
) {
|
||||
|
||||
$this->reference_field = $relation->getForeign();
|
||||
|
||||
$value = $record->get($relation->getLocal());
|
||||
|
||||
foreach ($this->getNormalIterator() as $record) {
|
||||
if ($value !== null) {
|
||||
$record->set($this->reference_field, $value, false);
|
||||
} else {
|
||||
$record->set($this->reference_field, $this->reference, false);
|
||||
}
|
||||
}
|
||||
} elseif ($relation instanceof Doctrine_Relation_Association) {
|
||||
|
||||
}
|
||||
}
|
||||
/**
|
||||
* getReference
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getReference()
|
||||
{
|
||||
return $this->reference;
|
||||
}
|
||||
/**
|
||||
* expand
|
||||
* expands the collection
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
$fields = implode(", ",$this->table->getColumnNames());
|
||||
break;
|
||||
default:
|
||||
if ( ! $this->expandable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! isset($this->reference)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$id = $this->reference->obtainIdentifier();
|
||||
|
||||
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());
|
||||
};
|
||||
};
|
||||
|
||||
if (isset($this->relation)) {
|
||||
if ($this->relation instanceof Doctrine_Relation_ForeignKey) {
|
||||
$params[] = $this->reference->getIncremented();
|
||||
$where[] = $this->reference_field." = ?";
|
||||
|
||||
if ( ! isset($offset)) {
|
||||
$ids = $this->getPrimaryKeys();
|
||||
|
||||
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_Relation_Association) {
|
||||
|
||||
$asf = $this->relation->getAssociationFactory();
|
||||
$query = 'SELECT '.$foreign." FROM ".$asf->getTableName()." WHERE ".$local."=".$this->getIncremented();
|
||||
|
||||
$table = $fk->getTable();
|
||||
$graph = new Doctrine_Query($table->getConnection());
|
||||
|
||||
$q = 'FROM ' . $table->getComponentName() . ' WHERE ' . $table->getComponentName() . '.' . $table->getIdentifier()." IN ($query)";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$query = "SELECT ".$fields." FROM ".$this->table->getTableName();
|
||||
|
||||
// apply column aggregation inheritance
|
||||
$map = $this->table->inheritanceMap;
|
||||
foreach ($map 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->set($this->reference_field,$this->reference, false);
|
||||
}
|
||||
$this->reference->addReference($record, $this->relation);
|
||||
}
|
||||
} else {
|
||||
$i = $offset;
|
||||
|
||||
foreach ($coll as $record) {
|
||||
if (isset($this->reference)) {
|
||||
$this->reference->addReference($record, $this->relation, $i);
|
||||
} else {
|
||||
$this->data[$i] = $record;
|
||||
}
|
||||
$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;
|
||||
}
|
||||
/**
|
||||
* remove
|
||||
* removes a specified collection element
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
if ( ! isset($this->data[$key])) {
|
||||
$this->expand($key);
|
||||
|
||||
throw new Doctrine_Collection_Exception('Unknown key ' . $key);
|
||||
}
|
||||
|
||||
$removed = $this->data[$key];
|
||||
|
||||
unset($this->data[$key]);
|
||||
return $removed;
|
||||
}
|
||||
/**
|
||||
* 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]);
|
||||
}
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if ($key === null) {
|
||||
$record = $this->table->create();
|
||||
|
||||
if (isset($this->reference_field)) {
|
||||
$record->set($this->reference_field, $this->reference, false);
|
||||
}
|
||||
|
||||
$this->data[] = $record;
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
|
||||
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]->set($this->reference_field, $value, false);
|
||||
} else {
|
||||
|
||||
$this->data[$key]->set($this->reference_field, $this->reference, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
$list[] = $record->getIncremented();
|
||||
}
|
||||
};
|
||||
return $list;
|
||||
}
|
||||
/**
|
||||
* returns all keys
|
||||
* @return array
|
||||
*/
|
||||
public function getKeys()
|
||||
{
|
||||
return array_keys($this->data);
|
||||
}
|
||||
/**
|
||||
* count
|
||||
* this class implements interface countable
|
||||
* returns the number of records in this collection
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
/**
|
||||
* set
|
||||
* @param integer $key
|
||||
* @param Doctrine_Record $record
|
||||
* @return void
|
||||
*/
|
||||
public function set($key, Doctrine_Record $record)
|
||||
{
|
||||
if (isset($this->reference_field)) {
|
||||
$record->set($this->reference_field, $this->reference, false);
|
||||
}
|
||||
$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->set($this->reference_field, $this->reference, false);
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
if (isset($this->keyColumn)) {
|
||||
$value = $record->get($this->keyColumn);
|
||||
if ($value === null) {
|
||||
throw new Doctrine_Collection_Exception("Couldn't create collection index. Record field '".$this->keyColumn."' was null.");
|
||||
}
|
||||
$this->data[$value] = $record;
|
||||
} else {
|
||||
$this->data[] = $record;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* loadRelated
|
||||
*
|
||||
* @param mixed $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function loadRelated($name = null)
|
||||
{
|
||||
$list = array();
|
||||
$query = new Doctrine_Query($this->table->getConnection());
|
||||
|
||||
if ( ! isset($name)) {
|
||||
foreach ($this->data as $record) {
|
||||
$value = $record->getIncremented();
|
||||
if ($value !== null) {
|
||||
$list[] = $value;
|
||||
}
|
||||
};
|
||||
$query->from($this->table->getComponentName() . '(' . implode(", ",$this->table->getPrimaryKeys()) . ')');
|
||||
$query->where($this->table->getComponentName() . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')');
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
$rel = $this->table->getRelation($name);
|
||||
|
||||
if ($rel instanceof Doctrine_Relation_LocalKey || $rel instanceof Doctrine_Relation_ForeignKey) {
|
||||
foreach ($this->data as $record) {
|
||||
$list[] = $record[$rel->getLocal()];
|
||||
}
|
||||
} else {
|
||||
foreach ($this->data as $record) {
|
||||
$value = $record->getIncremented();
|
||||
if ($value !== null) {
|
||||
$list[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$dql = $rel->getRelationDql(count($list), 'collection');
|
||||
|
||||
$coll = $query->query($dql, $list);
|
||||
|
||||
$this->populateRelated($name, $coll);
|
||||
}
|
||||
/**
|
||||
* populateRelated
|
||||
*
|
||||
* @param string $name
|
||||
* @param Doctrine_Collection $coll
|
||||
* @return void
|
||||
*/
|
||||
public function populateRelated($name, Doctrine_Collection $coll)
|
||||
{
|
||||
$rel = $this->table->getRelation($name);
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($rel instanceof Doctrine_Relation_ForeignKey) {
|
||||
foreach ($this->data as $key => $record) {
|
||||
if ($record->state() == Doctrine_Record::STATE_TCLEAN
|
||||
|| $record->state() == 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_Relation_Association) {
|
||||
$identifier = $this->table->getIdentifier();
|
||||
$asf = $rel->getAssociationFactory();
|
||||
$name = $table->getComponentName();
|
||||
|
||||
foreach ($this->data as $key => $record) {
|
||||
if ($record->state() == Doctrine_Record::STATE_TCLEAN
|
||||
|| $record->state() == Doctrine_Record::STATE_TDIRTY
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$sub = new Doctrine_Collection($table);
|
||||
foreach ($coll as $k => $related) {
|
||||
if ($related->get($local) == $record[$identifier]) {
|
||||
$sub->add($related->get($name));
|
||||
}
|
||||
}
|
||||
$this->data[$key]->setRelated($name, $sub);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
/**
|
||||
* save
|
||||
* saves all records of this collection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save(Doctrine_Connection $conn = null)
|
||||
{
|
||||
if ($conn == null) {
|
||||
$conn = $this->table->getConnection();
|
||||
}
|
||||
$conn->beginTransaction();
|
||||
|
||||
foreach ($this as $key => $record) {
|
||||
$record->save($conn);
|
||||
};
|
||||
|
||||
$conn->commit();
|
||||
}
|
||||
/**
|
||||
* single shot delete
|
||||
* deletes all records from this collection
|
||||
* and uses only one database query to perform this operation
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete(Doctrine_Connection $conn = null)
|
||||
{
|
||||
if ($conn == null) {
|
||||
$conn = $this->table->getConnection();
|
||||
}
|
||||
|
||||
$conn->beginTransaction();
|
||||
|
||||
foreach ($this as $key => $record) {
|
||||
$record->delete($conn);
|
||||
}
|
||||
|
||||
$conn->commit();
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
778
draft/new-core/Hydrate.php
Normal file
778
draft/new-core/Hydrate.php
Normal file
@ -0,0 +1,778 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id: Hydrate.php 1255 2007-04-16 14:43:12Z pookey $
|
||||
*
|
||||
* 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('Doctrine_Access');
|
||||
/**
|
||||
* Doctrine_Hydrate is a base class for Doctrine_RawSql and Doctrine_Query.
|
||||
* Its purpose is to populate object graphs.
|
||||
*
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision: 1255 $
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
abstract class Doctrine_Hydrate extends Doctrine_Access
|
||||
{
|
||||
/**
|
||||
* QUERY TYPE CONSTANTS
|
||||
*/
|
||||
|
||||
/**
|
||||
* constant for SELECT queries
|
||||
*/
|
||||
const SELECT = 0;
|
||||
/**
|
||||
* constant for DELETE queries
|
||||
*/
|
||||
const DELETE = 1;
|
||||
/**
|
||||
* constant for UPDATE queries
|
||||
*/
|
||||
const UPDATE = 2;
|
||||
/**
|
||||
* constant for INSERT queries
|
||||
*/
|
||||
const INSERT = 3;
|
||||
/**
|
||||
* constant for CREATE queries
|
||||
*/
|
||||
const CREATE = 4;
|
||||
/**
|
||||
* @var array $fetchmodes an array containing all fetchmodes
|
||||
*/
|
||||
protected $fetchModes = array();
|
||||
/**
|
||||
* @var array $tables an array containing all the tables used in the query
|
||||
*/
|
||||
protected $tables = array();
|
||||
/**
|
||||
* @var array $collections an array containing all collections
|
||||
* this hydrater has created/will create
|
||||
*/
|
||||
protected $collections = array();
|
||||
/**
|
||||
* @var array $joins an array containing all table joins
|
||||
*/
|
||||
protected $joins = array();
|
||||
/**
|
||||
* @var array $params query input parameters
|
||||
*/
|
||||
protected $params = array();
|
||||
/**
|
||||
* @var Doctrine_Connection $conn Doctrine_Connection object
|
||||
*/
|
||||
protected $conn;
|
||||
/**
|
||||
* @var Doctrine_View $view Doctrine_View object
|
||||
*/
|
||||
protected $view;
|
||||
/**
|
||||
* @var boolean $inheritanceApplied
|
||||
*/
|
||||
protected $inheritanceApplied = false;
|
||||
/**
|
||||
* @var boolean $aggregate
|
||||
*/
|
||||
protected $aggregate = false;
|
||||
/**
|
||||
* @var array $compAliases
|
||||
*/
|
||||
protected $compAliases = array();
|
||||
/**
|
||||
* @var array $tableAliases
|
||||
*/
|
||||
protected $tableAliases = array();
|
||||
/**
|
||||
* @var array $tableIndexes
|
||||
*/
|
||||
protected $tableIndexes = array();
|
||||
|
||||
protected $pendingAggregates = array();
|
||||
|
||||
protected $subqueryAggregates = array();
|
||||
/**
|
||||
* @var array $aggregateMap an array containing all aggregate aliases, keys as dql aliases
|
||||
* and values as sql aliases
|
||||
*/
|
||||
protected $aggregateMap = array();
|
||||
/**
|
||||
* @var Doctrine_Hydrate_Alias $aliasHandler
|
||||
*/
|
||||
protected $aliasHandler;
|
||||
/**
|
||||
* @var array $parts SQL query string parts
|
||||
*/
|
||||
protected $parts = array(
|
||||
'select' => array(),
|
||||
'from' => array(),
|
||||
'set' => array(),
|
||||
'join' => array(),
|
||||
'where' => array(),
|
||||
'groupby' => array(),
|
||||
'having' => array(),
|
||||
'orderby' => array(),
|
||||
'limit' => false,
|
||||
'offset' => false,
|
||||
);
|
||||
/**
|
||||
* @var integer $type the query type
|
||||
*
|
||||
* @see Doctrine_Query::* constants
|
||||
*/
|
||||
protected $type = self::SELECT;
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param Doctrine_Connection|null $connection
|
||||
*/
|
||||
public function __construct($connection = null)
|
||||
{
|
||||
if ( ! ($connection instanceof Doctrine_Connection)) {
|
||||
$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
|
||||
}
|
||||
$this->conn = $connection;
|
||||
$this->aliasHandler = new Doctrine_Hydrate_Alias();
|
||||
}
|
||||
/**
|
||||
* getComponentAliases
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getComponentAliases()
|
||||
{
|
||||
return $this->compAliases;
|
||||
}
|
||||
/**
|
||||
* getTableAliases
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTableAliases()
|
||||
{
|
||||
return $this->tableAliases;
|
||||
}
|
||||
/**
|
||||
* getTableIndexes
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTableIndexes()
|
||||
{
|
||||
return $this->tableIndexes;
|
||||
}
|
||||
/**
|
||||
* getTables
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTables()
|
||||
{
|
||||
return $this->tables;
|
||||
}
|
||||
/**
|
||||
* copyAliases
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function copyAliases(Doctrine_Hydrate $query)
|
||||
{
|
||||
$this->compAliases = $query->getComponentAliases();
|
||||
$this->tableAliases = $query->getTableAliases();
|
||||
$this->tableIndexes = $query->getTableIndexes();
|
||||
$this->aliasHandler = $query->aliasHandler;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPathAlias($path)
|
||||
{
|
||||
$s = array_search($path, $this->compAliases);
|
||||
if ($s === false)
|
||||
return $path;
|
||||
|
||||
return $s;
|
||||
}
|
||||
/**
|
||||
* createSubquery
|
||||
*
|
||||
* @return Doctrine_Hydrate
|
||||
*/
|
||||
public function createSubquery()
|
||||
{
|
||||
$class = get_class($this);
|
||||
$obj = new $class();
|
||||
|
||||
// copy the aliases to the subquery
|
||||
$obj->copyAliases($this);
|
||||
|
||||
// this prevents the 'id' being selected, re ticket #307
|
||||
$obj->isSubquery(true);
|
||||
|
||||
return $obj;
|
||||
}
|
||||
/**
|
||||
* getQuery
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getQuery();
|
||||
/**
|
||||
* limitSubqueryUsed
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isLimitSubqueryUsed()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* remove
|
||||
*
|
||||
* @param $name
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
if (isset($this->parts[$name])) {
|
||||
if ($name == "limit" || $name == "offset") {
|
||||
$this->parts[$name] = false;
|
||||
} else {
|
||||
$this->parts[$name] = array();
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* clear
|
||||
* resets all the variables
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function clear()
|
||||
{
|
||||
$this->fetchModes = array();
|
||||
$this->tables = array();
|
||||
$this->parts = array(
|
||||
"select" => array(),
|
||||
"from" => array(),
|
||||
"join" => array(),
|
||||
"where" => array(),
|
||||
"groupby" => array(),
|
||||
"having" => array(),
|
||||
"orderby" => array(),
|
||||
"limit" => false,
|
||||
"offset" => false,
|
||||
);
|
||||
$this->inheritanceApplied = false;
|
||||
$this->aggregate = false;
|
||||
|
||||
$this->collections = array();
|
||||
$this->joins = array();
|
||||
$this->tableIndexes = array();
|
||||
$this->tableAliases = array();
|
||||
$this->aliasHandler->clear();
|
||||
}
|
||||
/**
|
||||
* getConnection
|
||||
*
|
||||
* @return Doctrine_Connection
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->conn;
|
||||
}
|
||||
/**
|
||||
* setView
|
||||
* sets a database view this query object uses
|
||||
* this method should only be called internally by doctrine
|
||||
*
|
||||
* @param Doctrine_View $view database view
|
||||
* @return void
|
||||
*/
|
||||
public function setView(Doctrine_View $view)
|
||||
{
|
||||
$this->view = $view;
|
||||
}
|
||||
/**
|
||||
* getView
|
||||
* returns the view associated with this query object (if any)
|
||||
*
|
||||
* @return Doctrine_View the view associated with this query object
|
||||
*/
|
||||
public function getView()
|
||||
{
|
||||
return $this->view;
|
||||
}
|
||||
/**
|
||||
* getParams
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
/**
|
||||
* getTableAlias
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
final public function getTableAlias($path)
|
||||
{
|
||||
if (isset($this->compAliases[$path])) {
|
||||
$path = $this->compAliases[$path];
|
||||
}
|
||||
if ( ! isset($this->tableAliases[$path])) {
|
||||
return false;
|
||||
}
|
||||
return $this->tableAliases[$path];
|
||||
}
|
||||
/**
|
||||
* getCollection
|
||||
*
|
||||
* @parma string $name component name
|
||||
* @param integer $index
|
||||
*/
|
||||
private function getCollection($name)
|
||||
{
|
||||
$table = $this->tables[$name];
|
||||
if ( ! isset($this->fetchModes[$name])) {
|
||||
return new Doctrine_Collection($table);
|
||||
}
|
||||
switch ($this->fetchModes[$name]) {
|
||||
case Doctrine::FETCH_BATCH:
|
||||
$coll = new Doctrine_Collection_Batch($table);
|
||||
break;
|
||||
case Doctrine::FETCH_LAZY:
|
||||
$coll = new Doctrine_Collection_Lazy($table);
|
||||
break;
|
||||
case Doctrine::FETCH_OFFSET:
|
||||
$coll = new Doctrine_Collection_Offset($table);
|
||||
break;
|
||||
case Doctrine::FETCH_IMMEDIATE:
|
||||
$coll = new Doctrine_Collection_Immediate($table);
|
||||
break;
|
||||
case Doctrine::FETCH_LAZY_OFFSET:
|
||||
$coll = new Doctrine_Collection_LazyOffset($table);
|
||||
break;
|
||||
default:
|
||||
throw new Doctrine_Exception("Unknown fetchmode");
|
||||
};
|
||||
|
||||
return $coll;
|
||||
}
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams(array $params = array()) {
|
||||
$this->params = $params;
|
||||
}
|
||||
/**
|
||||
* execute
|
||||
* executes the dql query and populates all collections
|
||||
*
|
||||
* @param string $params
|
||||
* @return Doctrine_Collection the root collection
|
||||
*/
|
||||
public function execute($params = array(), $return = Doctrine::FETCH_RECORD) {
|
||||
$this->collections = array();
|
||||
|
||||
$params = $this->conn->convertBooleans(array_merge($this->params, $params));
|
||||
|
||||
if ( ! $this->view) {
|
||||
$query = $this->getQuery($params);
|
||||
} else {
|
||||
$query = $this->view->getSelectSql();
|
||||
}
|
||||
|
||||
if ($this->isLimitSubqueryUsed() &&
|
||||
$this->conn->getDBH()->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') {
|
||||
|
||||
$params = array_merge($params, $params);
|
||||
}
|
||||
$stmt = $this->conn->execute($query, $params);
|
||||
|
||||
if ($this->aggregate) {
|
||||
return $stmt->fetchAll(Doctrine::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
if (count($this->tables) == 0) {
|
||||
throw new Doctrine_Query_Exception('No components selected');
|
||||
}
|
||||
|
||||
$keys = array_keys($this->tables);
|
||||
$root = $keys[0];
|
||||
|
||||
$previd = array();
|
||||
|
||||
$coll = $this->getCollection($root);
|
||||
$prev[$root] = $coll;
|
||||
|
||||
if ($this->aggregate) {
|
||||
$return = Doctrine::FETCH_ARRAY;
|
||||
}
|
||||
|
||||
$array = $this->parseData($stmt);
|
||||
|
||||
if ($return == Doctrine::FETCH_ARRAY) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
foreach ($array as $data) {
|
||||
/**
|
||||
* remove duplicated data rows and map data into objects
|
||||
*/
|
||||
foreach ($data as $key => $row) {
|
||||
if (empty($row)) {
|
||||
continue;
|
||||
}
|
||||
//$key = array_search($key, $this->shortAliases);
|
||||
|
||||
foreach ($this->tables as $k => $t) {
|
||||
if ( ! strcasecmp($key, $k)) {
|
||||
$key = $k;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !isset($this->tables[$key]) ) {
|
||||
throw new Doctrine_Exception('No table named ' . $key . ' found.');
|
||||
}
|
||||
$ids = $this->tables[$key]->getIdentifier();
|
||||
$name = $key;
|
||||
|
||||
if ($this->isIdentifiable($row, $ids)) {
|
||||
if ($name !== $root) {
|
||||
$prev = $this->initRelated($prev, $name);
|
||||
}
|
||||
// aggregate values have numeric keys
|
||||
if (isset($row[0])) {
|
||||
$component = $this->tables[$name]->getComponentName();
|
||||
|
||||
// if the collection already has objects, get the last object
|
||||
// otherwise create a new one where the aggregate values are being mapped
|
||||
|
||||
if ($prev[$name]->count() > 0) {
|
||||
$record = $prev[$name]->getLast();
|
||||
} else {
|
||||
$record = new $component();
|
||||
$prev[$name]->add($record);
|
||||
}
|
||||
|
||||
$path = array_search($name, $this->tableAliases);
|
||||
$alias = $this->getPathAlias($path);
|
||||
|
||||
// map each aggregate value
|
||||
foreach ($row as $index => $value) {
|
||||
$agg = false;
|
||||
|
||||
if (isset($this->pendingAggregates[$alias][$index])) {
|
||||
$agg = $this->pendingAggregates[$alias][$index][3];
|
||||
} elseif (isset($this->subqueryAggregates[$alias][$index])) {
|
||||
$agg = $this->subqueryAggregates[$alias][$index];
|
||||
}
|
||||
|
||||
$record->mapValue($agg, $value);
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
if ( ! isset($previd[$name])) {
|
||||
$previd[$name] = array();
|
||||
}
|
||||
if ($previd[$name] !== $row) {
|
||||
// set internal data
|
||||
|
||||
$this->tables[$name]->setData($row);
|
||||
|
||||
// initialize a new record
|
||||
|
||||
$record = $this->tables[$name]->getRecord();
|
||||
|
||||
// aggregate values have numeric keys
|
||||
if (isset($row[0])) {
|
||||
$path = array_search($name, $this->tableAliases);
|
||||
$alias = $this->getPathAlias($path);
|
||||
|
||||
// map each aggregate value
|
||||
foreach ($row as $index => $value) {
|
||||
$agg = false;
|
||||
|
||||
if (isset($this->pendingAggregates[$alias][$index])) {
|
||||
$agg = $this->pendingAggregates[$alias][$index][3];
|
||||
} elseif (isset($this->subqueryAggregates[$alias][$index])) {
|
||||
$agg = $this->subqueryAggregates[$alias][$index];
|
||||
}
|
||||
$record->mapValue($agg, $value);
|
||||
}
|
||||
}
|
||||
|
||||
if ($name == $root) {
|
||||
// add record into root collection
|
||||
|
||||
$coll->add($record);
|
||||
unset($previd);
|
||||
|
||||
} else {
|
||||
$prev = $this->addRelated($prev, $name, $record);
|
||||
}
|
||||
|
||||
// following statement is needed to ensure that mappings
|
||||
// are being done properly when the result set doesn't
|
||||
// contain the rows in 'right order'
|
||||
|
||||
if ($prev[$name] !== $record) {
|
||||
$prev[$name] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
$previd[$name] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $coll;
|
||||
}
|
||||
/**
|
||||
* initRelation
|
||||
*
|
||||
* @param array $prev
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public function initRelated(array $prev, $name)
|
||||
{
|
||||
$pointer = $this->joins[$name];
|
||||
$path = array_search($name, $this->tableAliases);
|
||||
$tmp = explode('.', $path);
|
||||
$alias = end($tmp);
|
||||
|
||||
if ( ! isset($prev[$pointer]) ) {
|
||||
return $prev;
|
||||
}
|
||||
$fk = $this->tables[$pointer]->getRelation($alias);
|
||||
|
||||
if ( ! $fk->isOneToOne()) {
|
||||
if ($prev[$pointer]->getLast() instanceof Doctrine_Record) {
|
||||
if ( ! $prev[$pointer]->getLast()->hasReference($alias)) {
|
||||
$prev[$name] = $this->getCollection($name);
|
||||
$prev[$pointer]->getLast()->initReference($prev[$name],$fk);
|
||||
} else {
|
||||
$prev[$name] = $prev[$pointer]->getLast()->get($alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $prev;
|
||||
}
|
||||
/**
|
||||
* addRelated
|
||||
*
|
||||
* @param array $prev
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public function addRelated(array $prev, $name, Doctrine_Record $record)
|
||||
{
|
||||
$pointer = $this->joins[$name];
|
||||
|
||||
$path = array_search($name, $this->tableAliases);
|
||||
$tmp = explode('.', $path);
|
||||
$alias = end($tmp);
|
||||
|
||||
$fk = $this->tables[$pointer]->getRelation($alias);
|
||||
|
||||
if ($fk->isOneToOne()) {
|
||||
$prev[$pointer]->getLast()->set($fk->getAlias(), $record);
|
||||
|
||||
$prev[$name] = $record;
|
||||
} else {
|
||||
// one-to-many relation or many-to-many relation
|
||||
|
||||
if ( ! $prev[$pointer]->getLast()->hasReference($alias)) {
|
||||
$prev[$name] = $this->getCollection($name);
|
||||
$prev[$pointer]->getLast()->initReference($prev[$name], $fk);
|
||||
|
||||
} else {
|
||||
// previous entry found from memory
|
||||
$prev[$name] = $prev[$pointer]->getLast()->get($alias);
|
||||
}
|
||||
|
||||
$prev[$pointer]->getLast()->addReference($record, $fk);
|
||||
}
|
||||
return $prev;
|
||||
}
|
||||
/**
|
||||
* isIdentifiable
|
||||
* returns whether or not a given data row is identifiable (it contains
|
||||
* all id fields specified in the second argument)
|
||||
*
|
||||
* @param array $row
|
||||
* @param mixed $ids
|
||||
* @return boolean
|
||||
*/
|
||||
public function isIdentifiable(array $row, $ids)
|
||||
{
|
||||
if (is_array($ids)) {
|
||||
foreach ($ids as $id) {
|
||||
if ($row[$id] == null)
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if ( ! isset($row[$ids])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* getType
|
||||
*
|
||||
* returns the type of this query object
|
||||
* by default the type is Doctrine_Hydrate::SELECT but if update() or delete()
|
||||
* are being called the type is Doctrine_Hydrate::UPDATE and Doctrine_Hydrate::DELETE,
|
||||
* respectively
|
||||
*
|
||||
* @see Doctrine_Hydrate::SELECT
|
||||
* @see Doctrine_Hydrate::UPDATE
|
||||
* @see Doctrine_Hydrate::DELETE
|
||||
*
|
||||
* @return integer return the query type
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
/**
|
||||
* applyInheritance
|
||||
* applies column aggregation inheritance to DQL / SQL query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function applyInheritance()
|
||||
{
|
||||
// get the inheritance maps
|
||||
$array = array();
|
||||
|
||||
foreach ($this->tables as $alias => $table) {
|
||||
$array[$alias][] = $table->inheritanceMap;
|
||||
}
|
||||
|
||||
// apply inheritance maps
|
||||
$str = "";
|
||||
$c = array();
|
||||
|
||||
$index = 0;
|
||||
foreach ($array as $tableAlias => $maps) {
|
||||
$a = array();
|
||||
|
||||
// don't use table aliases if the query isn't a select query
|
||||
if ($this->type !== Doctrine_Query::SELECT) {
|
||||
$tableAlias = '';
|
||||
} else {
|
||||
$tableAlias .= '.';
|
||||
}
|
||||
|
||||
foreach ($maps as $map) {
|
||||
$b = array();
|
||||
foreach ($map as $field => $value) {
|
||||
if ($index > 0) {
|
||||
$b[] = '(' . $tableAlias . $field . ' = ' . $value
|
||||
. ' OR ' . $tableAlias . $field . ' IS NULL)';
|
||||
} else {
|
||||
$b[] = $tableAlias . $field . ' = ' . $value;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty($b)) {
|
||||
$a[] = implode(' AND ', $b);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty($a)) {
|
||||
$c[] = implode(' AND ', $a);
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
|
||||
$str .= implode(' AND ', $c);
|
||||
|
||||
return $str;
|
||||
}
|
||||
/**
|
||||
* parseData
|
||||
* parses the data returned by statement object
|
||||
*
|
||||
* @param mixed $stmt
|
||||
* @return array
|
||||
*/
|
||||
public function parseData($stmt)
|
||||
{
|
||||
$array = array();
|
||||
|
||||
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
/**
|
||||
* parse the data into two-dimensional array
|
||||
*/
|
||||
foreach ($data as $key => $value) {
|
||||
$e = explode('__', $key);
|
||||
|
||||
$field = strtolower(array_pop($e));
|
||||
$component = strtolower(implode('__', $e));
|
||||
|
||||
$data[$component][$field] = $value;
|
||||
|
||||
unset($data[$key]);
|
||||
};
|
||||
$array[] = $data;
|
||||
};
|
||||
|
||||
$stmt->closeCursor();
|
||||
return $array;
|
||||
}
|
||||
/**
|
||||
* returns a Doctrine_Table for given name
|
||||
*
|
||||
* @param string $name component name
|
||||
* @return Doctrine_Table|boolean
|
||||
*/
|
||||
public function getTable($name)
|
||||
{
|
||||
if (isset($this->tables[$name])) {
|
||||
return $this->tables[$name];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @return string returns a string representation of this object
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return Doctrine_Lib::formatSql($this->getQuery());
|
||||
}
|
||||
}
|
1680
draft/new-core/Query.php
Normal file
1680
draft/new-core/Query.php
Normal file
File diff suppressed because it is too large
Load Diff
246
draft/new-core/RawSql.php
Normal file
246
draft/new-core/RawSql.php
Normal file
@ -0,0 +1,246 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id: RawSql.php 1181 2007-03-20 23:22:51Z gnat $
|
||||
*
|
||||
* 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('Doctrine_Hydrate');
|
||||
/**
|
||||
* Doctrine_RawSql
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision: 1181 $
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_RawSql extends Doctrine_Hydrate
|
||||
{
|
||||
/**
|
||||
* @var array $fields
|
||||
*/
|
||||
private $fields;
|
||||
/**
|
||||
* __call
|
||||
* method overloader
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return Doctrine_RawSql
|
||||
*/
|
||||
public function __call($name, $args)
|
||||
{
|
||||
if ( ! isset($this->parts[$name])) {
|
||||
throw new Doctrine_RawSql_Exception("Unknown overload method $name. Availible overload methods are ".implode(" ",array_keys($this->parts)));
|
||||
}
|
||||
if ($name == 'select') {
|
||||
preg_match_all('/{([^}{]*)}/U', $args[0], $m);
|
||||
|
||||
$this->fields = $m[1];
|
||||
$this->parts["select"] = array();
|
||||
} else {
|
||||
$this->parts[$name][] = $args[0];
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* get
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
if ( ! isset($this->parts[$name])) {
|
||||
throw new Doctrine_RawSql_Exception('Unknown query part '.$name);
|
||||
}
|
||||
return $this->parts[$name];
|
||||
}
|
||||
/**
|
||||
* parseQuery
|
||||
*
|
||||
* @param string $query
|
||||
* @return Doctrine_RawSql
|
||||
*/
|
||||
public function parseQuery($query)
|
||||
{
|
||||
preg_match_all('/{([^}{]*)}/U', $query, $m);
|
||||
|
||||
$this->fields = $m[1];
|
||||
$this->clear();
|
||||
|
||||
$e = Doctrine_Query::sqlExplode($query,' ');
|
||||
|
||||
foreach ($e as $k => $part) {
|
||||
$low = strtolower($part);
|
||||
switch (strtolower($part)) {
|
||||
case "select":
|
||||
case "from":
|
||||
case "where":
|
||||
case "limit":
|
||||
case "offset":
|
||||
case "having":
|
||||
$p = $low;
|
||||
if ( ! isset($parts[$low])) {
|
||||
$parts[$low] = array();
|
||||
}
|
||||
break;
|
||||
case "order":
|
||||
case "group":
|
||||
$i = ($k + 1);
|
||||
if (isset($e[$i]) && strtolower($e[$i]) === "by") {
|
||||
$p = $low;
|
||||
$p .= "by";
|
||||
$parts[$low."by"] = array();
|
||||
|
||||
} else {
|
||||
$parts[$p][] = $part;
|
||||
}
|
||||
break;
|
||||
case "by":
|
||||
continue;
|
||||
default:
|
||||
if ( ! isset($parts[$p][0])) {
|
||||
$parts[$p][0] = $part;
|
||||
} else {
|
||||
$parts[$p][0] .= ' '.$part;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
$this->parts = $parts;
|
||||
$this->parts["select"] = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* getQuery
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$e = explode(".", $field);
|
||||
if ( ! isset($e[1])) {
|
||||
throw new Doctrine_RawSql_Exception("All selected fields in Sql query must be in format tableAlias.fieldName");
|
||||
}
|
||||
if ( ! isset($this->tables[$e[0]])) {
|
||||
try {
|
||||
$this->addComponent($e[0], ucwords($e[0]));
|
||||
} catch(Doctrine_Exception $exception) {
|
||||
throw new Doctrine_RawSql_Exception("The associated component for table alias $e[0] couldn't be found.");
|
||||
}
|
||||
}
|
||||
|
||||
if ($e[1] == '*') {
|
||||
foreach ($this->tables[$e[0]]->getColumnNames() as $name) {
|
||||
$field = $e[0].".".$name;
|
||||
$this->parts["select"][$field] = $field." AS ".$e[0]."__".$name;
|
||||
}
|
||||
} else {
|
||||
$field = $e[0].".".$e[1];
|
||||
$this->parts["select"][$field] = $field." AS ".$e[0]."__".$e[1];
|
||||
}
|
||||
}
|
||||
|
||||
// force-add all primary key fields
|
||||
|
||||
foreach ($this->tableAliases as $alias) {
|
||||
foreach ($this->tables[$alias]->getPrimaryKeys() as $key) {
|
||||
$field = $alias . '.' . $key;
|
||||
if ( ! isset($this->parts["select"][$field])) {
|
||||
$this->parts["select"][$field] = $field." AS ".$alias."__".$key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$q = 'SELECT '.implode(', ', $this->parts['select']);
|
||||
|
||||
$string = $this->applyInheritance();
|
||||
if ( ! empty($string)) {
|
||||
$this->parts['where'][] = $string;
|
||||
}
|
||||
$copy = $this->parts;
|
||||
unset($copy['select']);
|
||||
|
||||
$q .= ( ! empty($this->parts['from']))? ' FROM ' . implode(' ', $this->parts['from']) : '';
|
||||
$q .= ( ! empty($this->parts['where']))? ' WHERE ' . implode(' AND ', $this->parts['where']) : '';
|
||||
$q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
|
||||
$q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' ', $this->parts['having']) : '';
|
||||
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(' ', $this->parts['orderby']) : '';
|
||||
$q .= ( ! empty($this->parts['limit']))? ' LIMIT ' . implode(' ', $this->parts['limit']) : '';
|
||||
$q .= ( ! empty($this->parts['offset']))? ' OFFSET ' . implode(' ', $this->parts['offset']) : '';
|
||||
|
||||
if ( ! empty($string)) {
|
||||
array_pop($this->parts['where']);
|
||||
}
|
||||
return $q;
|
||||
}
|
||||
/**
|
||||
* getFields
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFields()
|
||||
{
|
||||
return $this->fields;
|
||||
}
|
||||
/**
|
||||
* addComponent
|
||||
*
|
||||
* @param string $tableAlias
|
||||
* @param string $componentName
|
||||
* @return Doctrine_RawSql
|
||||
*/
|
||||
public function addComponent($tableAlias, $componentName)
|
||||
{
|
||||
$e = explode('.', $componentName);
|
||||
|
||||
$currPath = '';
|
||||
$table = null;
|
||||
|
||||
foreach ($e as $k => $component) {
|
||||
$currPath .= '.' . $component;
|
||||
if ($k == 0)
|
||||
$currPath = substr($currPath,1);
|
||||
|
||||
if (isset($this->tableAliases[$currPath])) {
|
||||
$alias = $this->tableAliases[$currPath];
|
||||
} else {
|
||||
$alias = $tableAlias;
|
||||
}
|
||||
|
||||
if ($table) {
|
||||
$tableName = $table->getAliasName($component);
|
||||
}
|
||||
$table = $this->conn->getTable($component);
|
||||
$this->tables[$alias] = $table;
|
||||
$this->fetchModes[$alias] = Doctrine::FETCH_IMMEDIATE;
|
||||
$this->tableAliases[$currPath] = $alias;
|
||||
|
||||
if ($k !== 0) {
|
||||
$this->joins[$alias] = $prevAlias;
|
||||
}
|
||||
$prevAlias = $alias;
|
||||
$prevPath = $currPath;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
1682
draft/new-core/Record.php
Normal file
1682
draft/new-core/Record.php
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user