Changeset 4877 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
09/07/08 14:48:40 (4 months ago)
Author:
romanb
Message:

cleanup

Location:
trunk/lib/Doctrine
Files:
2 added
41 removed
21 modified

Legend:

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

    r4866 r4877  
    170170     
    171171    /** 
     172     * The Id generator. 
     173     * 
     174     * @var Doctrine::ORM::Id::IdGenerator 
     175     */ 
     176    protected $_idGenerator; 
     177     
     178    /** 
    172179     * The field mappings of the class. 
    173180     * Keys are field names and values are mapping definitions. 
     
    599606     */ 
    600607    public function getAssociationMappings() 
     608    { 
     609        return $this->_associationMappings; 
     610    } 
     611     
     612    /** 
     613     * Gets all association mappings of the class. 
     614     * Alias for getAssociationMappings(). 
     615     * 
     616     * @return array 
     617     */ 
     618    public function getAssociations() 
    601619    { 
    602620        return $this->_associationMappings; 
     
    687705        } 
    688706        $this->_fieldMappings[$mapping['fieldName']] = $mapping; 
    689     } 
    690      
    691     /** 
    692      * Overrides an existant field mapping. 
    693      * Used i.e. by Entity classes deriving from another Entity class that acts 
    694      * as a mapped superclass to refine the basic mapping. 
    695      * 
    696      * @param array $newMapping 
    697      * @todo Implementation. 
    698      */ 
    699     public function overrideFieldMapping(array $newMapping) 
    700     { 
    701         //... 
    702707    } 
    703708     
     
    756761         
    757762        return $mapping; 
     763    } 
     764     
     765    /** 
     766     * @todo Implementation of Optimistic Locking. 
     767     */ 
     768    public function mapVersionField(array $mapping) 
     769    { 
     770        //... 
     771    } 
     772     
     773    /** 
     774     * Overrides an existant field mapping. 
     775     * Used i.e. by Entity classes deriving from another Entity class that acts 
     776     * as a mapped superclass to refine the basic mapping. 
     777     * 
     778     * @param array $newMapping 
     779     * @todo Implementation. 
     780     */ 
     781    public function overrideFieldMapping(array $newMapping) 
     782    { 
     783        //... 
     784    } 
     785     
     786    /** 
     787     * Used to lazily create the id generator. 
     788     * 
     789     * @param string $generatorType 
     790     * @return void 
     791     */ 
     792    protected function _createIdGenerator() 
     793    { 
     794        if ($this->_generatorType == self::GENERATOR_TYPE_IDENTITY) { 
     795            $this->_idGenerator = new Doctrine_Id_IdentityGenerator($this->_em); 
     796        } else if ($this->_generatorType == self::GENERATOR_TYPE_SEQUENCE) { 
     797            $this->_idGenerator = new Doctrine_Id_SequenceGenerator($this->_em); 
     798        } else if ($this->_generatorType == self::GENERATOR_TYPE_TABLE) { 
     799            $this->_idGenerator = new Doctrine_Id_TableGenerator($this->_em); 
     800        } else { 
     801            $this->_idGenerator = new Doctrine_Id_Assigned($this->_em); 
     802        } 
    758803    } 
    759804     
     
    16211666         return $this->_customRepositoryClassName; 
    16221667    } 
     1668     
     1669    /** 
     1670     * Gets the Id generator used by the class. 
     1671     * 
     1672     * @return Doctrine::ORM::Id::AbstractIdGenerator 
     1673     */ 
     1674    public function getIdGenerator() 
     1675    { 
     1676        if (is_null($this->_idGenerator)) { 
     1677            $this->_createIdGenerator(); 
     1678        } 
     1679        return $this->_idGenerator; 
     1680    } 
    16231681 
    16241682    /** 
     
    17251783     
    17261784    /** 
    1727      * Completes the identifier mapping of the class. 
     1785     * INTERNAL: Completes the identifier mapping of the class. 
    17281786     * NOTE: Should only be called by the ClassMetadataFactory! 
    17291787     *  
     
    17321790    public function completeIdentifierMapping() 
    17331791    { 
    1734         if ($this->getIdGeneratorType() == self::GENERATOR_TYPE_AUTO) { 
     1792        if ($this->_generatorType == self::GENERATOR_TYPE_AUTO) { 
    17351793            $platform = $this->_em->getConnection()->getDatabasePlatform(); 
     1794            if ($platform === null) { 
     1795                try { 
     1796                    throw new Exception(); 
     1797                } catch (Exception $e) { 
     1798                    echo $e->getTraceAsString(); 
     1799                } 
     1800            } 
    17361801            if ($platform->prefersSequences()) { 
    17371802                $this->_generatorType = self::GENERATOR_TYPE_SEQUENCE; 
  • trunk/lib/Doctrine/Collection.php

    r4866 r4877  
    131131    { 
    132132        $this->_entityBaseType = $entityBaseType; 
    133         $this->_em = Doctrine_EntityManagerFactory::getManager($entityBaseType); 
     133        $this->_em = Doctrine_EntityManager::getActiveEntityManager(); 
    134134 
    135135        if ($keyField !== null) { 
     
    10191019    public function unserialize($serialized) 
    10201020    { 
    1021         $manager = Doctrine_EntityManagerFactory::getManager(); 
     1021        $manager = Doctrine_EntityManager::getActiveEntityManager(); 
    10221022        $connection = $manager->getConnection(); 
    10231023         
  • trunk/lib/Doctrine/Connection.php

    r4866 r4877  
    162162     
    163163    /** 
    164      * The sequence manager. 
    165      * 
    166      * @var Doctrine::DBAL::Sequencing::SequenceManager 
    167      */ 
    168     protected $_sequenceManager; 
    169      
    170     /** 
    171164     * The schema manager. 
    172165     * 
     
    947940     * @param string $field     Name of the field into which a new row was inserted. 
    948941     */ 
    949     public function lastInsertId($table = null, $field = null) 
    950     { 
    951         return $this->getSequenceManager()->lastInsertId($table, $field); 
     942    public function lastInsertId($seqName = null) 
     943    { 
     944        return $this->_pdo->lastInsertId($seqName); 
    952945    } 
    953946 
     
    11791172     
    11801173    /** 
    1181      * Gets the SequenceManager that can be used to retrieve sequence values 
    1182      * through this connection. 
    1183      * 
    1184      * @return Doctrine::DBAL::Sequencing::SequenceManager 
    1185      */ 
    1186     public function getSequenceManager() 
    1187     { 
    1188         if ( ! $this->_sequenceManager) { 
    1189             $class = "Doctrine_Sequence_" . $this->_driverName; 
    1190             $this->_sequenceManager = new $class; 
    1191         } 
    1192         return $this->_sequenceManager; 
    1193     } 
    1194      
    1195     /** 
    11961174     * Gets the SchemaManager that can be used to inspect or change the  
    11971175     * database schema through the connection. 
  • trunk/lib/Doctrine/Connection/UnitOfWork.php

    r4789 r4877  
    183183    } 
    184184 
     185    /** 
     186     * Executes all entity insertions for entities of the specified type. 
     187     * 
     188     * @param Doctrine::ORM::Mapping::ClassMetadata $class 
     189     */ 
    185190    private function _executeInserts($class) 
    186191    { 
     
    194199            if ($entity->getClass()->getClassName() == $className) { 
    195200                $persister->insert($entity); 
     201                if ($class->isIdGeneratorIdentity()) { 
     202                    $entity->_assignIdentifier($class->getIdGenerator()->getPostInsertId()); 
     203                } 
    196204            } 
    197205        } 
    198206    } 
    199207 
     208    /** 
     209     * Executes all entity updates for entities of the specified type. 
     210     * 
     211     * @param Doctrine::ORM::Mapping::ClassMetadata $class 
     212     */ 
    200213    private function _executeUpdates($class) 
    201214    { 
     
    209222    } 
    210223 
     224    /** 
     225     * Executes all entity deletions for entities of the specified type. 
     226     * 
     227     * @param Doctrine::ORM::Mapping::ClassMetadata $class 
     228     */ 
    211229    private function _executeDeletions($class) 
    212230    { 
     
    225243     * @return array 
    226244     */ 
    227     private function _getCommitOrder() 
     245    private function _getCommitOrder(array $entityChangeSet = null) 
    228246    { 
    229247        //TODO: Once these 3 arrays are indexed by classname we can do this: 
     
    234252            array_keys($this->_deletedEntities) 
    235253        );*/ 
    236  
    237         $entityChangeSet = array_merge($this->_newEntities, $this->_dirtyEntities, $this->_deletedEntities); 
     254         
     255        if (is_null($entityChangeSet)) { 
     256            $entityChangeSet = array_merge($this->_newEntities, $this->_dirtyEntities, $this->_deletedEntities); 
     257        } 
    238258         
    239259        /* if (count($entityChangeSet) == 1) { 
     
    384404            return; // entity has not been persisted yet, so nothing more to do. 
    385405        } 
    386         /* Seems unnecessary since _dirtyEntities is filled & cleared on commit, not earlier 
    387          if (isset($this->_dirtyEntities[$oid])) { 
    388          unset($this->_dirtyEntities[$oid]); 
    389          }*/ 
     406 
     407        if (isset($this->_dirtyEntities[$oid])) { 
     408            unset($this->_dirtyEntities[$oid]); 
     409        } 
    390410        if ( ! isset($this->_deletedEntities[$oid])) { 
    391411            $this->_deletedEntities[$oid] = $entity; 
     
    431451        $oid = $entity->getOid(); 
    432452        return isset($this->_newEntities[$oid]) || 
    433                 //isset($this->_dirtyEntities[$oid]) || 
     453                isset($this->_dirtyEntities[$oid]) || 
    434454                isset($this->_deletedEntities[$oid]) || 
    435455                $this->isInIdentityMap($entity); 
     
    585605        $this->_doSave($entity, $visited, $insertNow); 
    586606        if ( ! empty($insertNow)) { 
    587             // We have no choice. This means that there are either new entities 
    588             // with an IDENTITY key generation or with a natural identifier. 
    589             // In both cases we must commit the inserts instantly. 
    590             //TODO: Isnt it enough to only execute the inserts instead of full flush? 
    591             $this->commit(); 
    592             /* The following may be better: 
     607            // We have no choice. This means that there are new entities 
     608            // with an IDENTITY column key generation. 
    593609            $commitOrder = $this->_getCommitOrder($insertNow); 
    594610            foreach ($commitOrder as $class) { 
    595611                $this->_executeInserts($class); 
    596612            } 
    597             //... remove them from _newEntities, or dont store them there in the first place 
    598             */ 
     613            // remove them from _newEntities 
     614            $this->_newEntities = array_diff_key($this->_newEntities, $insertNow); 
    599615        } 
    600616    } 
     
    622638                break; 
    623639            case Doctrine_Entity::STATE_NEW: 
    624                 if ($class->isIdGeneratorIdentity()) { 
     640                $result = $class->getIdGenerator()->generate($entity); 
     641                if ($result == Doctrine_Id_AbstractIdGenerator::POST_INSERT_INDICATOR) { 
    625642                    $insertNow[$entity->getOid()] = $entity; 
    626                     $this->_newEntities[$entity->getOid()] = $entity; 
    627                 } else if ( ! $class->usesIdGenerator()) { 
    628                     $insertNow[$entity->getOid()] = $entity; 
    629                     $this->_newEntities[$entity->getOid()] = $entity; 
    630                     //... 
    631                 } else if ($class->isIdGeneratorSequence()) { 
    632                     // Get the next sequence number 
    633                     //TODO: sequence name? 
    634                     $id = $this->_em->getConnection()->getSequenceManager()->nextId("foo"); 
    635                     $entity->set($class->getSingleIdentifierFieldName(), $id); 
    636                     $this->registerNew($entity); 
    637643                } else { 
    638                     throw new Doctrine_Exception("Unable to handle ID generation of new entity."); 
     644                    $entity->_assignIdentifier($result); 
    639645                } 
     646                $this->registerNew($entity); 
    640647                break; 
    641648            case Doctrine_Entity::STATE_DETACHED: 
     
    671678    } 
    672679 
     680    /** 
     681     * Enter description here... 
     682     * 
     683     * @param Doctrine_Entity $entity 
     684     * @param array $visited 
     685     */ 
    673686    private function _doDelete(Doctrine_Entity $entity, array &$visited) 
    674687    { 
  • trunk/lib/Doctrine/ConnectionFactory.php

    r4866 r4877  
    4646            'firebird' => 'Doctrine_Connection_Firebird', 
    4747            'informix' => 'Doctrine_Connection_Informix', 
    48             'mock'     => 'Doctrine_Connection_Mock'); 
    49       
    50      /** 
    51      * The EventManager that is injected into all created Connections. 
    52      * 
    53      * @var EventManager 
    54      */ 
    55     private $_eventManager; 
    56      
    57     /** 
    58      * The Configuration that is injected into all created Connections. 
    59      * 
    60      * @var Configuration 
    61      */ 
    62     private $_config; 
     48            ); 
    6349     
    6450    public function __construct() 
    6551    { 
    6652         
    67     } 
    68      
    69     /** 
    70      * Sets the Configuration that is injected into all Connections. 
    71      * 
    72      * @param Doctrine_Configuration $config 
    73      */ 
    74     public function setConfiguration(Doctrine_Configuration $config) 
    75     { 
    76         $this->_config = $config; 
    77     } 
    78      
    79     /** 
    80      * Sets the EventManager that is injected into all Connections. 
    81      * 
    82      * @param Doctrine_EventManager $eventManager 
    83      */ 
    84     public function setEventManager(Doctrine_EventManager $eventManager) 
    85     { 
    86         $this->_eventManager = $eventManager; 
    8753    } 
    8854     
     
    9359     * @return Connection 
    9460     */ 
    95     public function createConnection(array $params) 
     61    public function createConnection(array $params, Doctrine_Configuration $config = null, 
     62            Doctrine_EventManager $eventManager = null) 
    9663    { 
    9764        // create default config and event manager, if not set 
    98         if ( ! $this->_config) { 
    99             $this->_config = new Doctrine_Configuration(); 
     65        if ( ! $config) { 
     66            $config = new Doctrine_Configuration(); 
    10067        } 
    101         if ( ! $this->_eventManager) { 
    102             $this->_eventManager = new Doctrine_EventManager(); 
     68        if ( ! $eventManager) { 
     69            $eventManager = new Doctrine_EventManager(); 
    10370        } 
    10471         
     
    11178            $this->_checkParams($params); 
    11279        } 
    113         $className = $this->_drivers[$params['driver']]; 
     80        if (isset($params['driverClass'])) { 
     81            $className = $params['driverClass']; 
     82        } else { 
     83            $className = $this->_drivers[$params['driver']]; 
     84        } 
    11485         
    115         return new $className($params, $this->_config, $this->_eventManager); 
     86        return new $className($params, $config, $eventManager); 
    11687    } 
    11788     
     
    12697         
    12798        // driver 
    128         if ( ! isset($params['driver'])) { 
     99        if ( ! isset($params['driver']) && ! isset($params['driverClass'])) { 
    129100            throw Doctrine_ConnectionFactory_Exception::driverRequired(); 
    130101        } 
     
    133104         
    134105        // driver 
    135         if ( ! isset($this->_drivers[$params['driver']])) { 
     106        if ( isset($params['driver']) && ! isset($this->_drivers[$params['driver']])) { 
    136107            throw Doctrine_ConnectionFactory_Exception::unknownDriver($driverName); 
    137108        } 
  • trunk/lib/Doctrine/DatabasePlatform/FirebirdPlatform.php

    r4725 r4877  
    247247    } 
    248248     
     249    /** 
     250     * Enter description here... 
     251     * 
     252     * @param unknown_type $sequenceName 
     253     * @override 
     254     */ 
     255    public function getSequenceNextValSql($sequenceName) 
     256    { 
     257        return 'SELECT GEN_ID(' . $this->quoteIdentifier($sequenceName) . ', 1) FROM RDB$DATABASE'; 
     258    } 
     259     
    249260} 
    250261 
  • trunk/lib/Doctrine/DatabasePlatform/OraclePlatform.php

    r4776 r4877  
    332332                     'fixed'    => $fixed); 
    333333    } 
     334     
     335    /** 
     336     * Enter description here... 
     337     * 
     338     * @param unknown_type $sequenceName 
     339     * @override 
     340     */ 
     341    public function getSequenceNextValSql($sequenceName) 
     342    { 
     343        return 'SELECT ' . $this->quoteIdentifier($sequenceName) . '.nextval FROM DUAL'; 
     344    } 
    334345} 
    335346 
  • trunk/lib/Doctrine/Entity.php

    r4866 r4877  
    205205    { 
    206206        $this->_entityName = get_class($this); 
    207         $this->_em = Doctrine_EntityManagerFactory::getManager($this->_entityName); 
     207        $this->_em = Doctrine_EntityManager::getActiveEntityManager(); 
    208208        $this->_class = $this->_em->getClassMetadata($this->_entityName); 
    209209        $this->_oid = self::$_index++; 
  • trunk/lib/Doctrine/EntityManager.php

    r4866 r4877  
    6363     
    6464    /** 
     65     * The currently active EntityManager. Only one EntityManager can be active 
     66     * at any time. 
     67     * 
     68     * @var Doctrine::ORM::EntityManager 
     69     */ 
     70    private static $_activeEm; 
     71     
     72    /** 
    6573     * The unique name of the EntityManager. The name is used to bind entity classes 
    6674     * to certain EntityManagers. 
     
    133141    private $_tmpEntityData = array(); 
    134142     
     143    private $_closed = false; 
     144     
    135145    /** 
    136146     * Creates a new EntityManager that operates on the given database connection. 
     
    139149     * @param string $name 
    140150     */ 
    141     public function __construct(Doctrine_Connection $conn, $name = null) 
     151    protected function __construct(Doctrine_Connection $conn, $name, Doctrine_Configuration $config, 
     152            Doctrine_EventManager $eventManager) 
    142153    { 
    143154        $this->_conn = $conn; 
    144155        $this->_name = $name; 
     156        $this->_config = $config; 
     157        $this->_eventManager = $eventManager; 
    145158        $this->_metadataFactory = new Doctrine_ClassMetadata_Factory( 
    146159                $this, new Doctrine_ClassMetadata_CodeDriver()); 
     
    157170    { 
    158171        return $this->_conn; 
     172    } 
     173     
     174    /** 
     175     * Gets the name of the EntityManager. 
     176     * 
     177     * @return string The name of the EntityManager. 
     178     */ 
     179    public function getName() 
     180    { 
     181        return $this->_name; 
    159182    } 
    160183     
     
    303326    public function flush() 
    304327    { 
     328        $this