1
0
mirror of synced 2024-12-13 14:56:01 +03:00

added missing class Doctrine_Hydrate_Alias, refactored query and hydrate classes

This commit is contained in:
zYne 2006-11-27 22:39:18 +00:00
parent be7931ed3a
commit 161b9125e1
3 changed files with 106 additions and 45 deletions

View File

@ -50,10 +50,6 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
* @var array $joins an array containing all table joins
*/
protected $joins = array();
/**
* @var array $data fetched data
*/
protected $data = array();
/**
* @var array $params query input parameters
*/
@ -92,7 +88,9 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
protected $pendingAggregates = array();
protected $aggregateMap = array();
/**
* @var Doctrine_Hydrate_Alias $aliasHandler
*/
protected $aliasHandler;
/**
* @var array $parts SQL query string parts
@ -229,7 +227,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
);
$this->inheritanceApplied = false;
$this->aggregate = false;
$this->data = array();
$this->collections = array();
$this->joins = array();
$this->tableIndexes = array();
@ -541,16 +539,6 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
}
return false;
}
public function getShortAliasIndex($alias) {
return $this->aliasHandler->getShortAliasIndex($alias);
}
public function generateShortAlias($tableName) {
return $this->aliasHandler->generateShortAlias($tableName);
}
public function getShortAlias($tableName) {
return $this->aliasHandler->getShortAlias($tableName);
}
/**
* applyInheritance
* applies column aggregation inheritance to DQL / SQL query

View File

@ -0,0 +1,94 @@
<?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_Alias
* This class handles the creation of aliases for components in DQL query
*
* @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>
*/
class Doctrine_Hydrate_Alias {
protected $shortAliases = array();
protected $shortAliasIndexes = array();
public function clear() {
$this->shortAliases = array();
$this->shortAliasIndexes = array();
}
public function generateNewAlias($alias) {
if(isset($this->shortAliases[$alias])) {
// generate a new alias
$name = substr($alias, 0, 1);
$i = ((int) substr($alias, 1));
if($i == 0)
$i = 1;
$newIndex = ($this->shortAliasIndexes[$name] + $i);
return $name . $newIndex;
}
return $alias;
}
public function hasAliasFor($tableName) {
return (isset($this->shortAliases[$tableName]));
}
public function getShortAliasIndex($alias) {
if( ! isset($this->shortAliasIndexes[$alias]))
return 0;
return $this->shortAliasIndexes[$alias];
}
public function generateShortAlias($tableName) {
$char = strtolower(substr($tableName, 0, 1));
$alias = $char;
if( ! isset($this->shortAliasIndexes[$alias]))
$this->shortAliasIndexes[$alias] = 1;
while(isset($this->shortAliases[$alias])) {
$alias = $char . ++$this->shortAliasIndexes[$alias];
}
$this->shortAliases[$alias] = $tableName;
return $alias;
}
public function getShortAlias($tableName) {
$alias = array_search($tableName, $this->shortAliases);
if($alias !== false)
return $alias;
return $this->generateShortAlias($tableName);
}
}

View File

@ -200,9 +200,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$having = $this->having;
$table = reset($this->tables);
$q = 'SELECT COUNT(DISTINCT ' . $this->getShortAlias($table->getTableName())
. '.' . $table->getIdentifier()
. ') FROM ' . $table->getTableName() . ' ' . $this->getShortAlias($table->getTableName());
$q = 'SELECT COUNT(DISTINCT ' . $this->aliasHandler->getShortAlias($table->getTableName())
. '.' . $table->getIdentifier()
. ') FROM ' . $table->getTableName() . ' ' . $this->aliasHandler->getShortAlias($table->getTableName());
foreach($join as $j) {
$q .= ' '.implode(' ',$j);
@ -528,7 +528,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
break;
}
$field = $this->getShortAlias($table->getTableName()) . '.' . $table->getIdentifier();
$field = $this->aliasHandler->getShortAlias($table->getTableName()) . '.' . $table->getIdentifier();
// only append the subquery if it actually contains something
if($subquery !== '')
@ -569,7 +569,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$k = array_keys($this->tables);
$table = $this->tables[$k[0]];
$alias = $this->getShortAlias($table->getTableName());
$alias = $this->aliasHandler->getShortAlias($table->getTableName());
$primaryKey = $alias . '.' . $table->getIdentifier();
// initialize the base of the subquery
@ -638,27 +638,6 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
return $subquery;
}
/**
public function generateNewAlias($alias) {
if(isset($this->shortAliases[$alias])) {
// generate a new alias
$name = substr($alias, 0, 1);
$i = ((int) substr($alias, 1));
if($i == 0)
$i = 1;
$newIndex = ($this->shortAliasIndexes[$name] + $i);
return $name . $newIndex;
}
return $alias;
}
*/
/**
* query the database with DQL (Doctrine Query Language)
*
@ -1072,7 +1051,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$table = $this->connection->getTable($name);
$tname = $this->getShortAlias($table->getTableName());
$tname = $this->aliasHandler->getShortAlias($table->getTableName());
if( ! isset($this->tableAliases[$currPath]))
$this->tableIndexes[$tname] = 1;
@ -1093,7 +1072,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if(isset($this->tableAliases[$prevPath])) {
$tname = $this->tableAliases[$prevPath];
} else
$tname = $this->getShortAlias($table->getTableName());
$tname = $this->aliasHandler->getShortAlias($table->getTableName());
$fk = $table->getRelation($name);
@ -1105,7 +1084,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if(isset($this->tableAliases[$currPath])) {
$tname2 = $this->tableAliases[$currPath];
} else
$tname2 = $this->generateShortAlias($original);
$tname2 = $this->aliasHandler->generateShortAlias($original);
$aliasString = $original . ' ' . $tname2;