Changeset 4800 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
08/24/08 20:27:24 (5 months ago)
Author:
romanb
Message:

Minor updates

Location:
trunk/lib/Doctrine
Files:
12 modified

Legend:

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

    r4789 r4800  
    9090     * association). 
    9191     * 
    92      * @var unknown_type 
     92     * @var string 
    9393     */ 
    9494    protected $_targetEntityName; 
     
    105105    /** 
    106106     * Identifies the field on the owning side that has the mapping for the 
    107      * association. 
     107     * association. This is only set on the inverse side of an association. 
    108108     * 
    109109     * @var string 
    110110     */ 
    111111    protected $_mappedByFieldName; 
     112     
     113    /** 
     114     * The name of the join table, if any. 
     115     * 
     116     * @var string 
     117     */ 
     118    protected $_joinTable; 
     119     
     120    //protected $_mapping = array(); 
    112121     
    113122    /** 
     
    119128    public function __construct(array $mapping) 
    120129    { 
     130        /*$this->_mapping = array( 
     131            'fieldName' => null, 
     132            'sourceEntity' => null, 
     133            'targetEntity' => null, 
     134            'mappedBy' => null, 
     135            'joinColumns' => null, 
     136            'joinTable' => null, 
     137            'accessor' => null, 
     138            'mutator' => null, 
     139            'optional' => true, 
     140            'cascade' => array() 
     141        ); 
     142        $this->_mapping = array_merge($this->_mapping, $mapping);*/ 
    121143        $this->_validateAndCompleteMapping($mapping); 
    122          
    123         $this->_sourceEntityName = $mapping['sourceEntity']; 
    124         $this->_targetEntityName = $mapping['targetEntity']; 
     144    } 
     145     
     146    /** 
     147     * Validates & completes the mapping. Mapping defaults are applied here. 
     148     * 
     149     * @param array $mapping 
     150     */ 
     151    protected function _validateAndCompleteMapping(array $mapping) 
     152    {         
     153        // Mandatory attributes for both sides 
     154        if ( ! isset($mapping['fieldName'])) { 
     155            throw Doctrine_MappingException::missingFieldName(); 
     156        } 
    125157        $this->_sourceFieldName = $mapping['fieldName']; 
    126158         
    127         if ( ! $this->_isOwningSide) { 
    128             $this->_mappedByFieldName = $mapping['mappedBy']; 
    129         }         
    130     } 
    131      
    132     /** 
    133      * Validates & completes the mapping. Mapping defaults are applied here. 
    134      * 
    135      * @param array $mapping 
    136      * @return array  The validated & completed mapping. 
    137      */ 
    138     protected function _validateAndCompleteMapping(array $mapping) 
    139     {         
    140         if (isset($mapping['mappedBy'])) { 
    141             // Inverse side, mapping can be found on the owning side. 
    142             $this->_isOwningSide = false; 
    143         } else { 
    144             // Owning side 
    145         } 
    146          
    147         // Mandatory attributes 
    148         if ( ! isset($mapping['fieldName'])) { 
    149             throw Doctrine_MappingException::missingFieldName(); 
    150         } 
    151159        if ( ! isset($mapping['sourceEntity'])) { 
    152160            throw Doctrine_MappingException::missingSourceEntity($mapping['fieldName']); 
    153161        } 
     162        $this->_sourceEntityName = $mapping['sourceEntity']; 
     163         
    154164        if ( ! isset($mapping['targetEntity'])) { 
    155165            throw Doctrine_MappingException::missingTargetEntity($mapping['fieldName']); 
    156166        } 
     167        $this->_targetEntityName = $mapping['targetEntity']; 
    157168         
    158         // Optional attributes 
    159         $this->_customAccessor = isset($mapping['accessor']) ? $mapping['accessor'] : null; 
    160         $this->_customMutator = isset($mapping['mutator']) ? $mapping['mutator'] : null; 
    161         $this->_isOptional = isset($mapping['isOptional']) ? (bool)$mapping['isOptional'] : true; 
    162         $this->_cascades = isset($mapping['cascade']) ? (array)$mapping['cascade'] : array(); 
    163  
    164         return $mapping; 
    165     } 
    166      
    167     protected function _validateAndCompleteInverseSideMapping() 
    168     { 
     169        // Mandatory and optional attributes for either side 
     170        if ( ! isset($mapping['mappedBy'])) {             
     171            // Optional 
     172            if (isset($mapping['joinTable'])) { 
     173                $this->_joinTable = $mapping['joinTable'];    
     174            } 
     175        } else { 
     176            $this->_isOwningSide = false; 
     177            $this->_mappedByFieldName = $mapping['mappedBy']; 
     178        } 
    169179         
    170     } 
    171      
    172     protected function _validateAndCompleteOwningSideMapping() 
    173     { 
    174          
     180        // Optional attributes for both sides 
     181        if (isset($mapping['accessor'])) { 
     182            $this->_customAccessor = $mapping['accessor']; 
     183        } 
     184        if (isset($mapping['mutator'])) { 
     185            $this->_customMutator = $mapping['mutator']; 
     186        } 
     187        $this->_isOptional = isset($mapping['optional']) ? 
     188                (bool)$mapping['optional'] : true; 
     189        $this->_cascades = isset($mapping['cascade']) ? 
     190                (array)$mapping['cascade'] : array(); 
    175191    } 
    176192     
     
    295311    { 
    296312        return $this->_targetEntityName; 
     313    } 
     314     
     315    /** 
     316     * Gets the name of the join table. 
     317     * 
     318     * @return string 
     319     */ 
     320    public function getJoinTable() 
     321    { 
     322        return $this->_joinTable; 
    297323    } 
    298324     
  • trunk/lib/Doctrine/Association/ManyToMany.php

    r4718 r4800  
    1111 */ 
    1212class Doctrine_Association_ManyToMany extends Doctrine_Association 
    13 { 
    14     /** 
    15      * Whether the mapping uses an association class. 
    16      * 
    17      * @var boolean 
    18      */ 
    19     private $_usesAssociationClass; 
    20      
     13{     
    2114    /** 
    2215     * The name of the association class (if an association class is used). 
     
    2417     * @var string 
    2518     */ 
    26     private $_associationClassName; 
    27      
    28     /** 
    29      * The name of the intermediate table. 
    30      * 
    31      * @var string 
    32      */ 
    33     private $_relationTableName; 
     19    private $_associationClass; 
    3420     
    3521    /** The field in the source table that corresponds to the key in the relation table */ 
     
    4531    protected $_targetRelationKeyColumns; 
    4632     
     33    /** 
     34     * Constructor. 
     35     * Creates a new ManyToManyMapping. 
     36     * 
     37     * @param array $mapping  The mapping info. 
     38     */ 
     39    public function __construct(array $mapping) 
     40    { 
     41        parent::__construct($mapping); 
     42    } 
     43     
     44    /** 
     45     * Validates and completes the mapping. 
     46     * 
     47     * @param array $mapping 
     48     * @override 
     49     */ 
     50    protected function _validateAndCompleteMapping(array $mapping) 
     51    { 
     52        parent::_validateAndCompleteMapping($mapping); 
     53         
     54        if ($this->isOwningSide()) { 
     55            // many-many owning MUST have a join table 
     56            if ( ! isset($mapping['joinTable'])) { 
     57                throw Doctrine_MappingException::joinTableRequired($mapping['fieldName']); 
     58            } 
     59         
     60            // optional attributes for many-many owning side 
     61            $this->_associationClass = isset($mapping['associationClass']) ? 
     62                    $mapping['associationClass'] : null; 
     63        } 
     64    } 
    4765     
    4866    /** 
     
    5472    public function usesAssociationClass() 
    5573    { 
    56          
    57     } 
    58      
    59     /** 
    60      * Gets the name of the intermediate table. 
    61      * 
    62      * @return string 
    63      */ 
    64     public function getRelationTableName() 
    65     { 
    66         return $this->_relationTableName; 
     74        return $this->_associationClass !== null; 
    6775    } 
    6876     
     
    7482    public function getAssociationClassName() 
    7583    { 
    76         return $this->_associationClassName; 
     84        return $this->_associationClass; 
    7785    } 
    7886} 
  • trunk/lib/Doctrine/Association/OneToMany.php

    r4792 r4800  
    6464    { 
    6565        parent::__construct($mapping); 
    66         // one side in one-many can currently never be owning side, we may support that later 
    67         $this->_isOwningSide = false; 
    6866    } 
    6967     
     
    7775    protected function _validateAndCompleteMapping(array $mapping) 
    7876    { 
    79         $mapping = parent::_validateAndCompleteMapping($mapping); 
     77        parent::_validateAndCompleteMapping($mapping); 
     78         
    8079        // one-side MUST be inverse (must have mappedBy) 
    8180        if ( ! isset($mapping['mappedBy'])) { 
     
    8382        } 
    8483         
    85         return $mapping; 
     84        $this->_deleteOrphans = isset($mapping['deleteOrphans']) ? 
     85                (bool)$mapping['deleteOrphans'] : false; 
    8686    } 
    8787     
  • trunk/lib/Doctrine/Association/OneToOne.php

    r4776 r4800  
    4848    protected $_targetToSourceKeyColumns = array(); 
    4949     
    50     /** Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null) */ 
     50    /** 
     51     * Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null) 
     52     *  
     53     * @var boolean 
     54     */ 
    5155    protected $_deleteOrphans = false; 
    5256     
     
    6064    { 
    6165        parent::__construct($mapping); 
    62         if ($this->isOwningSide()) { 
    63             $this->_sourceToTargetKeyColumns = $mapping['joinColumns']; 
    64             $this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns); 
    65         } 
    6666    } 
    6767     
     
    7373     * @override 
    7474     */ 
    75     protected function _validateMapping(array $mapping) 
     75    protected function _validateAndCompleteMapping(array $mapping) 
    7676    { 
    77         $mapping = parent::_validateMapping($mapping); 
     77        parent::_validateAndCompleteMapping($mapping); 
    7878         
    7979        if ($this->isOwningSide()) { 
    8080            if ( ! isset($mapping['joinColumns'])) { 
    81                 throw Doctrine_MappingException::missingJoinColumns(); 
     81                throw Doctrine_MappingException::invalidMapping($this->_sourceFieldName); 
    8282            } 
     83            $this->_sourceToTargetKeyColumns = $mapping['joinColumns']; 
     84            $this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns); 
    8385        } 
     86         
     87        $this->_deleteOrphans = isset($mapping['deleteOrphans']) ? 
     88                (bool)$mapping['deleteOrphans'] : false; 
    8489         
    8590        return $mapping; 
     
    146151            $otherEntity = Doctrine_Null::$INSTANCE; 
    147152        } 
    148         $entity->_rawSetReference($this->_sourceFieldName, $otherEntity); 
     153        $entity->_internalSetReference($this->_sourceFieldName, $otherEntity); 
    149154    } 
    150155     
  • trunk/lib/Doctrine/Entity.php

    r4789 r4800  
    526526     * @param string $fieldName 
    527527     * @param mixed $value 
    528      */ 
    529     final public function _internalSetReference($name, $value) 
     528     * @param boolean $completeBidirectional Whether to complete bidirectional associations 
     529     *                                       (creating the back-reference). Should only 
     530     *                                       be used by hydration. 
     531     */ 
     532    final public function _internalSetReference($name, $value, $completeBidirectional = false) 
    530533    { 
    531534        if ($value === Doctrine_Null::$INSTANCE) { 
     
    547550 
    548551        $this->_references[$name] = $value; 
     552         
     553        if ($completeBidirectional && $rel->isOneToOne()) { 
     554            //TODO: check if $rel is bidirectional, if yes create the back-reference 
     555            if ($rel->isOwningSide()) { 
     556                //TODO: how to check if its bidirectional? should be as efficient as possible 
     557                /*$targetClass = $this->_em->getClassMetadata($rel->getTargetEntityName()); 
     558                if (($invAssoc = $targetClass->getInverseAssociation($name)) !== null) { 
     559                    $value->_internalSetReference($invAssoc->getSourceFieldName(), $this); 
     560                }*/ 
     561            } else { 
     562                // for sure bi-directional, as there is no inverse side in unidirectional 
     563                $value->_internalSetReference($rel->getMappedByFieldName(), $this); 
     564            } 
     565        } 
    549566    } 
    550567 
  • trunk/lib/Doctrine/Hydrator/RecordDriver.php

    r4776 r4800  
    7575            $coll = $this->getElementCollection($relatedClass->getClassName()); 
    7676            $coll->setReference($entity, $relation); 
    77             $entity->_internalSetReference($name, $coll); 
     77            $entity->_internalSetReference($name, $coll, true); 
    7878            $this->_initializedRelations[$entity->getOid()][$name] = true; 
    7979        } 
     
    109109    public function setRelatedElement(Doctrine_Entity $entity1, $property, $entity2) 
    110110    { 
    111         $entity1->_internalSetReference($property, $entity2); 
     111        $entity1->_internalSetReference($property, $entity2, true); 
    112112    } 
    113113     
     
    152152    } 
    153153     
    154      
    155154} 
  • trunk/lib/Doctrine/MappingException.php

    r4792 r4800  
    11<?php 
     2/* 
     3 *  $Id$ 
     4 * 
     5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
     8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
     9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
     10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
     11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
     12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
     13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
     14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
     15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     16 * 
     17 * This software consists of voluntary contributions made by many individuals 
     18 * and is licensed under the LGPL. For more information, see 
     19 * <http://www.phpdoctrine.org>. 
     20 */ 
     21 
     22#namespace Doctrine::ORM::Exceptions; 
    223 
    324/** 
     
    5374        return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute."); 
    5475    } 
     76     
     77    public static function joinTableRequired($fieldName) 
     78    { 
     79        return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute."); 
     80    } 
     81     
     82    /** 
     83     * Generic exception for invalid mappings. 
     84     * 
     85     * @param string $fieldName 
     86     */ 
     87    public static function invalidMapping($fieldName) 
     88    { 
     89        return new self("The mapping of field '$fieldName' is invalid."); 
     90    } 
    5591} 
    5692 
  • trunk/lib/Doctrine/Query/SqlExecutor/Abstract.php

    r4422 r4800  
    2323 * Doctrine_Query_QueryResult 
    2424 * 
    25  * @package     Doctrine 
    26  * @subpackage  Query 
    2725 * @author      Roman Borschel <roman@code-factory.org> 
    2826 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
     
    3432{ 
    3533    // [TODO] Remove me later! 
    36     public $AST; 
     34    //public $AST; 
    3735 
    3836    protected $_sqlStatements; 
     
    4139    { 
    4240        // [TODO] Remove me later! 
    43         $this->AST = $AST; 
     41        //$this->AST = $AST; 
    4442    } 
    45  
    4643 
    4744    /** 
     
    5552    } 
    5653 
    57  
    5854    /** 
    5955     * Executes all sql statements. 
     
    6359     */ 
    6460    abstract public function execute(Doctrine_Connection $conn, array $params); 
    65  
    6661 
    6762    /** 
  • trunk/lib/Doctrine/Query/SqlExecutor/MultiTableDelete.php

    r4422 r4800  
    2424 * Class Table Inheritance (JOINED). 
    2525 * 
    26  * @package     Doctrine 
    27  * @subpackage  Query 
    2826 * @author      Roman Borschel <roman@code-factory.org> 
    2927 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
     
    3331 * @todo For a good implementation that uses temporary tables see the Hibernate sources: 
    3432 *       (org.hibernate.hql.ast.exec.MultiTableDeleteExecutor). 
     33 * @todo Rename to MultiTableDeleteExecutor 
    3534 */ 
    3635class Doctrine_Query_SqlExecutor_MultiTableDelete extends Doctrine_Query_SqlExecutor_Abstract 
    3736{ 
     37    /** 
     38     * Enter description here... 
     39     * 
     40     * @param Doctrine_Query_Production $AST 
     41     */ 
     42    public function __construct(Doctrine_Query_Production $AST) 
     43    { 
     44        // TODO: Inspect the AST, create the necessary SQL queries and store them 
     45        // in $this->_sqlStatements 
     46    } 
    3847     
    39      
     48    /** 
     49     * Executes all sql statements. 
     50     * 
     51     * @param Doctrine_Connection $conn  The database connection that is used to execute the queries. 
     52     * @param array $params  The parameters. 
     53     * @override 
     54     */ 
     55    public function execute(Doctrine_Connection $conn, array $params) 
     56    { 
     57        //... 
     58    }    
    4059} 
  • trunk/lib/Doctrine/Query/SqlExecutor/MultiTableUpdate.php

    r4422 r4800  
    2424 * Class Table Inheritance (JOINED). 
    2525 * 
    26  * @package     Doctrine 
    27  * @subpackage  Query 
    2826 * @author      Roman Borschel <roman@code-factory.org> 
    2927 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
     
    3331 * @todo For a good implementation that uses temporary tables see the Hibernate sources: 
    3432 *       (org.hibernate.hql.ast.exec.MultiTableUpdateExecutor). 
     33 * @todo Rename to MultiTableUpdateExecutor 
    3534 */ 
    3635class Doctrine_Query_SqlExecutor_MultiTableUpdate extends Doctrine_Query_SqlExecutor_Abstract 
     
    4241    } 
    4342     
     43    /** 
     44     * Executes all sql statements. 
     45     * 
     46     * @param Doctrine_Connection $conn  The database connection that is used to execute the queries. 
     47     * @param array $params  The parameters. 
     48     * @override 
     49     */ 
     50    public function execute(Doctrine_Connection $conn, array $params) 
     51    { 
     52        //... 
     53    } 
    4454} 
  • trunk/lib/Doctrine/Query/SqlExecutor/SingleSelect.php

    r4422 r4800  
    2323 * Executor that executes the SQL statement for simple DQL SELECT statements. 
    2424 * 
    25  * @package     Doctrine 
    26  * @subpackage  Abstract 
    2725 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    2826 * @author      Roman Borschel <roman@code-factory.org> 
  • trunk/lib/Doctrine/Query/SqlExecutor/SingleTableDeleteUpdate.php

    r4723 r4800  
    2424 * that are mapped to a single table. 
    2525 * 
    26  * @package     Doctrine 
    27  * @subpackage  SingleTableDeleteUpdate 
    2826 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL 
    2927 * @author      Roman Borschel <roman@code-factory.org> 
     
    3129 * @link        www.phpdoctrine.org 
    3230 * @since       2.0 
     31 * @todo This is exactly the same as SingleSelectExecutor. Unify in SingleStatementExecutor.  
    3332 */ 
    3433class Doctrine_Query_SqlExecutor_SingleTableDeleteUpdate extends Doctrine_Query_SqlExecutor_Abstract