diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index e8c4a83c1..2d5fa7c3f 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -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 diff --git a/lib/Doctrine/Hydrate/Alias.php b/lib/Doctrine/Hydrate/Alias.php new file mode 100644 index 000000000..3e36ef5a6 --- /dev/null +++ b/lib/Doctrine/Hydrate/Alias.php @@ -0,0 +1,94 @@ +. + */ +/** + * 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 + */ +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); + } +} diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 0a9ae0000..b46543d20 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -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;