1
0
mirror of synced 2025-01-18 22:41:43 +03:00
doctrine2/lib/Doctrine/Hydrate.php

934 lines
30 KiB
PHP
Raw Normal View History

2006-07-21 23:22:15 +00:00
<?php
2006-12-29 14:01:31 +00:00
/*
2006-08-06 20:46:12 +00: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>.
*/
2007-05-16 19:20:55 +00:00
2006-08-06 20:46:12 +00:00
/**
* 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$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
2007-05-27 11:38:16 +00:00
class Doctrine_Hydrate implements Serializable
2006-12-29 14:40:47 +00:00
{
/**
* 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;
2007-05-16 19:20:55 +00:00
/**
* @var array $params query input parameters
*/
2007-05-24 17:49:15 +00:00
protected $_params = array();
2006-07-21 23:22:15 +00:00
/**
* @var Doctrine_Connection $conn Doctrine_Connection object
2006-07-21 23:22:15 +00:00
*/
2007-05-24 17:46:32 +00:00
protected $_conn;
2006-07-21 23:22:15 +00:00
/**
2007-05-20 21:30:08 +00:00
* @var Doctrine_View $_view Doctrine_View object, when set this object will use the
* the query given by the view object for object population
2006-07-21 23:22:15 +00:00
*/
2007-05-20 21:30:08 +00:00
protected $_view;
/**
2007-05-16 19:20:55 +00:00
* @var array $_aliasMap two dimensional array containing the map for query aliases
* Main keys are component aliases
*
* table table object associated with given alias
*
* relation the relation object owned by the parent
*
* parent the alias of the parent
*
2007-05-26 19:50:40 +00:00
* agg the aggregates of this component
*/
2007-05-24 17:49:15 +00:00
protected $_aliasMap = array();
2006-07-21 23:22:15 +00:00
/**
2007-05-16 19:20:55 +00:00
*
2006-07-21 23:22:15 +00:00
*/
2006-10-17 17:21:21 +00:00
protected $pendingAggregates = array();
/**
* @var array $aggregateMap an array containing all aggregate aliases, keys as dql aliases
* and values as sql aliases
*/
2006-10-17 17:21:21 +00:00
protected $aggregateMap = array();
2007-05-27 11:38:16 +00:00
/**
* @var array $_options an array of options
*/
protected $_options = array(
'fetchMode' => Doctrine::FETCH_RECORD,
'parserCache' => false,
'resultSetCache' => false,
);
2006-07-21 23:22:15 +00:00
/**
* @var array $parts SQL query string parts
*/
protected $parts = array(
2006-12-14 13:26:16 +00:00
'select' => array(),
2007-05-17 23:13:58 +00:00
'distinct' => false,
'forUpdate' => false,
2006-12-14 13:26:16 +00:00
'from' => array(),
'set' => array(),
'join' => array(),
'where' => array(),
'groupby' => array(),
'having' => array(),
'orderby' => array(),
'limit' => false,
'offset' => false,
2006-07-21 23:22:15 +00:00
);
/**
* @var integer $type the query type
*
* @see Doctrine_Query::* constants
*/
protected $type = self::SELECT;
2007-05-27 18:52:33 +00:00
protected $_cache;
2007-05-24 17:40:54 +00:00
2007-05-26 19:50:40 +00:00
protected $_tableAliases = array();
2007-05-24 18:21:26 +00:00
/**
* @var array $_tableAliasSeeds A simple array keys representing table aliases and values
* as table alias seeds. The seeds are used for generating short table
* aliases.
*/
protected $_tableAliasSeeds = array();
2006-07-21 23:22:15 +00:00
/**
* constructor
*
* @param Doctrine_Connection|null $connection
2006-07-21 23:22:15 +00:00
*/
2006-12-29 14:40:47 +00:00
public function __construct($connection = null)
{
2006-12-29 14:01:31 +00:00
if ( ! ($connection instanceof Doctrine_Connection)) {
$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
2006-12-29 14:01:31 +00:00
}
2007-05-24 17:46:32 +00:00
$this->_conn = $connection;
2007-05-24 17:40:54 +00:00
}
2007-05-27 11:38:16 +00:00
public function getSql()
{
return $this->getQuery();
}
2007-05-27 18:52:33 +00:00
public function setCache(Doctrine_Cache_Interface $cache)
{
$this->_cache = $cache;
}
public function getCache()
{
return $this->_cache;
}
2007-05-27 11:38:16 +00:00
/**
* serialize
* this method is automatically called when this Doctrine_Hydrate is serialized
*
* @return array an array of serialized properties
*/
public function serialize()
{
$vars = get_object_vars($this);
}
/**
* unseralize
* this method is automatically called everytime a Doctrine_Hydrate object is unserialized
*
* @param string $serialized Doctrine_Record as serialized string
* @return void
*/
public function unserialize($serialized)
{
}
2007-05-24 18:21:26 +00:00
/**
* generateNewTableAlias
* generates a new alias from given table alias
*
* @param string $tableAlias table alias from which to generate the new alias from
* @return string the created table alias
*/
public function generateNewTableAlias($tableAlias)
2007-05-24 17:40:54 +00:00
{
2007-05-24 18:21:26 +00:00
if (isset($this->_tableAliases[$tableAlias])) {
2007-05-24 17:40:54 +00:00
// generate a new alias
2007-05-24 18:21:26 +00:00
$name = substr($tableAlias, 0, 1);
$i = ((int) substr($tableAlias, 1));
2007-05-24 17:40:54 +00:00
if ($i == 0) {
$i = 1;
}
2007-05-24 18:21:26 +00:00
$newIndex = ($this->_tableAliasSeeds[$name] + $i);
2007-05-24 17:40:54 +00:00
return $name . $newIndex;
}
return $alias;
}
2007-05-24 18:30:18 +00:00
/**
* hasTableAlias
* whether or not this object has given tableAlias
*
* @param string $tableAlias the table alias to be checked
* @return boolean true if this object has given alias, otherwise false
*/
public function hasTableAlias($tableAlias)
2007-05-24 17:40:54 +00:00
{
2007-05-24 18:30:18 +00:00
return (isset($this->_tableAliases[$tableAlias]));
2007-05-24 17:40:54 +00:00
}
2007-05-24 18:30:18 +00:00
/**
* getComponentAlias
* get component alias associated with given table alias
*
* @param string $tableAlias the table alias that identifies the component alias
* @return string component alias
*/
2007-05-24 17:40:54 +00:00
public function getComponentAlias($tableAlias)
{
2007-05-24 18:21:26 +00:00
if ( ! isset($this->_tableAliases[$tableAlias])) {
2007-05-24 17:40:54 +00:00
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
}
2007-05-24 18:21:26 +00:00
return $this->_tableAliases[$tableAlias];
2007-05-24 17:40:54 +00:00
}
2007-05-24 18:21:26 +00:00
/**
* getTableAliasSeed
* returns the alias seed for given table alias
*
* @param string $tableAlias table alias that identifies the alias seed
* @return integer table alias seed
*/
public function getTableAliasSeed($tableAlias)
2007-05-24 17:40:54 +00:00
{
2007-05-24 18:21:26 +00:00
if ( ! isset($this->_tableAliasSeeds[$tableAlias])) {
2007-05-24 17:40:54 +00:00
return 0;
}
2007-05-24 18:21:26 +00:00
return $this->_tableAliasSeeds[$tableAlias];
2007-05-24 17:40:54 +00:00
}
2007-05-24 18:21:26 +00:00
/**
* generateTableAlias
* generates a table alias from given table name and associates
* it with given component alias
*
* @param string $componentAlias the component alias to be associated with generated table alias
* @param string $tableName the table name from which to generate the table alias
* @return string the generated table alias
*/
public function generateTableAlias($componentAlias, $tableName)
2007-05-24 17:40:54 +00:00
{
$char = strtolower(substr($tableName, 0, 1));
$alias = $char;
2007-05-24 18:21:26 +00:00
if ( ! isset($this->_tableAliasSeeds[$alias])) {
$this->_tableAliasSeeds[$alias] = 1;
2007-05-24 17:40:54 +00:00
}
2007-05-24 18:21:26 +00:00
while (isset($this->_tableAliases[$alias])) {
$alias = $char . ++$this->_tableAliasSeeds[$alias];
2007-05-24 17:40:54 +00:00
}
2007-05-24 18:21:26 +00:00
$this->_tableAliases[$alias] = $componentAlias;
2007-05-24 17:40:54 +00:00
return $alias;
}
2007-05-24 18:00:35 +00:00
/**
2007-05-27 11:38:16 +00:00
* getTableAliases
* returns all table aliases
2007-05-24 18:00:35 +00:00
*
2007-05-27 11:38:16 +00:00
* @return array table aliases as an array
2007-05-24 18:00:35 +00:00
*/
2007-05-27 11:38:16 +00:00
public function getTableAliases()
2007-05-24 17:40:54 +00:00
{
2007-05-24 18:21:26 +00:00
return $this->_tableAliases;
2007-05-24 17:40:54 +00:00
}
2007-05-24 18:00:35 +00:00
/**
* addTableAlias
* adds an alias for table and associates it with given component alias
*
* @param string $componentAlias the alias for the query component associated with given tableAlias
* @param string $tableAlias the table alias to be added
* @return Doctrine_Hydrate
*/
public function addTableAlias($tableAlias, $componentAlias)
2007-05-24 17:40:54 +00:00
{
2007-05-24 18:21:26 +00:00
$this->_tableAliases[$tableAlias] = $componentAlias;
2007-05-24 18:00:35 +00:00
return $this;
2007-05-24 17:40:54 +00:00
}
/**
2007-05-24 18:00:35 +00:00
* getTableAlias
2007-05-24 17:40:54 +00:00
* some database such as Oracle need the identifier lengths to be < ~30 chars
* hence Doctrine creates as short identifier aliases as possible
*
* this method is used for the creation of short table aliases, its also
* smart enough to check if an alias already exists for given component (componentAlias)
*
* @param string $componentAlias the alias for the query component to search table alias for
* @param string $tableName the table name from which the table alias is being created
* @return string the generated / fetched short alias
*/
2007-05-24 18:00:35 +00:00
public function getTableAlias($componentAlias, $tableName = null)
2007-05-24 17:40:54 +00:00
{
2007-05-24 18:21:26 +00:00
$alias = array_search($componentAlias, $this->_tableAliases);
2007-05-24 17:40:54 +00:00
if ($alias !== false) {
return $alias;
}
if ($tableName === null) {
throw new Doctrine_Hydrate_Exception("Couldn't get short alias for " . $componentAlias);
}
2007-05-24 18:21:26 +00:00
return $this->generateTableAlias($componentAlias, $tableName);
2006-07-21 23:22:15 +00:00
}
2007-05-24 18:00:35 +00:00
/**
* addQueryPart
* adds a query part in the query part array
*
* @param string $name the name of the query part to be added
* @param string $part query part string
* @throws Doctrine_Hydrate_Exception if trying to add unknown query part
* @return Doctrine_Hydrate this object
*/
public function addQueryPart($name, $part)
2007-05-16 19:20:55 +00:00
{
if ( ! isset($this->parts[$name])) {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name);
}
$this->parts[$name][] = $part;
2007-05-24 18:00:35 +00:00
return $this;
2007-05-16 19:20:55 +00:00
}
2007-05-24 18:35:52 +00:00
/**
2007-05-27 11:38:16 +00:00
* setQueryPart
* sets a query part in the query part array
2007-05-24 18:35:52 +00:00
*
2007-05-27 11:38:16 +00:00
* @param string $name the name of the query part to be set
* @param string $part query part string
* @throws Doctrine_Hydrate_Exception if trying to set unknown query part
* @return Doctrine_Hydrate this object
2007-05-24 18:35:52 +00:00
*/
2007-05-27 11:38:16 +00:00
public function getQueryPart($part)
2006-12-29 14:40:47 +00:00
{
2007-05-27 11:38:16 +00:00
if ( ! isset($this->parts[$part])) {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $part);
}
2007-05-27 11:38:16 +00:00
return $this->parts[$part];
}
/**
* removeQueryPart
* removes a query part from the query part array
*
* @param string $name the name of the query part to be removed
* @throws Doctrine_Hydrate_Exception if trying to remove unknown query part
* @return Doctrine_Hydrate this object
*/
public function removeQueryPart($name)
{
if (isset($this->parts[$name])) {
if ($name == 'limit' || $name == 'offset') {
$this->parts[$name] = false;
} else {
$this->parts[$name] = array();
}
} else {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $part);
}
return $this;
2007-05-16 19:20:55 +00:00
}
2007-05-24 18:00:35 +00:00
/**
* setQueryPart
* sets a query part in the query part array
*
* @param string $name the name of the query part to be set
* @param string $part query part string
* @throws Doctrine_Hydrate_Exception if trying to set unknown query part
* @return Doctrine_Hydrate this object
*/
2007-05-16 19:20:55 +00:00
public function setQueryPart($name, $part)
{
if ( ! isset($this->parts[$name])) {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name);
}
2007-05-16 19:20:55 +00:00
if ($name !== 'limit' && $name !== 'offset') {
$this->parts[$name] = array($part);
} else {
$this->parts[$name] = $part;
}
2007-05-24 18:00:35 +00:00
return $this;
2006-12-23 22:45:36 +00:00
}
2007-05-27 11:38:16 +00:00
/**
* getAliasDeclaration
* get the declaration for given component alias
*
* @param string $componentAlias the component alias the retrieve the declaration from
* @return array the alias declaration
*/
public function getAliasDeclaration($componentAlias)
{
if ( ! isset($this->_aliasMap[$componentAlias])) {
throw new Doctrine_Hydrate_Exception('Unknown component alias ' . $componentAlias);
}
return $this->_aliasMap[$componentAlias];
}
/**
* copyAliases
2007-05-24 17:46:32 +00:00
* copy aliases from another Hydrate object
*
2007-05-24 17:46:32 +00:00
* this method is needed by DQL subqueries which need the aliases
* of the parent query
*
* @param Doctrine_Hydrate $query the query object from which the
* aliases are copied from
* @return Doctrine_Hydrate this object
*/
2007-05-24 17:46:32 +00:00
public function copyAliases(Doctrine_Hydrate $query)
2006-12-29 14:40:47 +00:00
{
2007-05-24 18:21:26 +00:00
$this->_tableAliases = $query->_tableAliases;
2006-12-29 14:01:31 +00:00
return $this;
}
/**
* createSubquery
2007-05-24 17:46:32 +00:00
* creates a subquery
2006-12-29 14:01:31 +00:00
*
* @return Doctrine_Hydrate
*/
2006-12-29 14:40:47 +00:00
public function createSubquery()
{
$class = get_class($this);
$obj = new $class();
2006-12-29 14:01:31 +00:00
// copy the aliases to the subquery
$obj->copyAliases($this);
2007-04-16 13:49:14 +00:00
// this prevents the 'id' being selected, re ticket #307
$obj->isSubquery(true);
return $obj;
}
/**
* limitSubqueryUsed
2007-05-24 18:35:52 +00:00
* whether or not limit subquery was used
2006-09-07 21:25:08 +00:00
*
* @return boolean
*/
2006-12-29 14:40:47 +00:00
public function isLimitSubqueryUsed()
{
return false;
}
2006-07-21 23:22:15 +00:00
/**
* clear
* resets all the variables
2006-12-29 14:01:31 +00:00
*
2006-07-21 23:22:15 +00:00
* @return void
*/
2006-12-29 14:40:47 +00:00
protected function clear()
{
2006-07-21 23:22:15 +00:00
$this->parts = array(
2007-05-16 19:20:55 +00:00
'select' => array(),
2007-05-17 23:13:58 +00:00
'distinct' => false,
'forUpdate' => false,
2007-05-16 19:20:55 +00:00
'from' => array(),
'set' => array(),
'join' => array(),
'where' => array(),
'groupby' => array(),
'having' => array(),
'orderby' => array(),
'limit' => false,
'offset' => false,
);
2006-07-21 23:22:15 +00:00
$this->inheritanceApplied = false;
}
/**
2006-09-07 21:25:08 +00:00
* getConnection
*
2006-08-21 23:19:15 +00:00
* @return Doctrine_Connection
2006-07-21 23:22:15 +00:00
*/
2006-12-29 14:40:47 +00:00
public function getConnection()
{
2007-05-24 17:46:32 +00:00
return $this->_conn;
2006-07-21 23:22:15 +00:00
}
/**
* 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
*/
2006-12-29 14:40:47 +00:00
public function setView(Doctrine_View $view)
{
2007-05-20 21:30:08 +00:00
$this->_view = $view;
2006-07-21 23:22:15 +00:00
}
/**
* getView
2006-12-23 22:45:36 +00:00
* returns the view associated with this query object (if any)
2006-07-21 23:22:15 +00:00
*
2006-12-23 22:45:36 +00:00
* @return Doctrine_View the view associated with this query object
2006-07-21 23:22:15 +00:00
*/
2006-12-29 14:40:47 +00:00
public function getView()
{
2007-05-20 21:30:08 +00:00
return $this->_view;
2006-07-21 23:22:15 +00:00
}
2006-12-29 11:30:36 +00:00
/**
* getParams
2006-12-29 14:40:47 +00:00
*
2006-12-29 11:30:36 +00:00
* @return array
*/
2006-12-29 14:40:47 +00:00
public function getParams()
{
2007-05-24 17:49:15 +00:00
return $this->_params;
2006-12-29 11:30:36 +00:00
}
2006-12-22 21:10:14 +00:00
/**
* setParams
*
* @param array $params
*/
public function setParams(array $params = array()) {
2007-05-24 17:49:15 +00:00
$this->_params = $params;
2006-12-22 21:10:14 +00:00
}
2007-05-17 23:13:58 +00:00
public function convertEnums($params)
{
return $params;
}
2007-05-24 18:35:52 +00:00
/**
* setAliasMap
* sets the whole component alias map
*
* @param array $map alias map
* @return Doctrine_Hydrate this object
*/
public function setAliasMap(array $map)
2007-05-16 19:20:55 +00:00
{
$this->_aliasMap = $map;
2007-05-24 18:35:52 +00:00
return $this;
2007-05-16 19:20:55 +00:00
}
2007-05-24 18:35:52 +00:00
/**
* getAliasMap
* returns the component alias map
*
* @return array component alias map
*/
2007-05-17 23:13:58 +00:00
public function getAliasMap()
2007-05-16 19:20:55 +00:00
{
return $this->_aliasMap;
}
/**
* mapAggregateValues
* map the aggregate values of given dataset row to a given record
*
* @param Doctrine_Record $record
* @param array $row
* @return Doctrine_Record
*/
public function mapAggregateValues($record, array $row, $alias)
{
$found = false;
2007-05-16 19:20:55 +00:00
// aggregate values have numeric keys
if (isset($row[0])) {
2007-05-26 19:50:40 +00:00
// map each aggregate value
2007-05-16 19:20:55 +00:00
foreach ($row as $index => $value) {
$agg = false;
2007-05-26 19:50:40 +00:00
if (isset($this->_aliasMap[$alias]['agg'][$index])) {
$agg = $this->_aliasMap[$alias]['agg'][$index];
2007-05-16 19:20:55 +00:00
}
$record->mapValue($agg, $value);
$found = true;
}
2007-04-14 16:28:09 +00:00
}
2007-05-16 19:20:55 +00:00
return $found;
}
2007-05-27 11:38:16 +00:00
public function getCachedForm(array $resultSet)
{
$map = '';
foreach ($this->getAliasMap() as $k => $v) {
if ( ! isset($v['parent'])) {
$map[$k][] = $v['table']->getComponentName();
} else {
$map[$k][] = $v['parent'] . '.' . $v['relation']->getAlias();
}
if (isset($v['agg'])) {
$map[$k][] = $v['agg'];
}
}
return serialize(array($resultSet, $map, $this->getTableAliases()));
}
public function _execute($params, $return = Doctrine::FETCH_RECORD)
2007-05-16 19:20:55 +00:00
{
2007-05-24 17:49:15 +00:00
$params = $this->_conn->convertBooleans(array_merge($this->_params, $params));
$params = $this->convertEnums($params);
if ( ! $this->_view) {
$query = $this->getQuery($params);
} else {
$query = $this->_view->getSelectSql();
}
2007-05-21 16:29:07 +00:00
if ($this->isLimitSubqueryUsed() &&
2007-05-24 17:46:32 +00:00
$this->_conn->getDBH()->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') {
$params = array_merge($params, $params);
}
if ($this->type !== self::SELECT) {
2007-05-24 17:46:32 +00:00
return $this->_conn->exec($query, $params);
}
2007-05-24 17:46:32 +00:00
$stmt = $this->_conn->execute($query, $params);
2007-05-27 11:38:16 +00:00
return (array) $this->parseData($stmt);
}
/**
* execute
* executes the query and populates the data set
*
* @param string $params
* @return Doctrine_Collection the root collection
*/
public function execute($params = array(), $return = Doctrine::FETCH_RECORD)
{
2007-05-27 18:52:33 +00:00
if ($this->_cache) {
2007-05-27 11:38:16 +00:00
$dql = $this->getDql();
// calculate hash for dql query
$hash = strlen($dql) . md5($dql);
2007-05-27 18:52:33 +00:00
$cached = $this->_cache->fetch($hash);
2007-05-27 11:38:16 +00:00
if ($cached === null) {
// cache miss
$array = $this->_execute($params, $return);
$cached = $this->getCachedForm($array);
2007-05-27 18:52:33 +00:00
$this->_cache->save($hash, $cached);
2007-05-27 11:38:16 +00:00
} else {
$cached = unserialize($cached);
$this->_tableAliases = $cached[2];
$array = $cached[0];
$map = array();
foreach ($cached[1] as $k => $v) {
$e = explode('.', $v[0]);
if (count($e) === 1) {
$map[$k]['table'] = $this->_conn->getTable($e[0]);
} else {
$map[$k]['parent'] = $e[0];
$map[$k]['relation'] = $map[$e[0]]['table']->getRelation($e[1]);
$map[$k]['table'] = $map[$k]['relation']->getTable();
}
if (isset($v[1])) {
$map[$k]['agg'] = $v[1];
}
}
$this->_aliasMap = $map;
}
} else {
$array = $this->_execute($params, $return);
}
if ($return === Doctrine::FETCH_ARRAY) {
return $array;
}
2007-05-16 19:20:55 +00:00
if (empty($this->_aliasMap)) {
throw new Doctrine_Hydrate_Exception("Couldn't execute query. Component alias map was empty.");
2007-04-14 16:28:09 +00:00
}
2007-05-16 19:20:55 +00:00
// initialize some variables used within the main loop
reset($this->_aliasMap);
$rootMap = current($this->_aliasMap);
$rootAlias = key($this->_aliasMap);
2007-05-16 19:52:06 +00:00
$coll = new Doctrine_Collection($rootMap['table']);
2007-05-16 19:20:55 +00:00
$prev[$rootAlias] = $coll;
2007-05-16 21:47:23 +00:00
2007-05-16 19:52:06 +00:00
// we keep track of all the collections
$colls = array();
2007-05-17 23:13:58 +00:00
$colls[] = $coll;
2007-05-16 19:20:55 +00:00
$prevRow = array();
/**
* iterate over the fetched data
* here $data is a two dimensional array
*/
2006-12-29 14:01:31 +00:00
foreach ($array as $data) {
2006-10-01 15:18:04 +00:00
/**
* remove duplicated data rows and map data into objects
*/
2007-05-16 19:20:55 +00:00
foreach ($data as $tableAlias => $row) {
// skip empty rows (not mappable)
2006-12-29 14:01:31 +00:00
if (empty($row)) {
2006-10-01 15:18:04 +00:00
continue;
2006-12-29 14:01:31 +00:00
}
2007-05-24 17:40:54 +00:00
$alias = $this->getComponentAlias($tableAlias);
2007-05-16 19:20:55 +00:00
$map = $this->_aliasMap[$alias];
2006-07-21 23:22:15 +00:00
2007-05-16 19:20:55 +00:00
// initialize previous row array if not set
if ( ! isset($prevRow[$tableAlias])) {
$prevRow[$tableAlias] = array();
2006-10-24 17:24:58 +00:00
}
2006-10-17 17:21:21 +00:00
2007-05-16 19:20:55 +00:00
// don't map duplicate rows
if ($prevRow[$tableAlias] !== $row) {
$identifiable = $this->isIdentifiable($row, $map['table']->getIdentifier());
2006-07-21 23:22:15 +00:00
2007-05-16 19:20:55 +00:00
if ($identifiable) {
// set internal data
$map['table']->setData($row);
2006-12-21 22:06:08 +00:00
}
2006-10-17 17:21:21 +00:00
2007-05-16 19:20:55 +00:00
// initialize a new record
$record = $map['table']->getRecord();
2006-10-17 17:21:21 +00:00
2007-05-16 19:20:55 +00:00
// map aggregate values (if any)
if($this->mapAggregateValues($record, $row, $alias)) {
$identifiable = true;
2006-12-21 22:06:08 +00:00
}
2007-05-16 19:20:55 +00:00
if ($alias == $rootAlias) {
// add record into root collection
2007-05-16 20:27:03 +00:00
2007-05-16 19:20:55 +00:00
if ($identifiable) {
$coll->add($record);
unset($prevRow);
}
} else {
2006-09-23 17:02:30 +00:00
2007-05-16 19:20:55 +00:00
$relation = $map['relation'];
$parentAlias = $map['parent'];
$parentMap = $this->_aliasMap[$parentAlias];
$parent = $prev[$parentAlias]->getLast();
2006-07-21 23:22:15 +00:00
2007-05-16 19:20:55 +00:00
// check the type of the relation
if ($relation->isOneToOne()) {
if ( ! $identifiable) {
continue;
}
$prev[$alias] = $record;
} else {
// one-to-many relation or many-to-many relation
if ( ! $prev[$parentAlias]->getLast()->hasReference($relation->getAlias())) {
// initialize a new collection
$prev[$alias] = new Doctrine_Collection($map['table']);
$prev[$alias]->setReference($parent, $relation);
} else {
// previous entry found from memory
$prev[$alias] = $prev[$parentAlias]->getLast()->get($relation->getAlias());
}
2007-05-16 19:52:06 +00:00
$colls[] = $prev[$alias];
2007-05-16 19:20:55 +00:00
// add record to the current collection
if ($identifiable) {
$prev[$alias]->add($record);
2006-12-29 14:01:31 +00:00
}
2006-12-21 22:06:08 +00:00
}
2007-05-16 19:20:55 +00:00
// initialize the relation from parent to the current collection/record
$parent->set($relation->getAlias(), $prev[$alias]);
2006-12-21 22:06:08 +00:00
}
2006-10-01 15:18:04 +00:00
// following statement is needed to ensure that mappings
// are being done properly when the result set doesn't
// contain the rows in 'right order'
2007-05-16 19:20:55 +00:00
if ($prev[$alias] !== $record) {
$prev[$alias] = $record;
2007-04-14 16:28:09 +00:00
}
2006-07-21 23:22:15 +00:00
}
2007-05-16 19:20:55 +00:00
$prevRow[$tableAlias] = $row;
2006-10-01 15:18:04 +00:00
}
}
2007-05-16 19:52:06 +00:00
// take snapshots from all initialized collections
2007-05-16 20:27:03 +00:00
foreach(array_unique($colls) as $c) {
$c->takeSnapshot();
2007-05-16 19:52:06 +00:00
}
2006-10-01 15:18:04 +00:00
return $coll;
2006-07-21 23:22:15 +00:00
}
/**
* isIdentifiable
2006-12-29 14:01:31 +00:00
* returns whether or not a given data row is identifiable (it contains
2007-05-16 19:20:55 +00:00
* all primary key fields specified in the second argument)
*
* @param array $row
2007-05-16 19:20:55 +00:00
* @param mixed $primaryKeys
* @return boolean
*/
2007-05-16 19:20:55 +00:00
public function isIdentifiable(array $row, $primaryKeys)
2006-12-29 14:40:47 +00:00
{
2007-05-16 19:20:55 +00:00
if (is_array($primaryKeys)) {
foreach ($primaryKeys as $id) {
if ($row[$id] == null) {
return false;
}
}
} else {
2007-05-16 19:20:55 +00:00
if ( ! isset($row[$primaryKeys])) {
return false;
2006-12-29 14:01:31 +00:00
}
}
2007-05-16 19:20:55 +00:00
return true;
}
/**
* 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;
}
2006-07-21 23:22:15 +00:00
/**
* applyInheritance
* applies column aggregation inheritance to DQL / SQL query
2006-07-21 23:22:15 +00:00
*
* @return string
*/
2006-12-29 14:40:47 +00:00
public function applyInheritance()
{
2006-07-21 23:22:15 +00:00
// get the inheritance maps
$array = array();
2007-05-16 19:20:55 +00:00
foreach ($this->_aliasMap as $componentAlias => $data) {
$tableAlias = $this->getTableAlias($componentAlias);
$array[$tableAlias][] = $data['table']->inheritanceMap;
2007-02-08 13:56:23 +00:00
}
2006-07-21 23:22:15 +00:00
// apply inheritance maps
2007-05-16 19:20:55 +00:00
$str = '';
2006-07-21 23:22:15 +00:00
$c = array();
$index = 0;
2006-12-29 14:01:31 +00:00
foreach ($array as $tableAlias => $maps) {
2006-07-21 23:22:15 +00:00
$a = array();
// don't use table aliases if the query isn't a select query
if ($this->type !== Doctrine_Query::SELECT) {
$tableAlias = '';
} else {
$tableAlias .= '.';
}
2007-04-14 16:28:09 +00:00
2006-12-29 14:01:31 +00:00
foreach ($maps as $map) {
2006-07-21 23:22:15 +00:00
$b = array();
2006-12-29 14:01:31 +00:00
foreach ($map as $field => $value) {
if ($index > 0) {
$b[] = '(' . $tableAlias . $field . ' = ' . $value
. ' OR ' . $tableAlias . $field . ' IS NULL)';
2006-12-29 14:01:31 +00:00
} else {
$b[] = $tableAlias . $field . ' = ' . $value;
2006-12-29 14:01:31 +00:00
}
2006-07-21 23:22:15 +00:00
}
2006-12-29 14:01:31 +00:00
if ( ! empty($b)) {
$a[] = implode(' AND ', $b);
2006-12-29 14:01:31 +00:00
}
2006-07-21 23:22:15 +00:00
}
2006-12-29 14:01:31 +00:00
if ( ! empty($a)) {
$c[] = implode(' AND ', $a);
2006-12-29 14:01:31 +00:00
}
2006-07-21 23:22:15 +00:00
$index++;
}
$str .= implode(' AND ', $c);
2006-07-21 23:22:15 +00:00
return $str;
}
/**
* parseData
* parses the data returned by statement object
2006-07-21 23:22:15 +00:00
*
* @param mixed $stmt
2006-07-21 23:22:15 +00:00
* @return array
*/
public function parseData($stmt)
2006-12-29 14:40:47 +00:00
{
2006-07-21 23:22:15 +00:00
$array = array();
2006-12-29 14:01:31 +00:00
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
2006-07-21 23:22:15 +00:00
/**
* parse the data into two-dimensional array
*/
2006-12-29 14:01:31 +00:00
foreach ($data as $key => $value) {
2006-12-23 22:45:36 +00:00
$e = explode('__', $key);
2006-07-21 23:22:15 +00:00
2007-05-16 19:20:55 +00:00
$field = strtolower(array_pop($e));
$tableAlias = strtolower(implode('__', $e));
2006-09-03 19:20:02 +00:00
2007-05-16 19:20:55 +00:00
$data[$tableAlias][$field] = $value;
2006-08-22 19:34:40 +00:00
2006-07-21 23:22:15 +00:00
unset($data[$key]);
2007-05-16 19:20:55 +00:00
}
2006-07-21 23:22:15 +00:00
$array[] = $data;
2007-05-16 19:20:55 +00:00
}
2006-08-22 19:34:40 +00:00
2006-07-21 23:22:15 +00:00
$stmt->closeCursor();
return $array;
}
/**
* @return string returns a string representation of this object
*/
2006-12-29 14:40:47 +00:00
public function __toString()
{
return Doctrine_Lib::formatSql($this->getQuery());
}
2006-07-21 23:22:15 +00:00
}