2007-05-28 00:00:53 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Hydrate is a base class for Doctrine_RawSql and Doctrine_Query.
|
|
|
|
* Its purpose is to populate object graphs.
|
2007-07-27 19:58:37 +04:00
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Hydrate
|
2007-05-28 00:00:53 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
2007-10-08 00:58:08 +04:00
|
|
|
class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializable
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* QUERY TYPE CONSTANTS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constant for SELECT queries
|
|
|
|
*/
|
|
|
|
const SELECT = 0;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* constant for DELETE queries
|
|
|
|
*/
|
|
|
|
const DELETE = 1;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* constant for UPDATE queries
|
|
|
|
*/
|
|
|
|
const UPDATE = 2;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* constant for INSERT queries
|
|
|
|
*/
|
|
|
|
const INSERT = 3;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* constant for CREATE queries
|
|
|
|
*/
|
|
|
|
const CREATE = 4;
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $params query input parameters
|
|
|
|
*/
|
2007-09-02 13:28:38 +04:00
|
|
|
protected $_params = array('where' => array(),
|
|
|
|
'set' => array(),
|
|
|
|
'having' => array());
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Connection $conn Doctrine_Connection object
|
|
|
|
*/
|
|
|
|
protected $_conn;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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
|
|
|
|
*/
|
|
|
|
protected $_view;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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
|
|
|
|
*
|
|
|
|
* agg the aggregates of this component
|
2007-09-02 02:22:54 +04:00
|
|
|
*
|
|
|
|
* map the name of the column / aggregate value this
|
|
|
|
* component is mapped to a collection
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
|
|
|
protected $_aliasMap = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected $pendingAggregates = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $aggregateMap an array containing all aggregate aliases, keys as dql aliases
|
|
|
|
* and values as sql aliases
|
|
|
|
*/
|
|
|
|
protected $aggregateMap = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $_options an array of options
|
|
|
|
*/
|
|
|
|
protected $_options = array(
|
|
|
|
'fetchMode' => Doctrine::FETCH_RECORD,
|
|
|
|
'parserCache' => false,
|
|
|
|
'resultSetCache' => false,
|
|
|
|
);
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-26 00:08:16 +04:00
|
|
|
/**
|
|
|
|
* @var string $_sql cached SQL query
|
|
|
|
*/
|
|
|
|
protected $_sql;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $parts SQL query string parts
|
|
|
|
*/
|
|
|
|
protected $parts = array(
|
|
|
|
'select' => array(),
|
|
|
|
'distinct' => false,
|
|
|
|
'forUpdate' => false,
|
|
|
|
'from' => array(),
|
|
|
|
'set' => array(),
|
|
|
|
'join' => array(),
|
|
|
|
'where' => array(),
|
|
|
|
'groupby' => array(),
|
|
|
|
'having' => array(),
|
|
|
|
'orderby' => array(),
|
|
|
|
'limit' => false,
|
|
|
|
'offset' => false,
|
|
|
|
);
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var integer $type the query type
|
|
|
|
*
|
|
|
|
* @see Doctrine_Query::* constants
|
|
|
|
*/
|
|
|
|
protected $type = self::SELECT;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-05-28 00:00:53 +04:00
|
|
|
protected $_cache;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-01 17:46:34 +04:00
|
|
|
/**
|
|
|
|
* The current hydration mode.
|
|
|
|
*/
|
2007-09-05 20:05:49 +04:00
|
|
|
protected $_hydrationMode = Doctrine::HYDRATE_RECORD;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 16:45:26 +04:00
|
|
|
/**
|
|
|
|
* @var boolean $_expireCache a boolean value that indicates whether or not to force cache expiration
|
|
|
|
*/
|
|
|
|
protected $_expireCache = false;
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-29 16:45:26 +04:00
|
|
|
protected $_timeToLive;
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-06-29 16:45:26 +04:00
|
|
|
protected $_tableAliases = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param Doctrine_Connection|null $connection
|
|
|
|
*/
|
|
|
|
public function __construct($connection = null)
|
|
|
|
{
|
|
|
|
if ( ! ($connection instanceof Doctrine_Connection)) {
|
|
|
|
$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
|
|
|
|
}
|
|
|
|
$this->_conn = $connection;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-14 01:51:03 +04:00
|
|
|
/**
|
|
|
|
* getRootAlias
|
|
|
|
* returns the alias of the the root component
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRootAlias()
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! $this->_aliasMap) {
|
2007-08-14 22:25:06 +04:00
|
|
|
$this->getSql();
|
|
|
|
}
|
|
|
|
|
2007-07-14 01:51:03 +04:00
|
|
|
reset($this->_aliasMap);
|
|
|
|
|
|
|
|
return key($this->_aliasMap);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-14 01:51:03 +04:00
|
|
|
/**
|
|
|
|
* getRootDeclaration
|
|
|
|
* returns the root declaration
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRootDeclaration()
|
|
|
|
{
|
|
|
|
$map = reset($this->_aliasMap);
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-19 03:42:30 +04:00
|
|
|
/**
|
|
|
|
* getRoot
|
|
|
|
* returns the root component for this object
|
|
|
|
*
|
|
|
|
* @return Doctrine_Table root components table
|
|
|
|
*/
|
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
$map = reset($this->_aliasMap);
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-19 03:42:30 +04:00
|
|
|
if ( ! isset($map['table'])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception('Root component not initialized.');
|
|
|
|
}
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-19 03:42:30 +04:00
|
|
|
return $map['table'];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-19 03:42:30 +04:00
|
|
|
/**
|
|
|
|
* getSql
|
|
|
|
* return the sql associated with this object
|
|
|
|
*
|
|
|
|
* @return string sql query string
|
|
|
|
*/
|
2007-05-28 00:00:53 +04:00
|
|
|
public function getSql()
|
|
|
|
{
|
|
|
|
return $this->getQuery();
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
/**
|
|
|
|
* useCache
|
|
|
|
*
|
|
|
|
* @param Doctrine_Cache_Interface|bool $driver cache driver
|
|
|
|
* @param integer $timeToLive how long the cache entry is valid
|
|
|
|
* @return Doctrine_Hydrate this object
|
|
|
|
*/
|
|
|
|
public function useCache($driver = true, $timeToLive = null)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-07-27 19:58:37 +04:00
|
|
|
if ($driver !== null) {
|
2007-06-29 14:18:05 +04:00
|
|
|
if ($driver !== true) {
|
|
|
|
if ( ! ($driver instanceof Doctrine_Cache_Interface)) {
|
|
|
|
$msg = 'First argument should be instance of Doctrine_Cache_Interface or null.';
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
throw new Doctrine_Hydrate_Exception($msg);
|
|
|
|
}
|
|
|
|
}
|
2007-07-27 19:58:37 +04:00
|
|
|
}
|
2007-06-29 14:18:05 +04:00
|
|
|
$this->_cache = $driver;
|
|
|
|
|
2007-06-29 16:45:26 +04:00
|
|
|
return $this->setCacheLifeSpan($timeToLive);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
/**
|
2007-06-29 16:45:26 +04:00
|
|
|
* expireCache
|
|
|
|
*
|
|
|
|
* @param boolean $expire whether or not to force cache expiration
|
|
|
|
* @return Doctrine_Hydrate this object
|
|
|
|
*/
|
|
|
|
public function expireCache($expire = true)
|
|
|
|
{
|
|
|
|
$this->_expireCache = true;
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-29 16:45:26 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 16:45:26 +04:00
|
|
|
/**
|
|
|
|
* setCacheLifeSpan
|
2007-06-29 14:18:05 +04:00
|
|
|
*
|
|
|
|
* @param integer $timeToLive how long the cache entry is valid
|
|
|
|
* @return Doctrine_Hydrate this object
|
|
|
|
*/
|
2007-06-29 16:45:26 +04:00
|
|
|
public function setCacheLifeSpan($timeToLive)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-07-27 19:58:37 +04:00
|
|
|
if ($timeToLive !== null) {
|
|
|
|
$timeToLive = (int) $timeToLive;
|
|
|
|
}
|
2007-06-29 14:18:05 +04:00
|
|
|
$this->_timeToLive = $timeToLive;
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
/**
|
|
|
|
* getCacheDriver
|
|
|
|
* returns the cache driver associated with this object
|
|
|
|
*
|
|
|
|
* @return Doctrine_Cache_Interface|boolean|null cache driver
|
|
|
|
*/
|
|
|
|
public function getCacheDriver()
|
|
|
|
{
|
2007-07-27 19:58:37 +04:00
|
|
|
if ($this->_cache instanceof Doctrine_Cache_Interface) {
|
|
|
|
return $this->_cache;
|
|
|
|
} else {
|
|
|
|
return $this->_conn->getCacheDriver();
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-01 17:46:34 +04:00
|
|
|
/**
|
|
|
|
* Sets the fetchmode.
|
|
|
|
*
|
2007-09-05 20:05:49 +04:00
|
|
|
* @param integer $fetchmode One of the Doctrine::HYDRATE_* constants.
|
2007-07-01 17:46:34 +04:00
|
|
|
*/
|
|
|
|
public function setHydrationMode($hydrationMode)
|
|
|
|
{
|
|
|
|
$this->_hydrationMode = $hydrationMode;
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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);
|
|
|
|
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* 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-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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)
|
|
|
|
{
|
|
|
|
if (isset($this->_tableAliases[$tableAlias])) {
|
|
|
|
// generate a new alias
|
|
|
|
$name = substr($tableAlias, 0, 1);
|
|
|
|
$i = ((int) substr($tableAlias, 1));
|
|
|
|
|
|
|
|
if ($i == 0) {
|
|
|
|
$i = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newIndex = ($this->_tableAliasSeeds[$name] + $i);
|
|
|
|
|
|
|
|
return $name . $newIndex;
|
|
|
|
}
|
|
|
|
|
2007-06-22 21:38:08 +04:00
|
|
|
return $tableAlias;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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)
|
|
|
|
{
|
|
|
|
return (isset($this->_tableAliases[$tableAlias]));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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
|
|
|
|
*/
|
|
|
|
public function getComponentAlias($tableAlias)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_tableAliases[$tableAlias])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
|
|
|
|
}
|
|
|
|
return $this->_tableAliases[$tableAlias];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_tableAliasSeeds[$tableAlias])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return $this->_tableAliasSeeds[$tableAlias];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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)
|
|
|
|
{
|
|
|
|
$char = strtolower(substr($tableName, 0, 1));
|
|
|
|
|
|
|
|
$alias = $char;
|
|
|
|
|
|
|
|
if ( ! isset($this->_tableAliasSeeds[$alias])) {
|
|
|
|
$this->_tableAliasSeeds[$alias] = 1;
|
|
|
|
}
|
2007-07-14 01:51:03 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
while (isset($this->_tableAliases[$alias])) {
|
2007-07-14 01:51:03 +04:00
|
|
|
if ( ! isset($this->_tableAliasSeeds[$alias])) {
|
|
|
|
$this->_tableAliasSeeds[$alias] = 1;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
$alias = $char . ++$this->_tableAliasSeeds[$alias];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_tableAliases[$alias] = $componentAlias;
|
|
|
|
|
|
|
|
return $alias;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getTableAliases
|
|
|
|
* returns all table aliases
|
|
|
|
*
|
|
|
|
* @return array table aliases as an array
|
|
|
|
*/
|
|
|
|
public function getTableAliases()
|
|
|
|
{
|
|
|
|
return $this->_tableAliases;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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)
|
|
|
|
{
|
|
|
|
$this->_tableAliases[$tableAlias] = $componentAlias;
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getTableAlias
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function getTableAlias($componentAlias, $tableName = null)
|
|
|
|
{
|
|
|
|
$alias = array_search($componentAlias, $this->_tableAliases);
|
|
|
|
|
|
|
|
if ($alias !== false) {
|
|
|
|
return $alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tableName === null) {
|
|
|
|
throw new Doctrine_Hydrate_Exception("Couldn't get short alias for " . $componentAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->generateTableAlias($componentAlias, $tableName);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->parts[$name])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name);
|
|
|
|
}
|
2007-06-27 21:41:02 +04:00
|
|
|
if (is_array($part)) {
|
|
|
|
$this->parts[$name] = array_merge($this->parts[$name], $part);
|
|
|
|
} else {
|
|
|
|
$this->parts[$name][] = $part;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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
|
|
|
|
*/
|
|
|
|
public function getQueryPart($part)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->parts[$part])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parts[$part];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* 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 {
|
2007-06-22 21:38:08 +04:00
|
|
|
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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
|
|
|
|
*/
|
|
|
|
public function setQueryPart($name, $part)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->parts[$name])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name !== 'limit' && $name !== 'offset') {
|
2007-06-27 21:41:02 +04:00
|
|
|
if (is_array($part)) {
|
|
|
|
$this->parts[$name] = $part;
|
|
|
|
} else {
|
|
|
|
$this->parts[$name] = array($part);
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
} else {
|
|
|
|
$this->parts[$name] = $part;
|
|
|
|
}
|
2007-06-25 01:50:19 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-25 01:50:19 +04:00
|
|
|
/**
|
|
|
|
* hasAliasDeclaration
|
|
|
|
* whether or not this object has a declaration for given component alias
|
|
|
|
*
|
|
|
|
* @param string $componentAlias the component alias the retrieve the declaration from
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasAliasDeclaration($componentAlias)
|
|
|
|
{
|
|
|
|
return isset($this->_aliasMap[$componentAlias]);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* copyAliases
|
|
|
|
* copy aliases from another Hydrate object
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function copyAliases(Doctrine_Hydrate $query)
|
|
|
|
{
|
|
|
|
$this->_tableAliases = $query->_tableAliases;
|
2007-07-14 01:51:03 +04:00
|
|
|
$this->_aliasMap = $query->_aliasMap;
|
|
|
|
$this->_tableAliasSeeds = $query->_tableAliasSeeds;
|
2007-05-28 00:00:53 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* createSubquery
|
|
|
|
* creates a subquery
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* limitSubqueryUsed
|
|
|
|
* whether or not limit subquery was used
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isLimitSubqueryUsed()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* clear
|
|
|
|
* resets all the variables
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function clear()
|
|
|
|
{
|
|
|
|
$this->parts = array(
|
|
|
|
'select' => array(),
|
|
|
|
'distinct' => false,
|
|
|
|
'forUpdate' => false,
|
|
|
|
'from' => array(),
|
|
|
|
'set' => array(),
|
|
|
|
'join' => array(),
|
|
|
|
'where' => array(),
|
|
|
|
'groupby' => array(),
|
|
|
|
'having' => array(),
|
|
|
|
'orderby' => array(),
|
|
|
|
'limit' => false,
|
|
|
|
'offset' => false,
|
|
|
|
);
|
|
|
|
$this->inheritanceApplied = false;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getConnection
|
|
|
|
*
|
|
|
|
* @return Doctrine_Connection
|
|
|
|
*/
|
|
|
|
public function getConnection()
|
|
|
|
{
|
|
|
|
return $this->_conn;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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
|
|
|
|
*/
|
|
|
|
public function setView(Doctrine_View $view)
|
|
|
|
{
|
|
|
|
$this->_view = $view;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getParams
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getParams()
|
|
|
|
{
|
2007-09-02 13:28:38 +04:00
|
|
|
return array_merge($this->_params['set'], $this->_params['where'], $this->_params['having']);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* setParams
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function setParams(array $params = array()) {
|
|
|
|
$this->_params = $params;
|
|
|
|
}
|
|
|
|
public function convertEnums($params)
|
|
|
|
{
|
|
|
|
return $params;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* setAliasMap
|
|
|
|
* sets the whole component alias map
|
|
|
|
*
|
|
|
|
* @param array $map alias map
|
|
|
|
* @return Doctrine_Hydrate this object
|
|
|
|
*/
|
|
|
|
public function setAliasMap(array $map)
|
|
|
|
{
|
|
|
|
$this->_aliasMap = $map;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getAliasMap
|
|
|
|
* returns the component alias map
|
|
|
|
*
|
|
|
|
* @return array component alias map
|
|
|
|
*/
|
|
|
|
public function getAliasMap()
|
|
|
|
{
|
|
|
|
return $this->_aliasMap;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
/**
|
|
|
|
* getCachedForm
|
|
|
|
* returns the cached form of this query for given resultSet
|
|
|
|
*
|
|
|
|
* @param array $resultSet
|
|
|
|
* @return string serialized string representation of this query
|
|
|
|
*/
|
2007-05-28 00:00:53 +04: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()));
|
|
|
|
}
|
2007-07-01 17:46:34 +04:00
|
|
|
public function _execute($params)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-02 13:28:38 +04:00
|
|
|
$params = $this->_conn->convertBooleans($params);
|
2007-06-26 00:08:16 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
if ( ! $this->_view) {
|
|
|
|
$query = $this->getQuery($params);
|
|
|
|
} else {
|
|
|
|
$query = $this->_view->getSelectSql();
|
|
|
|
}
|
|
|
|
|
2007-06-26 00:08:16 +04:00
|
|
|
$params = $this->convertEnums($params);
|
2007-06-13 15:02:30 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
if ($this->isLimitSubqueryUsed() &&
|
2007-06-25 21:48:44 +04:00
|
|
|
$this->_conn->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') {
|
2007-05-28 00:00:53 +04:00
|
|
|
|
|
|
|
$params = array_merge($params, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->type !== self::SELECT) {
|
|
|
|
return $this->_conn->exec($query, $params);
|
|
|
|
}
|
|
|
|
|
2007-06-26 00:08:16 +04:00
|
|
|
$stmt = $this->_conn->execute($query, $params);
|
2007-05-28 23:03:57 +04:00
|
|
|
return $stmt;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* execute
|
|
|
|
* executes the query and populates the data set
|
|
|
|
*
|
|
|
|
* @param string $params
|
|
|
|
* @return Doctrine_Collection the root collection
|
|
|
|
*/
|
2007-07-01 17:46:34 +04:00
|
|
|
public function execute($params = array(), $hydrationMode = null)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-10-06 02:31:02 +04:00
|
|
|
$params = array_merge($this->_params['set'],
|
|
|
|
$this->_params['where'],
|
|
|
|
$this->_params['having'],
|
|
|
|
$params);
|
2007-07-27 19:58:37 +04:00
|
|
|
if ($this->_cache) {
|
|
|
|
$cacheDriver = $this->getCacheDriver();
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
$dql = $this->getDql();
|
|
|
|
// calculate hash for dql query
|
2007-06-29 14:18:05 +04:00
|
|
|
$hash = md5($dql . var_export($params, true));
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-28 19:55:56 +04:00
|
|
|
$cached = ($this->_expireCache) ? false : $cacheDriver->fetch($hash);
|
2007-06-29 16:45:26 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-28 19:55:56 +04:00
|
|
|
if ($cached === false) {
|
2007-05-28 00:00:53 +04:00
|
|
|
// cache miss
|
2007-07-01 17:46:34 +04:00
|
|
|
$stmt = $this->_execute($params);
|
2007-09-05 20:05:49 +04:00
|
|
|
$array = $this->parseData2($stmt, Doctrine::HYDRATE_ARRAY);
|
2007-05-28 00:00:53 +04:00
|
|
|
|
|
|
|
$cached = $this->getCachedForm($array);
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-06-29 14:18:05 +04:00
|
|
|
$cacheDriver->save($hash, $cached, $this->_timeToLive);
|
2007-05-28 00:00:53 +04: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 {
|
2007-07-01 17:46:34 +04:00
|
|
|
$stmt = $this->_execute($params);
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-06-06 04:02:50 +04:00
|
|
|
if (is_integer($stmt)) {
|
|
|
|
return $stmt;
|
|
|
|
}
|
2007-10-22 20:32:07 +04:00
|
|
|
|
2007-07-01 17:46:34 +04:00
|
|
|
$array = $this->parseData2($stmt, $hydrationMode);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-05-31 21:45:07 +04:00
|
|
|
return $array;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-05-31 21:45:07 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* applyInheritance
|
|
|
|
* applies column aggregation inheritance to DQL / SQL query
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function applyInheritance()
|
|
|
|
{
|
|
|
|
// get the inheritance maps
|
|
|
|
$array = array();
|
|
|
|
|
|
|
|
foreach ($this->_aliasMap as $componentAlias => $data) {
|
|
|
|
$tableAlias = $this->getTableAlias($componentAlias);
|
|
|
|
$array[$tableAlias][] = $data['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) {
|
2007-07-07 00:55:15 +04:00
|
|
|
$identifier = $this->_conn->quoteIdentifier($tableAlias . $field);
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
if ($index > 0) {
|
2007-08-14 01:51:23 +04:00
|
|
|
$b[] = '(' . $identifier . ' = ' . $this->_conn->quote($value)
|
2007-07-07 00:55:15 +04:00
|
|
|
. ' OR ' . $identifier . ' IS NULL)';
|
2007-05-28 00:00:53 +04:00
|
|
|
} else {
|
2007-08-14 01:51:23 +04:00
|
|
|
$b[] = $identifier . ' = ' . $this->_conn->quote($value);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($b)) {
|
|
|
|
$a[] = implode(' AND ', $b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($a)) {
|
|
|
|
$c[] = implode(' AND ', $a);
|
|
|
|
}
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$str .= implode(' AND ', $c);
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-27 19:58:37 +04:00
|
|
|
/**
|
|
|
|
* fetchArray
|
|
|
|
* Convenience method to execute using array fetching as hydration mode.
|
|
|
|
*
|
|
|
|
* @param string $params
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-07-06 17:10:54 +04:00
|
|
|
public function fetchArray($params = array()) {
|
2007-09-05 20:05:49 +04:00
|
|
|
return $this->execute($params, Doctrine::HYDRATE_ARRAY);
|
2007-07-06 17:10:54 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-27 19:58:37 +04:00
|
|
|
/**
|
|
|
|
* fetchOne
|
|
|
|
* Convenience method to execute the query and return the first item
|
|
|
|
* of the collection.
|
|
|
|
*
|
|
|
|
* @param string $params Parameters
|
|
|
|
* @param int $hydrationMode Hydration mode
|
|
|
|
* @return mixed Array or Doctrine_Collection or false if no result.
|
|
|
|
*/
|
|
|
|
public function fetchOne($params = array(), $hydrationMode = null)
|
|
|
|
{
|
|
|
|
if (is_null($hydrationMode)) {
|
|
|
|
$hydrationMode = $this->_hydrationMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
$collection = $this->execute($params, $hydrationMode);
|
|
|
|
|
2007-09-06 17:18:46 +04:00
|
|
|
if (count($collection) === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-27 19:58:37 +04:00
|
|
|
switch ($hydrationMode) {
|
2007-09-05 20:05:49 +04:00
|
|
|
case Doctrine::HYDRATE_RECORD:
|
2007-09-06 17:18:46 +04:00
|
|
|
return $collection->getFirst();
|
2007-09-05 20:05:49 +04:00
|
|
|
case Doctrine::HYDRATE_ARRAY:
|
2007-09-02 15:29:14 +04:00
|
|
|
return array_shift($collection);
|
2007-07-27 19:58:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 23:03:57 +04:00
|
|
|
/**
|
|
|
|
* parseData
|
|
|
|
* parses the data returned by statement object
|
|
|
|
*
|
2007-06-05 02:37:27 +04:00
|
|
|
* This is method defines the core of Doctrine object population algorithm
|
|
|
|
* hence this method strives to be as fast as possible
|
|
|
|
*
|
|
|
|
* The key idea is the loop over the rowset only once doing all the needed operations
|
|
|
|
* within this massive loop.
|
|
|
|
*
|
2007-05-28 23:03:57 +04:00
|
|
|
* @param mixed $stmt
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-07-01 17:46:34 +04:00
|
|
|
public function parseData2($stmt, $hydrationMode)
|
2007-05-28 23:03:57 +04:00
|
|
|
{
|
2007-10-22 20:32:07 +04:00
|
|
|
if ($hydrationMode == Doctrine::HYDRATE_NONE) {
|
|
|
|
return $stmt->fetchAll(PDO::FETCH_NUM);
|
|
|
|
}
|
|
|
|
|
2007-05-28 23:03:57 +04:00
|
|
|
$cache = array();
|
|
|
|
$rootMap = reset($this->_aliasMap);
|
|
|
|
$rootAlias = key($this->_aliasMap);
|
2007-05-31 21:45:07 +04:00
|
|
|
$componentName = $rootMap['table']->getComponentName();
|
2007-10-21 13:00:40 +04:00
|
|
|
$isSimpleQuery = count($this->_aliasMap) <= 1;
|
|
|
|
|
2007-07-01 17:46:34 +04:00
|
|
|
if ($hydrationMode === null) {
|
|
|
|
$hydrationMode = $this->_hydrationMode;
|
|
|
|
}
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-09-05 20:05:49 +04:00
|
|
|
if ($hydrationMode === Doctrine::HYDRATE_ARRAY) {
|
2007-05-31 21:45:07 +04:00
|
|
|
$driver = new Doctrine_Hydrate_Array();
|
|
|
|
} else {
|
|
|
|
$driver = new Doctrine_Hydrate_Record();
|
|
|
|
}
|
|
|
|
|
|
|
|
$array = $driver->getElementCollection($componentName);
|
2007-05-28 23:03:57 +04:00
|
|
|
|
2007-06-06 03:35:11 +04:00
|
|
|
if ($stmt === false || $stmt === 0) {
|
|
|
|
return $array;
|
2007-06-06 03:19:12 +04:00
|
|
|
}
|
|
|
|
|
2007-10-06 02:31:02 +04:00
|
|
|
$event = new Doctrine_Event(Doctrine_Event::HYDRATE, null);
|
|
|
|
|
2007-10-13 23:30:45 +04:00
|
|
|
// for every getRecordListener() there is a little bit
|
|
|
|
// logic behind it, hence calling it multiple times on
|
|
|
|
// large result sets can be quite expensive.
|
|
|
|
// So for efficiency we use little listener caching here
|
|
|
|
foreach ($this->_aliasMap as $alias => $data) {
|
|
|
|
$componentName = $data['table']->getComponentName();
|
|
|
|
$listeners[$componentName] = $data['table']->getRecordListener();
|
|
|
|
}
|
|
|
|
|
2007-07-06 00:03:38 +04:00
|
|
|
while ($data = $stmt->fetch(Doctrine::FETCH_ASSOC)) {
|
2007-09-01 14:41:46 +04:00
|
|
|
$currData = array();
|
|
|
|
$identifiable = array();
|
2007-06-05 02:37:27 +04:00
|
|
|
|
2007-05-28 23:03:57 +04:00
|
|
|
foreach ($data as $key => $value) {
|
2007-06-05 02:55:47 +04:00
|
|
|
|
|
|
|
// The following little cache solution ensures that field aliases are
|
|
|
|
// parsed only once. This increases speed on large result sets by an order
|
|
|
|
// of magnitude.
|
2007-05-28 23:03:57 +04:00
|
|
|
if ( ! isset($cache[$key])) {
|
|
|
|
$e = explode('__', $key);
|
|
|
|
$cache[$key]['field'] = $field = strtolower(array_pop($e));
|
2007-06-05 02:45:03 +04:00
|
|
|
$cache[$key]['alias'] = $this->_tableAliases[strtolower(implode('__', $e))];
|
|
|
|
}
|
2007-05-28 23:03:57 +04:00
|
|
|
|
|
|
|
|
2007-06-05 02:45:03 +04:00
|
|
|
$map = $this->_aliasMap[$cache[$key]['alias']];
|
|
|
|
$table = $map['table'];
|
|
|
|
$alias = $cache[$key]['alias'];
|
2007-07-06 00:03:38 +04:00
|
|
|
$field = $cache[$key]['field'];
|
2007-05-28 23:03:57 +04:00
|
|
|
|
2007-07-16 23:19:29 +04:00
|
|
|
if (isset($this->_aliasMap[$alias]['agg'][$field])) {
|
|
|
|
$field = $this->_aliasMap[$alias]['agg'][$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-28 23:03:57 +04:00
|
|
|
if ( ! isset($currData[$alias])) {
|
|
|
|
$currData[$alias] = array();
|
|
|
|
}
|
2007-06-05 02:37:27 +04:00
|
|
|
|
2007-05-28 23:03:57 +04:00
|
|
|
if ( ! isset($prev[$alias])) {
|
|
|
|
$prev[$alias] = array();
|
2007-05-31 21:45:07 +04:00
|
|
|
}
|
|
|
|
|
2007-07-06 00:03:38 +04:00
|
|
|
$currData[$alias][$field] = $table->prepareValue($field, $value);
|
2007-06-05 02:37:27 +04:00
|
|
|
if ($value !== null) {
|
|
|
|
$identifiable[$alias] = true;
|
|
|
|
}
|
2007-06-05 02:45:03 +04:00
|
|
|
}
|
2007-06-05 02:37:27 +04:00
|
|
|
|
2007-09-07 00:52:16 +04:00
|
|
|
|
2007-09-01 14:41:46 +04:00
|
|
|
// dealing with root component
|
|
|
|
$table = $this->_aliasMap[$rootAlias]['table'];
|
2007-06-03 00:33:41 +04:00
|
|
|
$componentName = $table->getComponentName();
|
2007-10-06 02:31:02 +04:00
|
|
|
$event->set('data', $currData[$rootAlias]);
|
2007-10-13 23:30:45 +04:00
|
|
|
$listeners[$componentName]->preHydrate($event);
|
2007-09-01 14:41:46 +04:00
|
|
|
$element = $driver->getElement($currData[$rootAlias], $componentName);
|
2007-05-28 23:03:57 +04:00
|
|
|
|
2007-06-07 21:04:56 +04:00
|
|
|
$oneToOne = false;
|
2007-05-31 21:45:07 +04:00
|
|
|
|
2007-10-21 13:00:40 +04:00
|
|
|
$index = $isSimpleQuery ? false : $driver->search($element, $array);
|
|
|
|
|
2007-10-06 02:31:02 +04:00
|
|
|
if ($index === false) {
|
|
|
|
$event->set('data', $element);
|
2007-10-13 23:30:45 +04:00
|
|
|
$listeners[$componentName]->postHydrate($event);
|
2007-10-06 02:31:02 +04:00
|
|
|
|
2007-09-07 00:52:16 +04:00
|
|
|
if (isset($this->_aliasMap[$rootAlias]['map'])) {
|
|
|
|
$key = $this->_aliasMap[$rootAlias]['map'];
|
|
|
|
|
2007-09-02 02:22:54 +04:00
|
|
|
if (isset($array[$key])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found non-unique key mapping.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($element[$key])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found a non-existent key.");
|
|
|
|
}
|
2007-09-07 00:52:16 +04:00
|
|
|
|
2007-09-02 02:22:54 +04:00
|
|
|
$array[$element[$key]] = $element;
|
|
|
|
} else {
|
|
|
|
$array[] = $element;
|
|
|
|
}
|
2007-09-01 14:41:46 +04:00
|
|
|
}
|
|
|
|
$this->_setLastElement($prev, $array, $index, $rootAlias, $oneToOne);
|
|
|
|
unset($currData[$rootAlias]);
|
|
|
|
|
|
|
|
foreach ($currData as $alias => $data) {
|
|
|
|
$index = false;
|
|
|
|
$map = $this->_aliasMap[$alias];
|
|
|
|
$table = $this->_aliasMap[$alias]['table'];
|
|
|
|
$componentName = $table->getComponentName();
|
2007-10-06 02:31:02 +04:00
|
|
|
$event->set('data', $data);
|
2007-10-13 23:30:45 +04:00
|
|
|
$listeners[$componentName]->preHydrate($event);
|
2007-10-06 02:31:02 +04:00
|
|
|
|
2007-09-01 14:41:46 +04:00
|
|
|
$element = $driver->getElement($data, $componentName);
|
|
|
|
|
|
|
|
$parent = $map['parent'];
|
|
|
|
$relation = $map['relation'];
|
|
|
|
$componentAlias = $map['relation']->getAlias();
|
2007-07-27 19:58:37 +04:00
|
|
|
|
2007-09-02 02:22:54 +04:00
|
|
|
if ( ! isset($prev[$parent])) {
|
2007-07-08 16:57:52 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-06-07 21:04:56 +04:00
|
|
|
|
|
|
|
// check the type of the relation
|
|
|
|
if ( ! $relation->isOneToOne()) {
|
|
|
|
// initialize the collection
|
|
|
|
|
|
|
|
if ($driver->initRelated($prev[$parent], $componentAlias)) {
|
|
|
|
|
|
|
|
// append element
|
|
|
|
if (isset($identifiable[$alias])) {
|
2007-10-21 13:00:40 +04:00
|
|
|
$index = $isSimpleQuery ? false : $driver->search($element, $prev[$parent][$componentAlias]);
|
2007-06-07 21:04:56 +04:00
|
|
|
|
|
|
|
if ($index === false) {
|
2007-10-06 02:31:02 +04:00
|
|
|
$event->set('data', $element);
|
2007-10-13 23:30:45 +04:00
|
|
|
$listeners[$componentName]->postHydrate($event);
|
2007-10-06 02:31:02 +04:00
|
|
|
|
2007-09-02 13:28:38 +04:00
|
|
|
if (isset($map['map'])) {
|
|
|
|
$key = $map['map'];
|
2007-09-02 02:22:54 +04:00
|
|
|
if (isset($prev[$parent][$componentAlias][$key])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found non-unique key mapping.");
|
|
|
|
}
|
|
|
|
if ( ! isset($element[$key])) {
|
|
|
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found a non-existent key.");
|
|
|
|
}
|
|
|
|
$prev[$parent][$componentAlias][$element[$key]] = $element;
|
|
|
|
} else {
|
|
|
|
$prev[$parent][$componentAlias][] = $element;
|
|
|
|
}
|
2007-06-05 02:37:27 +04:00
|
|
|
}
|
|
|
|
}
|
2007-06-07 21:04:56 +04:00
|
|
|
// register collection for later snapshots
|
|
|
|
$driver->registerCollection($prev[$parent][$componentAlias]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( ! isset($identifiable[$alias])) {
|
|
|
|
$prev[$parent][$componentAlias] = $driver->getNullPointer();
|
2007-05-28 23:03:57 +04:00
|
|
|
} else {
|
2007-06-05 02:37:27 +04:00
|
|
|
$prev[$parent][$componentAlias] = $element;
|
|
|
|
}
|
2007-06-07 21:04:56 +04:00
|
|
|
$oneToOne = true;
|
2007-06-05 02:37:27 +04:00
|
|
|
}
|
2007-06-07 21:04:56 +04:00
|
|
|
$coll =& $prev[$parent][$componentAlias];
|
2007-09-01 14:41:46 +04:00
|
|
|
$this->_setLastElement($prev, $coll, $index, $alias, $oneToOne);
|
2007-06-07 21:04:56 +04:00
|
|
|
}
|
2007-05-28 23:03:57 +04:00
|
|
|
}
|
|
|
|
|
2007-05-31 21:45:07 +04:00
|
|
|
$driver->flush();
|
2007-05-28 23:03:57 +04:00
|
|
|
|
|
|
|
$stmt->closeCursor();
|
2007-05-28 00:00:53 +04:00
|
|
|
return $array;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-05 02:55:47 +04:00
|
|
|
/**
|
|
|
|
* _setLastElement
|
|
|
|
*
|
|
|
|
* sets the last element of given data array / collection
|
|
|
|
* as previous element
|
|
|
|
*
|
|
|
|
* @param boolean|integer $index
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-06-07 21:04:56 +04:00
|
|
|
public function _setLastElement(&$prev, &$coll, $index, $alias, $oneToOne)
|
2007-06-05 02:55:47 +04:00
|
|
|
{
|
2007-07-27 19:58:37 +04:00
|
|
|
if ($coll === self::$_null) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-05 02:55:47 +04:00
|
|
|
if ($index !== false) {
|
|
|
|
$prev[$alias] =& $coll[$index];
|
|
|
|
} else {
|
|
|
|
// first check the count (we do not want to get the last element
|
|
|
|
// of an empty collection/array)
|
|
|
|
if (count($coll) > 0) {
|
|
|
|
if (is_array($coll)) {
|
2007-06-07 21:04:56 +04:00
|
|
|
if ($oneToOne) {
|
|
|
|
$prev[$alias] =& $coll;
|
|
|
|
} else {
|
|
|
|
end($coll);
|
|
|
|
$prev[$alias] =& $coll[key($coll)];
|
|
|
|
}
|
2007-06-05 02:55:47 +04:00
|
|
|
} else {
|
|
|
|
$prev[$alias] = $coll->getLast();
|
|
|
|
}
|
2007-07-08 16:57:52 +04:00
|
|
|
} else {
|
|
|
|
if (isset($prev[$alias])) {
|
|
|
|
unset($prev[$alias]);
|
|
|
|
}
|
2007-06-05 02:55:47 +04:00
|
|
|
}
|
2007-07-27 19:58:37 +04:00
|
|
|
}
|
2007-06-05 02:55:47 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @return string returns a string representation of this object
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return Doctrine_Lib::formatSql($this->getQuery());
|
|
|
|
}
|
2007-07-27 19:58:37 +04:00
|
|
|
}
|