Changeset 4653 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
07/10/08 18:17:58 (6 months ago)
Author:
romanb
Message:

Refactorings. Merged hydrator fixes from 0.11/1.0

Location:
trunk/lib/Doctrine
Files:
13 modified

Legend:

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

    r4628 r4653  
    2525 * A <tt>ClassMetadata</tt> instance holds all the information (metadata) of an entity and 
    2626 * it's associations and how they're mapped to the relational database. 
     27 * It is the backbone of Doctrine's metadata mapping. 
    2728 * 
    28  * @package Doctrine 
    29  * @subpackage ClassMetadata 
    3029 * @author Roman Borschel <roman@code-factory.org> 
    3130 * @since 2.0 
     
    116115 
    117116    /** 
    118      * The mapped columns and their mapping definitions. 
    119      * Keys are column names and values are mapping definitions. 
     117     * The field mappings of the class. 
     118     * Keys are field names and values are mapping definitions. 
    120119     * 
    121120     * The mapping definition array has at least the following values: 
     
    129128     *  ... many more 
    130129     * 
    131      * @var array $columns 
    132      */ 
    133     protected $_mappedColumns = array(); 
     130     * @var array 
     131     */     
     132    protected $_fieldMappings = array(); 
    134133     
    135134    /** 
     
    283282     */ 
    284283    protected $_invokedMethods = array(); 
    285  
     284     
     285    /** 
     286     * The cached lifecycle listeners. There is only one instance of each 
     287     * listener class at any time. 
     288     * 
     289     * @var array 
     290     */ 
     291    protected $_lifecycleListenerInstances = array(); 
     292 
     293    /** 
     294     * The registered lifecycle callbacks for Entities of this class. 
     295     * 
     296     * @var array 
     297     */ 
     298    protected $_lifecycleCallbacks = array(); 
     299     
     300    /** 
     301     * The registered lifecycle listeners for Entities of this class. 
     302     * 
     303     * @var array 
     304     */ 
     305    protected $_lifecycleListeners = array(); 
     306     
    286307 
    287308    /** 
     
    375396    public function isUniqueField($fieldName) 
    376397    { 
    377         $mapping = $this->getColumnMapping($fieldName); 
     398        $mapping = $this->getFieldMapping($fieldName); 
    378399 
    379400        if ($mapping !== false) { 
     
    392413    public function isNotNull($fieldName) 
    393414    { 
    394         $mapping = $this->getColumnMapping($fieldName); 
     415        $mapping = $this->getFieldMapping($fieldName); 
    395416 
    396417        if ($mapping !== false) { 
     
    532553    { 
    533554        return isset($this->_columnNames[$fieldName]) ? 
    534         $this->_columnNames[$fieldName] : $fieldName; 
     555                $this->_columnNames[$fieldName] : $fieldName; 
    535556    } 
    536557 
     
    543564    } 
    544565 
    545     public function getColumnMapping($columnName) 
    546     { 
    547         return isset($this->_mappedColumns[$columnName]) ? 
    548                 $this->_mappedColumns[$columnName] : false; 
     566    public function getFieldMapping($fieldName) 
     567    { 
     568        return isset($this->_fieldMappings[$fieldName]) ? 
     569                $this->_fieldMappings[$fieldName] : false; 
    549570    } 
    550571 
     
    594615     * This lookup on subclasses is costly but happens only *once* for a column 
    595616     * during hydration because the hydrator caches effectively. 
     617     *  
     618     * @return string  The field name. 
     619     * @throws Doctrine::ORM::Exceptions::ClassMetadataException if the field name could 
     620     *         not be found. 
    596621     */ 
    597622    public function lookupFieldName($lcColumnName) 
     
    627652 
    628653    /** 
    629      * Maps a column of the class' database table to a field of the entity. 
     654     * Maps a field of the class to a database column. 
    630655     * 
    631656     * @param string $name      The name of the column to map. Syntax: columnName [as propertyName]. 
     
    640665     * @throws Doctrine_ClassMetadata_Exception If trying use wrongly typed parameter. 
    641666     */ 
    642     public function mapColumn($name, $type, $length = null, $options = array(), $prepend = false) 
     667    public function mapColumn($name, $type, $length = null, $options = array()) 
    643668    { 
    644669        // converts 0 => 'primary' to 'primary' => true etc. 
     
    662687        $lcColumnName = strtolower($parts[0]); 
    663688 
    664         if (isset($this->_mappedColumns[$columnName])) { 
     689        if (isset($this->_fieldMappings[$fieldName])) { 
    665690            return; 
    666691        } 
    667692 
    668693        // Fill column name <-> field name lookup maps 
    669         if ($prepend) { 
    670             $this->_columnNames = array_merge(array($fieldName => $columnName), $this->_columnNames); 
    671             $this->_fieldNames = array_merge(array($columnName => $fieldName), $this->_fieldNames); 
    672         } else { 
    673             $this->_columnNames[$fieldName] = $columnName; 
    674             $this->_fieldNames[$columnName] = $fieldName; 
    675         } 
     694        $this->_columnNames[$fieldName] = $columnName; 
     695        $this->_fieldNames[$columnName] = $fieldName; 
     696        $this->_lcColumnToFieldNames[$lcColumnName] = $fieldName; 
    676697        $this->_lcColumnToFieldNames[$lcColumnName] = $fieldName; 
    677698         
     
    702723        }*/ 
    703724 
    704         if ($prepend) { 
    705             $this->_mappedColumns = array_merge(array($columnName => $options), $this->_mappedColumns); 
    706         } else { 
    707             $this->_mappedColumns[$columnName] = $options; 
    708         } 
     725        $this->_fieldMappings[$fieldName] = $options; 
    709726 
    710727        $this->_columnCount++; 
     
    755772 
    756773    /** 
    757      * setColumn 
    758      * 
    759      * @param string $name 
    760      * @param string $type 
    761      * @param integer $length 
    762      * @param mixed $options 
    763      * @param boolean $prepend   Whether to prepend or append the new column to the column list. 
    764      *                           By default the column gets appended. 
    765      * @throws Doctrine_Table_Exception     if trying use wrongly typed parameter 
    766      * @return void 
    767      * @deprecated Use mapColumn() 
    768      */ 
    769     public function setColumn($name, $type, $length = null, $options = array(), $prepend = false) 
    770     { 
    771         return $this->mapColumn($name, $type, $length, $options, $prepend); 
    772     } 
    773  
    774     /** 
    775774     * Gets the names of all validators that are applied on a field. 
    776775     * 
     
    780779    public function getFieldValidators($fieldName) 
    781780    { 
    782         $columnName = $this->getColumnName($fieldName); 
    783         return isset($this->_mappedColumns[$columnName]['validators']) ? 
    784         $this->_mappedColumns[$columnName]['validators'] : array(); 
     781        return isset($this->_fieldMappings[$fieldName]['validators']) ? 
     782                $this->_fieldMappings[$fieldName]['validators'] : array(); 
    785783    } 
    786784 
     
    804802    public function getDefaultValueOf($fieldName) 
    805803    { 
    806         $columnName = $this->getColumnName($fieldName); 
    807         if ( ! isset($this->_mappedColumns[$columnName])) { 
    808             throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$columnName." doesn't exist."); 
    809         } 
    810         if (isset($this->_mappedColumns[$columnName]['default'])) { 
    811             return $this->_mappedColumns[$columnName]['default']; 
     804        if ( ! isset($this->_fieldMappings[$fieldName])) { 
     805            throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$fieldName." doesn't exist."); 
     806        } 
     807        if (isset($this->_fieldMappings[$fieldName]['default'])) { 
     808            return $this->_fieldMappings[$fieldName]['default']; 
    812809        } else { 
    813810            return null; 
     
    868865    public function hasColumn($columnName) 
    869866    { 
    870         return isset($this->_mappedColumns[$columnName]); 
     867        return isset($this->_fieldNames[$columnName]); 
    871868    } 
    872869 
    873870    public function hasMappedColumn($columnName) 
    874871    { 
    875         return isset($this->_mappedColumns[$columnName]); 
     872        return isset($this->_fieldNames[$columnName]); 
    876873    } 
    877874 
     
    891888    public function getEnumValues($fieldName) 
    892889    { 
    893         $columnName = $this->getColumnName($fieldName); 
    894         if (isset($this->_mappedColumns[$columnName]['values'])) { 
    895             return $this->_mappedColumns[$columnName]['values']; 
     890        if (isset($this->_fieldMappings[$fieldName]['values'])) { 
     891            return $this->_fieldMappings[$fieldName]['values']; 
    896892        } else { 
    897893            return array(); 
     
    918914        $columnName = $this->getColumnName($fieldName); 
    919915        if ( ! $this->_em->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM) && 
    920         isset($this->_mappedColumns[$columnName]['values'][$index])) { 
    921             $enumValue = $this->_mappedColumns[$columnName]['values'][$index]; 
     916                isset($this->_fieldMappings[$fieldName]['values'][$index])) { 
     917            $enumValue = $this->_fieldMappings[$fieldName]['values'][$index]; 
    922918        } else { 
    923919            $enumValue = $index; 
     
    974970    public function getCustomAccessor($fieldName) 
    975971    { 
    976         $columnName = $this->getColumnName($fieldName); 
    977         return isset($this->_mappedColumns[$columnName]['accessor']) ? 
    978                 $this->_mappedColumns[$columnName]['accessor'] : null; 
     972        return isset($this->_fieldMappings[$fieldName]['accessor']) ? 
     973                $this->_fieldMappings[$fieldName]['accessor'] : null; 
    979974    } 
    980975 
     
    986981    public function getCustomMutator($fieldName) 
    987982    { 
    988         $columnName = $this->getColumnName($fieldName); 
    989         return isset($this->_mappedColumns[$columnName]['mutator']) ? 
    990                 $this->_mappedColumns[$columnName]['mutator'] : null; 
     983        return isset($this->_fieldMappings[$fieldName]['mutator']) ? 
     984                $this->_fieldMappings[$fieldName]['mutator'] : null; 
    991985    } 
    992986 
     
    999993    public function getColumns() 
    1000994    { 
    1001         return $this->_mappedColumns; 
     995        return $this->_fieldMappings; 
    1002996    } 
    1003997 
     
    10091003    public function getMappedColumns() 
    10101004    { 
    1011         return $this->_mappedColumns; 
     1005        return $this->_fieldMappings; 
     1006    } 
     1007     
     1008    public function getFieldMappings() 
     1009    { 
     1010        return $this->_fieldMappings; 
    10121011    } 
    10131012 
     
    10241023        unset($this->_fieldNames[$columnName]); 
    10251024 
    1026         if (isset($this->_mappedColumns[$columnName])) { 
    1027             unset($this->_mappedColumns[$columnName]); 
     1025        if (isset($this->_fieldMappings[$fieldName])) { 
     1026            unset($this->_fieldMappings[$fieldName]); 
    10281027            return true; 
    10291028        } 
     
    10411040    { 
    10421041        if ($fieldNames === null) { 
    1043             return array_keys($this->_mappedColumns); 
     1042            return array_keys($this->_fieldNames); 
    10441043        } else { 
    10451044            $columnNames = array(); 
     
    10931092    public function getMappingForField($fieldName) 
    10941093    { 
    1095         $columnName = $this->getColumnName($fieldName); 
    1096         return $this->getColumnDefinition($columnName); 
     1094        return $this->_fieldMappings[$fieldName]; 
    10971095    } 
    10981096 
     
    11051103    public function getTypeOf($fieldName) 
    11061104    { 
     1105         
    11071106        return $this->getTypeOfColumn($this->getColumnName($fieldName)); 
    11081107    } 
     
    11161115    public function getTypeOfField($fieldName) 
    11171116    { 
    1118         return $this->getTypeOfColumn($this->getColumnName($fieldName)); 
     1117        return isset($this->_fieldMappings[$fieldName]) ? 
     1118                $this->_fieldMappings[$fieldName]['type'] : false; 
    11191119    } 
    11201120 
     
    11261126    public function getTypeOfColumn($columnName) 
    11271127    { 
    1128         return isset($this->_mappedColumns[$columnName]) ? $this->_mappedColumns[$columnName]['type'] : false; 
     1128        return $this->getTypeOfField($this->getFieldName($columnName)); 
    11291129    } 
    11301130 
     
    11341134    public function getFieldLength($fieldName) 
    11351135    { 
    1136         return $this->_mappedColumns[$this->getColumnName($fieldName)]['length']; 
     1136        return $this->_fieldMappings[$fieldName]['length']; 
    11371137    } 
    11381138 
     
    15031503        $options['foreignKeys'] = array(); 
    15041504        if ($parseForeignKeys && $this->getAttribute(Doctrine::ATTR_EXPORT) 
    1505         & Doctrine::EXPORT_CONSTRAINTS) { 
     1505                & Doctrine::EXPORT_CONSTRAINTS) { 
    15061506            $constraints = array(); 
    15071507            $emptyIntegrity = array('onUpdate' => null, 'onDelete' => null); 
     
    16841684    public function isInheritedField($fieldName) 
    16851685    { 
    1686         return isset($this->_mappedColumns[$this->getColumnName($fieldName)]['inherited']); 
     1686        return isset($this->_fieldMappings[$fieldName]['inherited']); 
    16871687    } 
    16881688 
     
    19131913 
    19141914    } 
     1915     
     1916    /** 
     1917     * Dispatches the lifecycle event of the given Entity to the registered 
     1918     * lifecycle callbacks and lifecycle listeners. 
     1919     * 
     1920     * @param string $event  The lifecycle event. 
     1921     * @param Entity $entity  The Entity on which the event occured. 
     1922     */ 
     1923    public function invokeLifecycleCallbacks($lifecycleEvent, Doctrine_Entity $entity) 
     1924    { 
     1925        foreach ($this->getLifecycleCallbacks($lifecycleEvent) as $callback) { 
     1926            $entity->$callback(); 
     1927        } 
     1928        foreach ($this->getLifecycleListeners($lifecycleEvent) as $className => $callback) { 
     1929            if ( ! isset($this->_lifecycleListenerInstances[$className])) { 
     1930                $this->_lifecycleListenerInstances[$className] = new $className; 
     1931            } 
     1932            $this->_lifecycleListenerInstances[$className]->$callback($entity); 
     1933        } 
     1934    } 
     1935     
     1936    /** 
     1937     * Gets the registered lifecycle callbacks for an event. 
     1938     * 
     1939     * @param string $event 
     1940     * @return array 
     1941     */ 
     1942    public function getLifecycleCallbacks($event) 
     1943    { 
     1944        return isset($this->_lifecycleCallbacks[$event]) ? 
     1945                $this->_lifecycleCallbacks[$event] : array(); 
     1946    } 
     1947     
     1948    /** 
     1949     * Gets the registered lifecycle listeners for an event. 
     1950     * 
     1951     * @param string $event 
     1952     * @return array 
     1953     */ 
     1954    public function getLifecycleListeners($event) 
     1955    { 
     1956        return isset($this->_lifecycleListeners[$event]) ? 
     1957                $this->_lifecycleListeners[$event] : array(); 
     1958    } 
     1959     
     1960    /** 
     1961     * Adds a lifecycle listener for Entities this class. 
     1962     *  
     1963     * Note: If the same listener class is registered more than once, the old 
     1964     * one will be overridden. 
     1965     * 
     1966     * @param string $listenerClass 
     1967     * @param array $callbacks 
     1968     */ 
     1969    public function addLifecycleListener($listenerClass, array $callbacks) 
     1970    { 
     1971        $this->_lifecycleListeners[$event][$listenerClass] = array(); 
     1972        foreach ($callbacks as $method => $event) { 
     1973            $this->_lifecycleListeners[$event][$listenerClass][] = $method; 
     1974        } 
     1975    } 
     1976     
     1977    /** 
     1978     * Adds a lifecycle callback for Entities of this class. 
     1979     * 
     1980     * Note: If a the same callback is registered more than once, the old one 
     1981     * will be overridden. 
     1982     *  
     1983     * @param string $callback 
     1984     * @param string $event 
     1985     */ 
     1986    public function addLifecycleCallback($callback, $event) 
     1987    { 
     1988        if ( ! isset($this->_lifecycleCallbacks[$event])) { 
     1989            $this->_lifecycleCallbacks[$event] = array(); 
     1990        } 
     1991        if ( ! in_array($callback, $this->_lifecycleCallbacks[$event])) { 
     1992            $this->_lifecycleCallbacks[$event][$callback] = $callback; 
     1993        }  
     1994    } 
    19151995 
    19161996    /** 
  • trunk/lib/Doctrine/ClassMetadata/CodeDriver.php

    r4435 r4653  
    3636{ 
    3737    /** 
     38     * Name of the callback method. 
     39     *  
     40     * @todo We could make the name of the callback methods customizable for users. 
     41     */ 
     42    const CALLBACK_METHOD = 'initMetadata'; 
     43     
     44    /** 
    3845     * Loads the metadata for the specified class into the provided container. 
    3946     */ 
    4047    public function loadMetadataForClass($className, Doctrine_ClassMetadata $metadata) 
    4148    { 
    42         if ( ! method_exists($className, 'initMetadata')) { 
     49        if ( ! method_exists($className, self::CALLBACK_METHOD)) { 
    4350            throw new Doctrine_ClassMetadata_Exception("Unable to load metadata for class" 
    4451                    . " '$className'. Callback method 'initMetadata' not found."); 
  • trunk/lib/Doctrine/ClassMetadata/Factory.php

    r4434 r4653  
    134134            $fullName = "$name as " . $parentClass->getFieldName($name); 
    135135            $definition['inherited'] = true; 
    136             $subClass->mapColumn($fullName, $definition['type'], $definition['length'], 
     136            $subClass->mapColumn( 
     137                    $fullName, 
     138                    $definition['type'], 
     139                    $definition['length'], 
    137140                    $definition); 
    138141        } 
     
    155158    { 
    156159        if ( ! class_exists($name) || empty($name)) { 
    157             /*try { 
    158                 throw new Exception(); 
    159             } catch (Exception $e) { 
    160                 echo $e->getTraceAsString(); 
    161             }*/ 
    162160            throw new Doctrine_Exception("Couldn't find class " . $name . "."); 
    163161        } 
     
    176174 
    177175        if ($className === false) { 
    178             try { 
    179                 throw new Exception(); 
    180             } catch (Exception $e) { 
    181                 echo $e->getTraceAsString() . "<br />"; 
    182             } 
    183176            throw new Doctrine_ClassMetadata_Factory_Exception("Unknown component '$className'."); 
    184177        } 
     
    208201    { 
    209202        switch (count((array)$class->getIdentifier())) { 
    210             case 0: 
     203            case 0: // No identifier in the class mapping yet 
     204                 
     205                // If its a subclass, inherit the identifier from the parent. 
    211206                if ($class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED && 
    212                         count($class->getParentClasses()) > 0) { 
    213                              
     207                        count($class->getParentClasses()) > 0) {        
    214208                    $parents = $class->getParentClasses(); 
    215209                    $root = end($parents); 
     
    238232                    } 
    239233                } else { 
     234                    throw Doctrine_MappingException::identifierRequired($class->getClassName());       
     235                    /* Legacy behavior of auto-adding an id field 
    240236                    $definition = array('type' => 'integer', 
    241237                                        'length' => 20, 
    242238                                        'autoincrement' => true, 
    243239                                        'primary' => true); 
    244                     $class->setColumn('id', $definition['type'], $definition['length'], $definition, true); 
     240                    $class->mapColumn('id', $definition['type'], $definition['length'], $definition, true); 
    245241                    $class->setIdentifier(array('id')); 
    246242                    $class->setIdentifierType(Doctrine::IDENTIFIER_AUTOINC); 
     243                    */ 
    247244                } 
    248245                break; 
    249             case 1: 
    250                 foreach ((array)$class->getIdentifier() as $pk) { 
     246            case 1: // A single identifier is in the mapping 
     247                foreach ($class->getIdentifier() as $pk) { 
    251248                    $columnName = $class->getColumnName($pk); 
    252249                    $thisColumns = $class->getColumns(); 
     
    295292 
    296293                break; 
    297             default: 
     294            default: // Multiple identifiers are in the mapping so its a composite id 
    298295                $class->setIdentifierType(Doctrine::IDENTIFIER_COMPOSITE); 
    299296        } 
  • trunk/lib/Doctrine/Connection/UnitOfWork.php

    r4628 r4653  
    8484     
    8585    /** 
    86      * The EntityManager the unit of work belongs to. 
     86     * The EntityManager the UnitOfWork belongs to. 
    8787     */ 
    8888    protected $_em; 
     
    9999    /** 
    100100     * Constructor. 
    101      * Created a new UnitOfWork. 
     101     * Creates a new UnitOfWork. 
    102102     * 
    103103     * @param Doctrine_EntityManager $em 
     
    111111     * Commits the unit of work, executing all operations that have been postponed 
    112112     * up to this point. 
    113      * 
     113     *  
     114     * @todo Impl 
    114115     */ 
    115116    public function commit() 
     
    192193    public function registerRemoved(Doctrine_Entity $entity) 
    193194    { 
    194         if ($entity->isTransient()) { 
     195        if ($entity->isNew()) { 
    195196            return; 
    196197        } 
     
    512513    } 
    513514     
     515    public function save(Doctrine_Entity $entity) 
     516    {         
     517        switch ($entity->_state()) { 
     518            case Doctrine_Entity::STATE_CLEAN: 
     519                //nothing to do 
     520                // ignore $entity but cascade 
     521                break; 
     522            case Doctrine_Entity::STATE_DIRTY: 
     523                // update 
     524                $this->registerDirty($entity); 
     525                // todo:cascade 
     526                break; 
     527            case Doctrine_Entity::STATE_TCLEAN: 
     528            case Doctrine_Entity::STATE_TDIRTY: 
     529                // insert 
     530                // if identifier type IDENTITY: 
     531                //     cascade 
     532                //     if no transaction is started yet, do it 
     533                //     force insert (directly to persister) 
     534                // else 
     535                //     cascade 
     536                //     get & assign the identifier, then registerNew() 
     537                break; 
     538        } 
     539    } 
     540  &