Changeset 4319

Show
Ignore:
Timestamp:
04/30/08 20:58:42 (14 months ago)
Author:
romanb
Message:

Fixed #963. Since it is impossible for Doctrine to determine where the foreign key resides we added a new option that can be used in such scenarios to help Doctrine: owningSide => true. Please refer to the ticket testcase and the introduction of chapter 4, Relations, for the usage.

Location:
branches/0.11
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11/lib/Doctrine/Export.php

    r4310 r4319  
    253253 
    254254        $query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields; 
    255  
     255         
    256256        $check = $this->getCheckDeclaration($fields); 
    257257 
     
    261261 
    262262        $query .= ')'; 
    263  
    264  
    265263 
    266264        $sql[] = $query; 
  • branches/0.11/lib/Doctrine/Relation.php

    r4249 r4319  
    6161    const MANY  = 2; 
    6262     
    63      
     63    // TRUE => mandatory, everything else is just a default value. this should be refactored 
     64    // since TRUE can bot be used as a default value this way. All values should be default values. 
    6465    protected $definition = array('alias'       => true, 
    6566                                  'foreign'     => true, 
     
    7778                                  'constraint'  => null, 
    7879                                  'equal'       => false, 
    79                                   'cascade'     => array() // application-level cascades 
     80                                  'cascade'     => array(), // application-level cascades 
     81                                  'owningSide'  => false // whether this is the owning side 
    8082                                  ); 
    8183 
     
    140142            } 
    141143        } 
    142  
    143144        $this->definition = $def; 
    144145    } 
  • branches/0.11/lib/Doctrine/Relation/Parser.php

    r4300 r4319  
    114114 
    115115        $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias)); 
    116  
     116         
    117117        return $this->_pending[$alias]; 
    118118    } 
     
    363363 
    364364        $localIdentifierColumnNames = $this->_table->getIdentifierColumnNames(); 
     365        $localIdentifierCount = count($localIdentifierColumnNames); 
    365366        $localIdColumnName = array_pop($localIdentifierColumnNames); 
    366367        $foreignIdentifierColumnNames = $def['table']->getIdentifierColumnNames(); 
     
    381382                } 
    382383            } else { 
    383                 if (count($localIdentifierColumnNames) > 0 || ($def['local'] !== $localIdColumnName &&  
    384                         $def['type'] == Doctrine_Relation::ONE)) { 
     384                if ($localIdentifierCount == 1) { 
     385                    if ($def['local'] == $localIdColumnName && isset($def['owningSide']) 
     386                            && $def['owningSide'] === true) { 
     387                        $def['localKey'] = true; 
     388                    } else if (($def['local'] !== $localIdColumnName && $def['type'] == Doctrine_Relation::ONE)) { 
     389                        $def['localKey'] = true; 
     390                    } 
     391                } else if ($localIdentifierCount > 1) { 
     392                    // It's a composite key and since 'foreign' can not point to a composite 
     393                    // key currently, we know that 'local' must be the foreign key. 
    385394                    $def['localKey'] = true; 
    386395                } 
  • branches/0.11/lib/Doctrine/Table.php

    r4301 r4319  
    594594            } 
    595595        } 
    596         $options['foreignKeys'] = array(); 
    597  
     596         
     597        $options['foreignKeys'] = isset($this->_options['foreignKeys']) ? 
     598                $this->_options['foreignKeys'] : array(); 
    598599 
    599600        if ($parseForeignKeys && $this->getAttribute(Doctrine::ATTR_EXPORT) 
     
    634635                } 
    635636            } 
    636  
     637             
    637638            foreach ($constraints as $k => $def) { 
    638639                $options['foreignKeys'][$k] = array_merge($options['foreignKeys'][$k], $def); 
  • branches/0.11/manual/docs/en/relations.txt

    r4298 r4319  
    88second argument is an array consisting of relation options. The option array contains the following keys: 
    99 
    10 * **local**, the local field of the relation. Local field is the linked field or fields in the defining class. 
    11 * **foreign**, the foreign field of the relation. Foreign field is the linked field or fields in the linked class. 
    12 * **refClass**, the name of the reference / join class. This is needed for many-to-many associations. 
    13 * **onDelete**, the onDelete integrity action. 
    14 * **onUpdate**, the onUpdate integrity action. 
     10* **local**, the local field of the relation. Local field is the linked field in the defining class. 
     11* **foreign**, the foreign field of the relation. Foreign field is the linked field in the linked class. 
     12* **refClass**, the name of the association class. This is only needed for many-to-many associations. 
     13* **owningSide**, (optional) set to boolean true to indicate the owning side of the relation. The owning side is the side that owns the foreign key. There can only be one owning side in an association between two classes. Note that this option is required if Doctrine can't guess the owning side or it's guess is wrong. An example where this is the case is when both 'local' and 'foreign' are part of the identifier (primary key). It never hurts to specify the owning side in this way.',  
     14* **onDelete**, (optional) the onDelete integrity action that is applied on the foreign key constraint when the tables are created by Doctrine. 
     15* **onUpdate**, (optional) the onUpdate integrity action that is applied on the foreign key constraint when the tables are created by Doctrine. 
    1516 
    1617So lets take our first example, say we have two classes Forum_Board and Forum_Thread. Here Forum_Board has many  
  • branches/0.11/tests/Ticket/963TestCase.php

    r4309 r4319  
    7474  public function setUp() 
    7575  { 
    76     $this->hasOne('Ticket_963_User as User', array('local' => 'user_id', 
     76    $this->hasOne('Ticket_963_User as User', array( 
     77                                'local' => 'user_id', 
    7778                                'foreign' => 'id', 
     79                                'owningSide' => true, 
    7880                                'onDelete' => 'CASCADE')); 
    7981  }