1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/draft/new-core/Hydrate.php

614 lines
18 KiB
PHP
Raw Normal View History

2007-05-10 21:57:15 +04:00
<?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>.
*/
2007-05-11 04:19:46 +04:00
2007-05-10 21:57:15 +04: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: 1255 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
2007-05-11 04:19:46 +04:00
class Doctrine_Hydrate2
2007-05-10 21:57:15 +04: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-10 23:49:05 +04:00
2007-05-10 21:57:15 +04:00
/**
* @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;
/**
2007-05-10 23:49:05 +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
*
2007-05-11 04:19:46 +04:00
* relation the relation object owned by the parent
2007-05-10 23:49:05 +04:00
*
2007-05-11 04:19:46 +04:00
* parent the alias of the parent
2007-05-10 21:57:15 +04:00
*/
2007-05-10 23:49:05 +04:00
protected $_aliasMap = array();
2007-05-10 21:57:15 +04:00
/**
* @var array $tableAliases
*/
protected $tableAliases = array();
/**
2007-05-10 23:49:05 +04:00
*
2007-05-10 21:57:15 +04:00
*/
protected $pendingAggregates = array();
2007-05-10 23:49:05 +04:00
/**
*
*/
2007-05-10 21:57:15 +04:00
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();
}
/**
* getTableAliases
*
* @return array
*/
public function getTableAliases()
{
return $this->tableAliases;
}
2007-05-11 01:31:35 +04:00
public function setTableAliases(array $aliases)
{
$this->tableAliases = $aliases;
}
2007-05-10 21:57:15 +04:00
/**
* copyAliases
*
* @return void
*/
public function copyAliases(Doctrine_Hydrate $query)
{
$this->tableAliases = $query->getTableAliases();
$this->aliasHandler = $query->aliasHandler;
return $this;
}
/**
* 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;
}
/**
* limitSubqueryUsed
*
* @return boolean
*/
public function isLimitSubqueryUsed()
{
return false;
}
2007-05-12 01:33:31 +04:00
public function getQueryPart($part)
{
if ( ! isset($this->parts[$part])) {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $part);
}
return $this->parts[$part];
}
2007-05-10 21:57:15 +04:00
/**
* 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->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->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;
}
/**
* setParams
*
* @param array $params
*/
public function setParams(array $params = array()) {
$this->params = $params;
}
/**
2007-05-11 00:09:32 +04:00
* _fetch
*
2007-05-10 21:57:15 +04:00
*
*/
2007-05-11 00:09:32 +04:00
public function _fetch($params = array(), $return = Doctrine::FETCH_RECORD)
{
2007-05-10 21:57:15 +04:00
$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);
2007-05-11 00:09:32 +04:00
return $this->parseData($stmt);
}
public function setAliasMap($map)
{
$this->_aliasMap = $map;
}
public function getAliasMap()
{
return $this->_aliasMap;
}
2007-05-11 01:31:35 +04:00
/**
* mapAggregateValues
* map the aggregate values of given dataset row to a given record
*
* @param Doctrine_Record $record
* @param array $row
2007-05-11 04:19:46 +04:00
* @return Doctrine_Record
2007-05-11 01:31:35 +04:00
*/
public function mapAggregateValues($record, array $row)
{
// aggregate values have numeric keys
if (isset($row[0])) {
// 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);
}
}
return $record;
}
2007-05-11 00:09:32 +04:00
/**
* 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)
{
2007-05-11 01:31:35 +04:00
$array = (array) $this->_fetch($params = array(), $return = Doctrine::FETCH_RECORD);
if (empty($this->_aliasMap)) {
throw new Doctrine_Hydrate_Exception("Couldn't execute query. Component alias map was empty.");
}
2007-05-11 04:19:46 +04:00
// initialize some variables used within the main loop
2007-05-11 01:31:35 +04:00
$rootMap = current($this->_aliasMap);
2007-05-11 04:19:46 +04:00
$rootAlias = key($this->_aliasMap);
$coll = new Doctrine_Collection2($rootMap['table']);
$prev[$rootAlias] = $coll;
2007-05-10 21:57:15 +04:00
2007-05-11 01:31:35 +04:00
$prevRow = array();
2007-05-11 00:09:32 +04:00
/**
* iterate over the fetched data
* here $data is a two dimensional array
*/
foreach ($array as $data) {
/**
* remove duplicated data rows and map data into objects
*/
2007-05-11 01:31:35 +04:00
foreach ($data as $tableAlias => $row) {
2007-05-11 04:19:46 +04:00
// skip empty rows (not mappable)
2007-05-11 00:09:32 +04:00
if (empty($row)) {
continue;
}
2007-05-11 04:19:46 +04:00
// check for validity
2007-05-11 01:31:35 +04:00
if ( ! isset($this->tableAliases[$tableAlias])) {
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
}
$alias = $this->tableAliases[$tableAlias];
$map = $this->_aliasMap[$alias];
2007-05-11 04:19:46 +04:00
// initialize previous row array if not set
if ( ! isset($prevRow[$tableAlias])) {
$prevRow[$tableAlias] = array();
}
// don't map duplicate rows
2007-05-11 01:31:35 +04:00
if ($prevRow[$tableAlias] !== $row) {
// set internal data
2007-05-11 04:19:46 +04:00
$map['table']->setData($row);
2007-05-11 22:58:50 +04:00
$identifiable = $this->isIdentifiable($row, $map['table']->getIdentifier());
// only initialize record if the current data row is identifiable
if ($identifiable) {
// initialize a new record
$record = $map['table']->getRecord();
}
2007-05-11 04:19:46 +04:00
// map aggregate values (if any)
2007-05-11 01:31:35 +04:00
$this->mapAggregateValues($record, $row);
2007-05-11 04:19:46 +04:00
if ($alias == $rootAlias) {
2007-05-11 01:31:35 +04:00
// add record into root collection
2007-05-11 22:58:50 +04:00
if ($identifiable) {
$coll->add($record);
unset($prevRow);
}
2007-05-11 01:31:35 +04:00
} else {
2007-05-11 04:19:46 +04:00
$relation = $map['relation'];
$parentAlias = $map['parent'];
$parentMap = $this->_aliasMap[$parentAlias];
$parent = $prev[$parentAlias]->getLast();
// check the type of the relation
if ($relation->isOneToOne()) {
2007-05-11 22:58:50 +04:00
if ( ! $identifiable) {
continue;
}
2007-05-11 04:19:46 +04:00
$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_Collection2($parentMap['table']);
$prev[$alias]->setReference($parent, $relation);
} else {
// previous entry found from memory
$prev[$alias] = $prev[$parentAlias]->getLast()->get($relation->getAlias());
}
// add record to the current collection
2007-05-11 22:58:50 +04:00
if ($identifiable) {
$prev[$alias]->add($record);
}
2007-05-11 04:19:46 +04:00
}
// initialize the relation from parent to the current collection/record
$parent->set($relation->getAlias(), $prev[$alias]);
2007-05-11 01:31:35 +04: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-11 04:19:46 +04:00
if ($prev[$alias] !== $record) {
$prev[$alias] = $record;
2007-05-11 01:31:35 +04:00
}
}
$prevRow[$tableAlias] = $row;
2007-05-11 00:09:32 +04:00
}
2007-05-10 21:57:15 +04:00
}
2007-05-11 04:19:46 +04:00
return $coll;
2007-05-11 00:09:32 +04:00
}
2007-05-10 21:57:15 +04:00
/**
* isIdentifiable
* returns whether or not a given data row is identifiable (it contains
2007-05-11 22:58:50 +04:00
* all primary key fields specified in the second argument)
2007-05-10 21:57:15 +04:00
*
* @param array $row
2007-05-11 22:58:50 +04:00
* @param mixed $primaryKeys
2007-05-10 21:57:15 +04:00
* @return boolean
*/
2007-05-11 22:58:50 +04:00
public function isIdentifiable(array $row, $primaryKeys)
2007-05-10 21:57:15 +04:00
{
2007-05-11 22:58:50 +04:00
if (is_array($primaryKeys)) {
foreach ($primaryKeys as $id) {
if ($row[$id] == null) {
return false;
}
2007-05-10 21:57:15 +04:00
}
} else {
2007-05-11 22:58:50 +04:00
if ( ! isset($row[$primaryKeys])) {
return false;
2007-05-10 21:57:15 +04:00
}
}
2007-05-11 22:58:50 +04:00
return true;
2007-05-10 21:57:15 +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;
}
/**
* 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);
2007-05-11 00:09:32 +04:00
$field = strtolower(array_pop($e));
$tableAlias = strtolower(implode('__', $e));
2007-05-10 21:57:15 +04:00
2007-05-11 00:09:32 +04:00
$data[$tableAlias][$field] = $value;
2007-05-10 21:57:15 +04:00
unset($data[$key]);
2007-05-10 22:10:28 +04:00
}
2007-05-10 21:57:15 +04:00
$array[] = $data;
2007-05-10 22:10:28 +04:00
}
2007-05-10 21:57:15 +04:00
$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());
}
}