Changeset 4628 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
07/04/08 17:32:19 (6 months ago)
Author:
romanb
Message:

The usual 2.0 refactoring/implementation commit.

Location:
trunk/lib/Doctrine
Files:
22 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine/ClassMetadata.php

    r4523 r4628  
    142142 
    143143    /** 
     144     * Enter description here... 
     145     * 
     146     * @var array 
     147     */ 
     148    protected $_attributes = array('loadReferences' => true); 
     149     
     150    /** 
    144151     * An array of field names. used to look up field names from column names. 
    145152     * Keys are column names and values are field names. 
     
    149156     */ 
    150157    protected $_fieldNames = array(); 
    151      
    152     /** 
    153      * Enter description here... 
    154      * 
    155      * @var unknown_type 
    156      */ 
    157     protected $_attributes = array('loadReferences' => true); 
    158158 
    159159    /** 
     
    167167     
    168168    /** 
     169     * Map that maps lowercased column names to field names. 
     170     * Mainly used during hydration because Doctrine enforces PDO_CASE_LOWER 
     171     * for portability. 
     172     * 
     173     * @var array 
     174     */ 
     175    protected $_lcColumnToFieldNames = array(); 
     176     
     177    /** 
    169178     * Enter description here... 
    170179     * 
    171180     * @var unknown_type 
    172181     */ 
    173     protected $_subclassFieldNames = array(); 
     182    //protected $_subclassFieldNames = array(); 
    174183 
    175184    /** 
     
    193202     * @var integer 
    194203     */ 
    195     protected $_columnCount; 
     204    //protected $_columnCount; 
    196205 
    197206    /** 
     
    541550 
    542551    /** 
    543      * getFieldName 
    544      * 
    545      * returns the field name for a column name 
    546      * if no field name can be found the column name is returned. 
     552     * Gets the field name for a column name. 
     553     * If no field name can be found the column name is returned. 
    547554     * 
    548555     * @param string $columnName    column name 
     
    556563     
    557564    /** 
     565     * Gets the field name for a completely lowercased column name. 
     566     * Mainly used during hydration. 
     567     * 
     568     * @param string $lcColumnName 
     569     * @return string 
     570     */ 
     571    public function getFieldNameForLowerColumnName($lcColumnName) 
     572    { 
     573        return isset($this->_lcColumnToFieldNames[$lcColumnName]) ? 
     574                $this->_lcColumnToFieldNames[$lcColumnName] : $lcColumnName; 
     575    } 
     576     
     577    public function hasLowerColumn($lcColumnName) 
     578    { 
     579        return isset($this->_lcColumnToFieldNames[$lcColumnName]); 
     580    } 
     581     
     582    /** 
     583     * Looks up the field name for a (lowercased) column name. 
    558584     *  
    559      */ 
    560     public function lookupFieldName($columnName) 
    561     { 
    562         if (isset($this->_fieldNames[$columnName])) { 
    563             return $this->_fieldNames[$columnName]; 
    564         } else if (isset($this->_subclassFieldNames[$columnName])) { 
    565             return $this->_subclassFieldNames[$columnName]; 
    566         } 
     585     * This is mostly used during hydration, because we want to make the 
     586     * conversion to field names while iterating over the result set for best 
     587     * performance. By doing this at that point, we can avoid re-iterating over 
     588     * the data just to convert the column names to field names. 
     589     *  
     590     * However, when this is happening, we don't know the real 
     591     * class name to instantiate yet (the row data may target a sub-type), hence 
     592     * this method looks up the field name in the subclass mappings if it's not 
     593     * found on this class mapping. 
     594     * This lookup on subclasses is costly but happens only *once* for a column 
     595     * during hydration because the hydrator caches effectively. 
     596     */ 
     597    public function lookupFieldName($lcColumnName) 
     598    { 
     599        if (isset($this->_lcColumnToFieldNames[$lcColumnName])) { 
     600            return $this->_lcColumnToFieldNames[$lcColumnName]; 
     601        }/* else if (isset($this->_subclassFieldNames[$lcColumnName])) { 
     602            return $this->_subclassFieldNames[$lcColumnName]; 
     603        }*/ 
    567604         
    568         $classMetadata = $this; 
    569         $conn = $this->_em; 
    570          
    571         foreach ($classMetadata->getSubclasses() as $subClass) { 
    572             $subClassMetadata = $conn->getClassMetadata($subClass); 
    573             if ($subClassMetadata->hasColumn($columnName)) { 
    574                 $this->_subclassFieldNames[$columnName] = $subClassMetadata->getFieldName($columnName); 
    575                 return $this->_subclassFieldNames[$columnName]; 
     605        foreach ($this->getSubclasses() as $subClass) { 
     606            $subClassMetadata = $this->_em->getClassMetadata($subClass); 
     607            if ($subClassMetadata->hasLowerColumn($lcColumnName)) { 
     608                /*$this->_subclassFieldNames[$lcColumnName] = $subClassMetadata-> 
     609                        getFieldNameForLowerColumnName($lcColumnName); 
     610                return $this->_subclassFieldNames[$lcColumnName];*/ 
     611                return $subClassMetadata->getFieldNameForLowerColumnName($lcColumnName); 
    576612            } 
    577613        } 
    578614 
    579         throw new Doctrine_ClassMetadata_Exception("No field name found for column name '$columnName' during lookup."); 
     615        throw new Doctrine_ClassMetadata_Exception("No field name found for column name '$lcColumnName' during lookup."); 
    580616    } 
    581617 
     
    616652        } 
    617653 
    618         // extract column name & field name 
     654        // extract column name & field name & lowercased column name 
    619655        $parts = explode(' as ', $name); 
    620656        if (count($parts) > 1) { 
     
    623659            $fieldName = $parts[0]; 
    624660        } 
    625         $name = strtolower($parts[0]); 
    626  
    627         if (isset($this->_columnNames[$fieldName])) { 
     661        $columnName = $parts[0]; 
     662        $lcColumnName = strtolower($parts[0]); 
     663 
     664        if (isset($this->_mappedColumns[$columnName])) { 
    628665            return; 
    629666        } 
    630667 
     668        // Fill column name <-> field name lookup maps 
    631669        if ($prepend) { 
    632             $this->_columnNames = array_merge(array($fieldName => $name), $this->_columnNames); 
    633             $this->_fieldNames = array_merge(array($name => $fieldName), $this->_fieldNames); 
     670            $this->_columnNames = array_merge(array($fieldName => $columnName), $this->_columnNames); 
     671            $this->_fieldNames = array_merge(array($columnName => $fieldName), $this->_fieldNames); 
    634672        } else { 
    635             $this->_columnNames[$fieldName] = $name; 
    636             $this->_fieldNames[$name] = $fieldName; 
    637         } 
     673            $this->_columnNames[$fieldName] = $columnName; 
     674            $this->_fieldNames[$columnName] = $fieldName; 
     675        } 
     676        $this->_lcColumnToFieldNames[$lcColumnName] = $fieldName; 
     677         
    638678         
    639679        // Inspect & fill $options 
     
    663703 
    664704        if ($prepend) { 
    665             $this->_mappedColumns = array_merge(array($name => $options), $this->_mappedColumns); 
     705            $this->_mappedColumns = array_merge(array($columnName => $options), $this->_mappedColumns); 
    666706        } else { 
    667             $this->_mappedColumns[$name] = $options; 
     707            $this->_mappedColumns[$columnName] = $options; 
    668708        } 
    669709 
  • trunk/lib/Doctrine/Collection.php

    r4523 r4628  
    6666     * @var Doctrine_Entity 
    6767     */ 
    68     protected $reference; 
     68    protected $_owner; 
    6969 
    7070    /** 
     
    9494     * @var Doctrine_Null 
    9595     */ 
    96     protected static $null; 
     96    //protected static $null; 
     97     
     98    /** 
     99     * The EntityManager. 
     100     * 
     101     * @var EntityManager 
     102     */ 
     103    protected $_em; 
    97104 
    98105    /** 
     
    105112    public function __construct($entityBaseType, $keyField = null) 
    106113    { 
    107         if (is_string($entityBaseType)) { 
    108             $this->_entityBaseType = $entityBaseType; 
    109             $mapper = Doctrine_EntityManagerFactory::getManager($entityBaseType) 
    110                     ->getEntityPersister($entityBaseType); 
    111         } 
    112         $this->_mapper = $mapper; 
     114        $this->_entityBaseType = $entityBaseType; 
     115        $this->_em = Doctrine_EntityManagerFactory::getManager($entityBaseType); 
     116        $this->_mapper = $this->_em->getEntityPersister($entityBaseType); 
    113117 
    114118        if ($keyField === null) { 
    115             $keyField = $mapper->getClassMetadata()->getBoundQueryPart('indexBy'); 
     119            $keyField = $this->_mapper->getClassMetadata()->getBoundQueryPart('indexBy'); 
    116120        } 
    117121 
     
    126130            $this->_keyField = $keyField; 
    127131        } 
    128     } 
    129  
    130     /** 
    131      * initNullObject 
    132      * Initializes the null object for this collection. 
    133      * 
    134      * @return void 
    135      */ 
    136     public static function initNullObject(Doctrine_Null $null) 
    137     { 
    138         self::$null = $null; 
    139     } 
    140  
    141     /** 
    142      * getTable 
    143      * Returns the table of the mapper of the collection. 
    144      * 
    145      * @return Doctrine_Table 
    146      */ 
    147     public function getTable() 
    148     { 
    149         return $this->_mapper->getTable(); 
    150     } 
    151      
    152     /** 
    153      * getMapper 
    154      * Returns the mapper of this collection. 
    155      * 
    156      * @return Doctrine_Mapper 
    157      */ 
    158     public function getMapper() 
    159     { 
    160         return $this->_mapper; 
    161132    } 
    162133 
     
    307278     
    308279    /** 
    309      * setReference 
     280     * INTERNAL: 
    310281     * sets a reference pointer 
    311282     * 
    312283     * @return void 
    313284     */ 
    314     public function setReference(Doctrine_Entity $record, Doctrine_Relation $relation) 
    315     { 
    316         $this->reference = $record; 
     285    public function setReference(Doctrine_Entity $entity, Doctrine_Relation $relation) 
     286    { 
     287        $this->_owner = $entity; 
    317288        $this->relation  = $relation; 
    318289 
     
    320291                $relation instanceof Doctrine_Relation_LocalKey) { 
    321292            $this->referenceField = $relation->getForeignFieldName(); 
    322             $value = $record->get($relation->getLocalFieldName()); 
    323             foreach ($this->data as $record) { 
     293            $value = $entity->get($relation->getLocalFieldName()); 
     294            foreach ($this->data as $entity) { 
    324295                if ($value !== null) { 
    325                     $record->set($this->referenceField, $value, false); 
     296                    $entity->set($this->referenceField, $value, false); 
    326297                } else { 
    327                     $record->set($this->referenceField, $this->reference, false); 
     298                    $entity->set($this->referenceField, $this->_owner, false); 
    328299                } 
    329300            } 
     
    334305 
    335306    /** 
     307     * INTERNAL: 
    336308     * getReference 
    337309     * 
     
    340312    public function getReference() 
    341313    { 
    342         return $this->reference; 
    343     } 
    344  
    345     /** 
    346      * remove 
    347      * removes a specified collection element 
     314        return $this->_owner; 
     315    } 
     316 
     317    /** 
     318     * Removes an entity from the collection. 
    348319     * 
    349320     * @param mixed $key 
     
    358329 
    359330    /** 
    360      * contains 
    361      * whether or not this collection contains a specified element 
     331     * Checks whether the collection contains an entity. 
    362332     * 
    363333     * @param mixed $key                    the key of the element 
     
    379349 
    380350    /** 
    381      * get 
    382351     * returns a record for given key 
    383      * 
    384      * There are two special cases: 
    385      * 
    386      * 1. if null is given as a key a new record is created and attached 
    387      * at the end of the collection 
    388      * 
    389      * 2. if given key does not exist, then a new record is create and attached 
    390      * to the given key 
    391352     * 
    392353     * Collection also maps referential information to newly created records 
     
    397358    public function get($key) 
    398359    { 
    399         if ( ! isset($this->data[$key])) { 
    400             $record = $this->_mapper->create(); 
    401  
    402             if (isset($this->referenceField)) { 
    403                 $value = $this->reference->get($this->relation->getLocalFieldName()); 
    404  
    405                 if ($value !== null) { 
    406                     $record->set($this->referenceField, $value, false); 
    407                 } else { 
    408                     $record->set($this->referenceField, $this->reference, false); 
    409                 } 
    410             } 
    411              
    412             if ($key === null) { 
    413                 $this->data[] = $record; 
    414             } else { 
    415                 $this->data[$key] = $record;             
    416             } 
    417  
    418             if (isset($this->_keyField)) { 
    419                 $record->set($this->_keyField, $key); 
    420             } 
    421  
    422             return $record; 
    423         } 
    424  
    425         return $this->data[$key]; 
     360        if (isset($this->data[$key])) { 
     361            return $this->data[$key]; 
     362        } 
     363        return null; 
    426364    } 
    427365 
     
    492430     *           to adhere to the Doctrine_Access::set() signature. 
    493431     */ 
    494     public function set($key, $record) 
    495     { 
    496         if ( ! $record instanceOf Doctrine_Entity) { 
     432    public function set($key, $entity) 
     433    { 
     434        if ( ! $entity instanceof Doctrine_Entity) { 
    497435            throw new Doctrine_Collection_Exception('Value variable in set is not an instance of Doctrine_Entity'); 
    498436        } 
    499437 
    500438        if (isset($this->referenceField)) { 
    501             $record->set($this->referenceField, $this->reference, false); 
    502         } 
    503         $this->data[$key] = $record; 
     439            $entity->set($this->referenceField, $this->_owner, false); 
     440        } 
     441        $this->data[$key] = $entity; 
    504442    } 
    505443 
     
    518456 
    519457        if (isset($this->referenceField)) { 
    520             $value = $this->reference->get($this->relation->getLocalFieldName()); 
     458            $value = $this->_owner->get($this->relation->getLocalFieldName()); 
    521459 
    522460            if ($value !== null) { 
    523461                $record->set($this->referenceField, $value, false); 
    524462            } else { 
    525                 $record->set($this->referenceField, $this->reference, false); 
     463                $record->set($this->referenceField, $this->_owner, false); 
    526464            } 
    527465        } 
     
    560498 
    561499    /** 
     500     * INTERNAL: 
    562501     * loadRelated 
    563502     * 
     
    579518                } 
    580519            } 
    581             $query->from($this->_mapper->getComponentName() . '(' . implode(", ",$this->_mapper->getTable()->getPrimaryKeys()) . ')'); 
    582             $query->where($this->_mapper->getComponentName() . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')'); 
     520            $query->from($this->_mapper->getComponentName() 
     521                    . '(' . implode(", ",$this->_mapper->getTable()->getPrimaryKeys()) . ')'); 
     522            $query->where($this->_mapper->getComponentName() 
     523                    . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')'); 
    583524 
    584525            return $query; 
     
    608549 
    609550    /** 
     551     * INTERNAL: 
    610552     * populateRelated 
    611553     * 
     
    930872        $this->data = array(); 
    931873 
    932         if ($this->reference) { 
    933             $this->reference->free($deep); 
    934             $this->reference = null; 
     874        if ($this->_owner) { 
     875            $this->_owner->free($deep); 
     876            $this->_owner = null; 
    935877        } 
    936878    } 
  • trunk/lib/Doctrine/Configuration.php

    r4523 r4628  
    2828 * It combines all configuration options from DBAL & ORM. 
    2929 * 
     30 * @author Roman Borschel <roman@code-factory.org> 
    3031 * @since 2.0 
    3132 */ 
  • trunk/lib/Doctrine/Connection.php

    r4523 r4628  
    7979     * The PDO database handle.  
    8080     * 
    81      * @var PDO 
    82      * @todo Rename to $pdo.               
    83      */ 
    84     protected $dbh; 
     81     * @var PDO            
     82     */ 
     83    protected $_pdo; 
    8584     
    8685    /** 
     
    117116     * @var string $driverName                   
    118117     */ 
    119     protected $driverName; 
     118    protected $_driverName; 
    120119     
    121120    /** 
     
    124123     * @var boolean $isConnected                 
    125124     */ 
    126     protected $isConnected = false; 
     125    protected $_isConnected = false; 
    127126     
    128127    /** 
     
    164163     * @var array $availableDrivers          
    165164     */ 
    166     private static $availableDrivers = array( 
     165    private static $_availableDrivers = array( 
    167166            'Mysql', 'Pgsql', 'Oracle', 'Informix', 'Mssql', 'Sqlite', 'Firebird' 
    168167            ); 
     
    173172     * @var integer 
    174173     */ 
    175     protected $_count = 0; 
     174    protected $_queryCount = 0; 
    176175 
    177176     
     
    221220     * Constructor. 
    222221     * 
    223      * @param Doctrine_Manager $manager                 the manager object 
    224      * @param PDO|Doctrine_Adapter_Interface $adapter   database driver 
     222     * @param array $params  The connection parameters. 
    225223     */ 
    226224    public function __construct(array $params) 
    227225    { 
    228226        if (isset($params['pdo'])) { 
    229             $this->dbh = $params['pdo']; 
    230             $this->isConnected = true; 
     227            $this->_pdo = $params['pdo']; 
     228            $this->_isConnected = true; 
    231229        } 
    232230        $this->_params = $params; 
     
    320318 
    321319    /** 
    322      * getDriverName 
    323      * 
    324320     * Gets the name of the instance driver 
    325321     * 
     
    328324    public function getDriverName() 
    329325    { 
    330         return $this->driverName; 
     326        return $this->_driverName; 
    331327    } 
    332328     
     
    335331     * 
    336332     * @return PDO              the database handler 
     333     * @deprecated 
    337334     */ 
    338335    public function getDbh() 
    339336    { 
    340         //$this->connect(); 
    341          
    342         return $this->dbh; 
     337        $this->connect(); 
     338        return $this->_pdo; 
     339    } 
     340     
     341    /** 
     342     * Gets the PDO handle used by the connection. 
     343     * 
     344     * @return PDO 
     345     */ 
     346    public function getPdo() 
     347    { 
     348        $this->connect(); 
     349        return $this->_pdo; 
    343350    } 
    344351     
     
    350357    public function connect() 
    351358    { 
    352         if ($this->isConnected) { 
     359        if ($this->_isConnected) { 
    353360            return false; 
    354361        } 
     
    357364        //$this->getListener()->preConnect($event); 
    358365 
    359         $e = explode(':', $this->options['dsn']); 
     366        // TODO: the extension_loaded check can happen earlier, maybe in the factory 
    360367        if (extension_loaded('pdo')) { 
    361             if (in_array($e[0], PDO::getAvailableDrivers())) { 
    362                 $this->dbh = new PDO( 
    363                         $this->options['dsn'], $this->options['username'], 
    364                         $this->options['password'], $this->options['other']); 
    365                 $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    366                 $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); 
    367             } 
     368            $driverOptions = isset($this->_params['driverOptions']) ? 
     369                    $this->_params['driverOptions'] : array(); 
     370            $user = isset($this->_params['user']) ? 
     371                    $this->_params['user'] : null; 
     372            $password = isset($this->_params['password']) ? 
     373                    $this->_params['password'] : null; 
     374            $this->_pdo = new PDO( 
     375                    $this->_constructPdoDsn(), 
     376                    $user, 
     377                    $password, 
     378                    $driverOptions 
     379                    ); 
     380            $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     381            $this->_pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); 
    368382