new DQL core now supports FROM and SELECT parts
This commit is contained in:
parent
fa2da84ba6
commit
9f93dbf1ee
@ -364,6 +364,7 @@ class Doctrine_Hydrate2
|
|||||||
throw new Doctrine_Hydrate_Exception("Couldn't execute query. Component alias map was empty.");
|
throw new Doctrine_Hydrate_Exception("Couldn't execute query. Component alias map was empty.");
|
||||||
}
|
}
|
||||||
// initialize some variables used within the main loop
|
// initialize some variables used within the main loop
|
||||||
|
reset($this->_aliasMap);
|
||||||
$rootMap = current($this->_aliasMap);
|
$rootMap = current($this->_aliasMap);
|
||||||
$rootAlias = key($this->_aliasMap);
|
$rootAlias = key($this->_aliasMap);
|
||||||
$coll = new Doctrine_Collection2($rootMap['table']);
|
$coll = new Doctrine_Collection2($rootMap['table']);
|
||||||
@ -518,12 +519,13 @@ class Doctrine_Hydrate2
|
|||||||
// get the inheritance maps
|
// get the inheritance maps
|
||||||
$array = array();
|
$array = array();
|
||||||
|
|
||||||
foreach ($this->tables as $alias => $table) {
|
foreach ($this->_aliasMap as $componentAlias => $data) {
|
||||||
$array[$alias][] = $table->inheritanceMap;
|
$tableAlias = $this->getTableAlias($componentAlias);
|
||||||
|
$array[$tableAlias][] = $data['table']->inheritanceMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply inheritance maps
|
// apply inheritance maps
|
||||||
$str = "";
|
$str = '';
|
||||||
$c = array();
|
$c = array();
|
||||||
|
|
||||||
$index = 0;
|
$index = 0;
|
||||||
@ -594,19 +596,6 @@ class Doctrine_Hydrate2
|
|||||||
$stmt->closeCursor();
|
$stmt->closeCursor();
|
||||||
return $array;
|
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
|
* @return string returns a string representation of this object
|
||||||
*/
|
*/
|
||||||
|
@ -371,7 +371,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
*/
|
*/
|
||||||
public function count($params = array())
|
public function count($params = array())
|
||||||
{
|
{
|
||||||
$parts_old = $this->parts;
|
$oldParts = $this->parts;
|
||||||
|
|
||||||
$this->remove('select');
|
$this->remove('select');
|
||||||
$join = $this->join;
|
$join = $this->join;
|
||||||
@ -400,12 +400,13 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
if( ! empty($having))
|
if( ! empty($having))
|
||||||
$q .= ' HAVING ' . implode(' AND ',$having);
|
$q .= ' HAVING ' . implode(' AND ',$having);
|
||||||
|
|
||||||
if( ! is_array($params))
|
if( ! is_array($params)) {
|
||||||
$params = array($params);
|
$params = array($params);
|
||||||
|
}
|
||||||
|
|
||||||
$params = array_merge($this->params, $params);
|
$params = array_merge($this->params, $params);
|
||||||
|
|
||||||
$this->parts = $parts_old;
|
$this->parts = $oldParts;
|
||||||
return (int) $this->getConnection()->fetchOne($q, $params);
|
return (int) $this->getConnection()->fetchOne($q, $params);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -622,7 +623,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
case self::SELECT:
|
case self::SELECT:
|
||||||
$distinct = ($this->isDistinct()) ? 'DISTINCT ' : '';
|
$distinct = ($this->isDistinct()) ? 'DISTINCT ' : '';
|
||||||
|
|
||||||
$q = 'SELECT '.$distinct.implode(', ', $this->parts['select']).' FROM ';
|
$q = 'SELECT ' . $distinct . implode(', ', $this->parts['select']) . ' FROM ';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return $q;
|
return $q;
|
||||||
@ -662,9 +663,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$q = $this->getQueryBase();
|
$q = $this->getQueryBase();
|
||||||
$q .= array_shift($this->parts['from']);
|
|
||||||
print $q;
|
foreach ($this->parts['from'] as $k => $part) {
|
||||||
foreach ($this->parts['from'] as $part) {
|
if ($k === 0) {
|
||||||
|
$q .= $part;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// preserve LEFT JOINs only if needed
|
// preserve LEFT JOINs only if needed
|
||||||
|
|
||||||
@ -1046,12 +1050,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
$tmp = explode(' ', $path);
|
$tmp = explode(' ', $path);
|
||||||
$componentAlias = (count($tmp) > 1) ? end($tmp) : false;
|
$componentAlias = (count($tmp) > 1) ? end($tmp) : false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$e = preg_split("/[.:]/", $tmp[0], -1);
|
$e = preg_split("/[.:]/", $tmp[0], -1);
|
||||||
|
|
||||||
$fullPath = $tmp[0];
|
$fullPath = $tmp[0];
|
||||||
$prevPath = '';
|
$prevPath = '';
|
||||||
|
$fullLength = strlen($fullPath);
|
||||||
|
|
||||||
if (isset($this->_aliasMap[$e[0]])) {
|
if (isset($this->_aliasMap[$e[0]])) {
|
||||||
$table = $this->_aliasMap[$e[0]]['table'];
|
$table = $this->_aliasMap[$e[0]]['table'];
|
||||||
|
|
||||||
@ -1059,23 +1063,27 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($e as $key => $name) {
|
foreach ($e as $key => $name) {
|
||||||
|
// get length of the previous path
|
||||||
|
$length = strlen($prevPath);
|
||||||
|
|
||||||
// build the current component path
|
// build the current component path
|
||||||
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
|
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
|
||||||
|
|
||||||
// if an alias is not given use the current path as an alias identifier
|
$delimeter = substr($fullPath, $length, 1);
|
||||||
if ($prevPath !== $fullPath || ! $componentAlias) {
|
|
||||||
$componentAlias = $prevPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
// load fields if necessary
|
// if an alias is not given use the current path as an alias identifier
|
||||||
if ($loadFields) {
|
if (strlen($prevPath) !== $fullLength || ! $componentAlias) {
|
||||||
$this->pendingFields[$componentAlias] = array('*');
|
$componentAlias = $prevPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! isset($table)) {
|
if ( ! isset($table)) {
|
||||||
// process the root of the path
|
// process the root of the path
|
||||||
$table = $this->loadRoot($name, $componentAlias);
|
$table = $this->loadRoot($name, $componentAlias);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|
||||||
|
$join = ($delimeter == ':') ? 'INNER JOIN ' : 'LEFT JOIN ';
|
||||||
|
|
||||||
$relation = $table->getRelation($name);
|
$relation = $table->getRelation($name);
|
||||||
$this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
|
$this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
|
||||||
'parent' => $parent,
|
'parent' => $parent,
|
||||||
@ -1108,7 +1116,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
|
|
||||||
$assocAlias = $this->getShortAlias($assocPath, $asf->getTableName());
|
$assocAlias = $this->getShortAlias($assocPath, $asf->getTableName());
|
||||||
|
|
||||||
$queryPart = 'LEFT JOIN ' . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.'
|
$queryPart = $join . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.'
|
||||||
. $table->getIdentifier() . ' = '
|
. $table->getIdentifier() . ' = '
|
||||||
. $assocAlias . '.' . $relation->getLocal();
|
. $assocAlias . '.' . $relation->getLocal();
|
||||||
|
|
||||||
@ -1119,7 +1127,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
|
|
||||||
$this->parts['from'][] = $queryPart;
|
$this->parts['from'][] = $queryPart;
|
||||||
|
|
||||||
$queryPart = 'LEFT JOIN ' . $foreignSql . ' ON ' . $foreignAlias . '.'
|
$queryPart = $join . $foreignSql . ' ON ' . $foreignAlias . '.'
|
||||||
. $relation->getTable()->getIdentifier() . ' = '
|
. $relation->getTable()->getIdentifier() . ' = '
|
||||||
. $assocAlias . '.' . $relation->getForeign()
|
. $assocAlias . '.' . $relation->getForeign()
|
||||||
. $joinCondition;
|
. $joinCondition;
|
||||||
@ -1130,7 +1138,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$queryPart = 'LEFT JOIN ' . $localSql
|
$queryPart = $join . $foreignSql
|
||||||
. ' ON ' . $localAlias . '.'
|
. ' ON ' . $localAlias . '.'
|
||||||
. $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign()
|
. $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign()
|
||||||
. $joinCondition;
|
. $joinCondition;
|
||||||
@ -1138,9 +1146,24 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
|
|||||||
$this->parts['from'][] = $queryPart;
|
$this->parts['from'][] = $queryPart;
|
||||||
}
|
}
|
||||||
if ($loadFields) {
|
if ($loadFields) {
|
||||||
|
|
||||||
|
$restoreState = false;
|
||||||
|
// load fields if necessary
|
||||||
|
if ($loadFields && empty($this->pendingFields)) {
|
||||||
|
$this->pendingFields[$componentAlias] = array('*');
|
||||||
|
|
||||||
|
$restoreState = true;
|
||||||
|
}
|
||||||
|
|
||||||
if(isset($this->pendingFields[$componentAlias])) {
|
if(isset($this->pendingFields[$componentAlias])) {
|
||||||
$this->processPendingFields($componentAlias);
|
$this->processPendingFields($componentAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($restoreState) {
|
||||||
|
$this->pendingFields = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if(isset($this->pendingAggregates[$componentAlias]) || isset($this->pendingAggregates[0])) {
|
if(isset($this->pendingAggregates[$componentAlias]) || isset($this->pendingAggregates[0])) {
|
||||||
$this->processPendingAggregates($componentAlias);
|
$this->processPendingAggregates($componentAlias);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user