From 8dc99daa9f7bc850ceac7c8d608f418d4c9c31dc Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 8 Feb 2007 13:56:23 +0000 Subject: [PATCH] Refactored many classes --- lib/Doctrine/Collection.php | 3 +- lib/Doctrine/Connection/UnitOfWork.php | 4 +- lib/Doctrine/Expression.php | 2 +- lib/Doctrine/Hydrate.php | 4 +- lib/Doctrine/Query.php | 21 ++- lib/Doctrine/Record.php | 4 +- lib/Doctrine/Relation/Association/Self.php | 8 +- lib/Doctrine/Table.php | 148 +++++---------------- 8 files changed, 63 insertions(+), 131 deletions(-) diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index 2f058ed27..ec33c4e81 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -373,7 +373,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $query = "SELECT ".$fields." FROM ".$this->table->getTableName(); // apply column aggregation inheritance - foreach ($this->table->getInheritanceMap() as $k => $v) { + $map = $this->table->inheritanceMap; + foreach ($map as $k => $v) { $where[] = $k." = ?"; $params[] = $v; } diff --git a/lib/Doctrine/Connection/UnitOfWork.php b/lib/Doctrine/Connection/UnitOfWork.php index b6a3a313a..e53610251 100644 --- a/lib/Doctrine/Connection/UnitOfWork.php +++ b/lib/Doctrine/Connection/UnitOfWork.php @@ -314,7 +314,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen $table = $record->getTable(); $keys = $table->getPrimaryKeys(); - $seq = $record->getTable()->getSequenceName(); + $seq = $record->getTable()->sequenceName; if ( ! empty($seq)) { $id = $this->conn->sequence->nextId($seq); @@ -329,7 +329,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen if (empty($seq) && count($keys) == 1 && $keys[0] == $table->getIdentifier()) { if (strtolower($this->conn->getName()) == 'pgsql') { - $seq = $table->getTableName() . '_' . $keys[0]; + $seq = $table->getTableName() . '_' . $keys[0]; } $id = $this->conn->sequence->lastInsertId($seq); diff --git a/lib/Doctrine/Expression.php b/lib/Doctrine/Expression.php index bf47b84f2..d0e7c0a7b 100644 --- a/lib/Doctrine/Expression.php +++ b/lib/Doctrine/Expression.php @@ -269,7 +269,7 @@ class Doctrine_Expression extends Doctrine_Connection_Module * * @param string|array(string) strings that will be concatinated. */ - public function concat(array $args) + public function concat($args) { $cols = $this->getIdentifiers($args); return 'CONCAT(' . join(', ', $cols) . ')'; diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index 858459252..94ed25c9d 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -632,8 +632,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access $array = array(); foreach ($this->tables as $alias => $table) { - $array[$alias][] = $table->getInheritanceMap(); - }; + $array[$alias][] = $table->inheritanceMap; + } // apply inheritance maps $str = ""; diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index a9098d5e8..f75b72873 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -1346,17 +1346,20 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $this->needsSubquery = true; } - if( ! $loadFields || $fk->getTable()->usesInheritanceMap() || $joinCondition) { + + $map = $fk->getTable()->inheritanceMap; + + if( ! $loadFields || ! empty($map) || $joinCondition) { $this->subqueryAliases[] = $tname2; } - if($fk instanceof Doctrine_Relation_Association) { + if ($fk instanceof Doctrine_Relation_Association) { $asf = $fk->getAssociationFactory(); $assocTableName = $asf->getTableName(); - if( ! $loadFields || $fk->getTable()->usesInheritanceMap() || $joinCondition) { + if( ! $loadFields || ! empty($map) || $joinCondition) { $this->subqueryAliases[] = $assocTableName; } @@ -1368,15 +1371,25 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $assocAlias = $this->aliasHandler->generateShortAlias($assocTableName); } - $this->parts['join'][$tname][$assocTableName] = $join . $assocTableName . ' ' . $assocAlias .' ON ' . $tname . '.' + $this->parts['join'][$tname][$assocTableName] = $join . $assocTableName . ' ' . $assocAlias . ' ON ' . $tname . '.' . $table->getIdentifier() . ' = ' . $assocAlias . '.' . $fk->getLocal(); + + if ($fk instanceof Doctrine_Relation_Association_Self) { + $this->parts['join'][$tname][$assocTableName] .= ' OR ' . $tname . '.' . $table->getIdentifier() . ' = ' + . $assocAlias . '.' . $fk->getForeign(); + } $this->parts['join'][$tname][$tname2] = $join . $aliasString . ' ON ' . $tname2 . '.' . $fk->getTable()->getIdentifier() . ' = ' . $assocAlias . '.' . $fk->getForeign() . $joinCondition; + if ($fk instanceof Doctrine_Relation_Association_Self) { + $this->parts['join'][$tname][$tname2] .= ' OR ' . $tname2 . '.' . $table->getIdentifier() . ' = ' + . $assocAlias . '.' . $fk->getLocal(); + } + } else { $this->parts['join'][$tname][$tname2] = $join . $aliasString . ' ON ' . $tname . '.' diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 247cd486d..7b283ebab 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1068,7 +1068,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } } - foreach ($this->_table->getInheritanceMap() as $k => $v) { + foreach ($this->_table->inheritanceMap as $k => $v) { $old = $this->get($k, false); if ((string) $old !== (string) $v || $old === null) { @@ -1477,7 +1477,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } public function setTableName($tableName) { - $this->_table->setTableName($tableName); + $this->_table->tableName = $tableName; } public function setInheritanceMap($map) { diff --git a/lib/Doctrine/Relation/Association/Self.php b/lib/Doctrine/Relation/Association/Self.php index 0868a737d..92baf6169 100644 --- a/lib/Doctrine/Relation/Association/Self.php +++ b/lib/Doctrine/Relation/Association/Self.php @@ -51,10 +51,10 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association ' WHERE '.$this->foreign. ' = ?'; - $dql = 'FROM '.$this->table->getComponentName(); - $dql .= '.'.$this->associationTable->getComponentName(); - $dql .= ' WHERE '.$this->table->getComponentName().'.'.$this->table->getIdentifier().' IN ('.$sub.')'; - $dql .= ' || '.$this->table->getComponentName().'.'.$this->table->getIdentifier().' IN ('.$sub2.')'; + $dql = 'FROM ' . $this->table->getComponentName(); + $dql .= '.' . $this->associationTable->getComponentName(); + $dql .= ' WHERE ' . $this->table->getComponentName() . '.' . $this->table->getIdentifier() . ' IN (' . $sub . ')'; + $dql .= ' || ' . $this->table->getComponentName() . '.' . $this->table->getIdentifier() . ' IN (' . $sub2 . ')'; break; case 'collection': $sub = substr(str_repeat('?, ', $count),0,-2); diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 4f104292d..3d51ea4e3 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -109,10 +109,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * determining its state */ private $columnCount; - /** - * @var array $parents the parent classes of this component - */ - private $parents = array(); /** * @var boolean $hasDefaultValues whether or not this table has default values */ @@ -122,6 +118,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * * -- name name of the component, for example component name of the GroupTable is 'Group' * + * -- parents the parent classes of this component + * * -- declaringClass name of the table definition declaring class (when using inheritance the class * that defines the table structure can be any class in the inheritance hierarchy, * hence we need reflection to check out which class actually calls setTableDefinition) @@ -308,7 +306,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable // save parents array_pop($names); - $this->parents = $names; + $this->options['parents'] = $names; $this->query = 'SELECT ' . implode(', ', array_keys($this->columns)) . ' FROM ' . $this->getTableName(); @@ -392,6 +390,28 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable throw $e; } } + /** + * __get + * an alias for getOption + * + * @param string $option + */ + public function __get($option) + { + if (isset($this->options[$option])) { + return $this->options[$option]; + } + return null; + } + /** + * __isset + * + * @param string $option + */ + public function __isset($option) + { + return isset($this->options[$option]); + } /** * createQuery * creates a new Doctrine_Query object and adds the component name @@ -430,11 +450,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable } $this->options[$name] = $value; } - - public function usesInheritanceMap() - { - return ( ! empty($this->options['inheritanceMap'])); - } public function getOption($name) { if (isset($this->options[$name])) { @@ -592,72 +607,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { return in_array($key,$this->primaryKeys); } - /** - * @param $sequence - * @return void - */ - final public function setSequenceName($sequence) - { - $this->options['sequenceName'] = $sequence; - } - /** - * @return string sequence name - */ - final public function getSequenceName() - { - return $this->options['sequenceName']; - } - /** - * getParents - */ - final public function getParents() - { - return $this->parents; - } - /** - * @return boolean - */ - final public function hasInheritanceMap() - { - return (empty($this->options['inheritanceMap'])); - } - /** - * @return array inheritance map (array keys as fields) - */ - final public function getInheritanceMap() - { - return $this->options['inheritanceMap']; - } - /** - * return all composite paths in the form [component1].[component2]. . .[componentN] - * @return array - */ - final public function getCompositePaths() - { - $array = array(); - $name = $this->getComponentName(); - foreach ($this->bound as $k=>$a) { - try { - $fk = $this->getRelation($k); - switch ($fk->getType()) { - case Doctrine_Relation::ONE_COMPOSITE: - case Doctrine_Relation::MANY_COMPOSITE: - $n = $fk->getTable()->getComponentName(); - $array[] = $name.".".$n; - $e = $fk->getTable()->getCompositePaths(); - if ( ! empty($e)) { - foreach ($e as $name) { - $array[] = $name.".".$n.".".$name; - } - } - break; - }; - } catch(Doctrine_Table_Exception $e) { - - } - } - return $array; - } /** * returns all bound relations * @@ -788,14 +737,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable $this->bound[$alias] = array($field, $type, $localKey, $name); } - /** - * getComponentName - * @return string the component name - */ - public function getComponentName() - { - return $this->options['name']; - } /** * @return Doctrine_Connection */ @@ -960,26 +901,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable return $this->relations; } - /** - * sets the database table name - * - * @param string $name database table name - * @return void - */ - final public function setTableName($name) - { - $this->options['tableName'] = $name; - } - - /** - * returns the database table name - * - * @return string - */ - final public function getTableName() - { - return $this->options['tableName']; - } /** * create * creates a new record @@ -1182,17 +1103,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable } return $coll; } - /** - * sets enumerated value array for given field - * - * @param string $field - * @param array $values - * @return void - */ - final public function setEnumValues($field, array $values) - { - $this->options['enumMap'][strtolower($field)] = $values; - } /** * @param string $field * @return array @@ -1403,6 +1313,14 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable } return false; } + public function getComponentName() + { + return $this->options['name']; + } + public function getTableName() + { + return $this->options['tableName']; + } /** * determine if table acts as tree *