Changeset 4557

Show
Ignore:
Timestamp:
06/24/08 04:29:36 (13 months ago)
Author:
jwage
Message:

fixes #625 - Implementing generating relationships from existing databases for MySQL.

Location:
branches/0.11/lib/Doctrine
Files:
2 modified

Legend:

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

    r4317 r4557  
    105105 
    106106    /** 
     107     * lists table relations 
     108     * 
     109     * Expects an array of this format to be returned with all the relationships in it where the key is  
     110     * the name of the foreign table, and the value is an array containing the local and foreign column 
     111     * name 
     112     * 
     113     * Array 
     114     * ( 
     115     *   [groups] => Array 
     116     *     ( 
     117     *        [local] => group_id 
     118     *        [foreign] => id 
     119     *     ) 
     120     * ) 
     121     * 
     122     * @param string $table     database table name 
     123     * @return array 
     124     */ 
     125    public function listTableRelations($table) 
     126    { 
     127        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.'); 
     128    } 
     129 
     130    /** 
    107131     * lists table constraints 
    108132     * 
     
    356380          $builder->setOptions($options); 
    357381 
     382          $definitions = array(); 
    358383          $classes = array(); 
    359384          foreach ($connection->import->listTables() as $table) { 
     
    362387 
    363388              if( ! isset($options['singularize']) || $options['singularize'] !== false) { 
    364                   $e = explode('_', Doctrine_Inflector::tableize($table)); 
    365                   foreach ($e as $k => $v) { 
    366                       $e[$k] = Doctrine_Inflector::singularize($v); 
    367                   } 
    368                   $classTable = implode('_', $e); 
     389                  $classTable = $this->_singularizeTableName($table); 
    369390              } else { 
    370391                  $classTable = Doctrine_Inflector::tableize($table); 
     
    372393 
    373394              $definition['className'] = Doctrine_Inflector::classify($classTable); 
    374  
    375395              $definition['columns'] = $connection->import->listTableColumns($table); 
    376396 
    377               $builder->buildRecord($definition); 
    378  
     397              try { 
     398                  $definition['relations'] = array(); 
     399                  $relations = $connection->import->listTableRelations($table); 
     400                  foreach ($relations as $table => $options) { 
     401                      if( ! isset($options['singularize']) || $options['singularize'] !== false) { 
     402                          $relClassTable = $this->_singularizeTableName($table); 
     403                      } else { 
     404                          $relClassTable = Doctrine_Inflector::tableize($table); 
     405                      } 
     406                      $class = Doctrine_Inflector::classify($relClassTable); 
     407                      $definition['relations'][$class] = array('local'          => $options['local'], 
     408                                                               'foreign'        => $options['foreign']); 
     409                  } 
     410              } catch (Exception $e) {} 
     411 
     412              $definitions[$definition['className']] = $definition; 
    379413              $classes[] = $definition['className']; 
    380414          } 
     415 
     416          // Build opposite end of relationships 
     417          foreach ($definitions as $defClass => $definition) { 
     418              foreach ($definition['relations'] as $relClass => $relation) { 
     419                  $definitions[$relClass]['relations'][$defClass] = array('local'   => $relation['foreign'], 
     420                                                                          'foreign' => $relation['local'], 
     421                                                                          'type'    => 'many'); 
     422              } 
     423          } 
     424 
     425          // Build records 
     426          foreach ($definitions as $definition) { 
     427              $builder->buildRecord($definition); 
     428          } 
    381429        } 
    382430 
    383431        return $classes; 
    384432    } 
     433 
     434    /** 
     435     * Singularize a table name 
     436     * 
     437     * @param string $tableName  
     438     * @return $singularTableName 
     439     */ 
     440    protected function _singularizeTableName($tableName) 
     441    { 
     442        $e = explode('_', Doctrine_Inflector::tableize($tableName)); 
     443        foreach ($e as $k => $v) { 
     444            $e[$k] = Doctrine_Inflector::singularize($v); 
     445        } 
     446        return implode('_', $e); 
     447    } 
    385448} 
  • branches/0.11/lib/Doctrine/Import/Mysql.php

    r4252 r4557  
    9999 
    100100    /** 
    101      * lists table foreign keys 
    102      * 
    103      * @param string $table     database table name 
    104      * @return array 
    105      */ 
    106     public function listTableForeignKeys($table)  
    107     { 
    108         $sql = 'SHOW CREATE TABLE ' . $this->conn->quoteIdentifier($table, true);     
     101     * lists table relations 
     102     * 
     103     * Expects an array of this format to be returned with all the relationships in it where the key is  
     104     * the name of the foreign table, and the value is an array containing the local and foreign column 
     105     * name 
     106     * 
     107     * Array 
     108     * ( 
     109     *   [groups] => Array 
     110     *     ( 
     111     *        [local] => group_id 
     112     *        [foreign] => id 
     113     *     ) 
     114     * ) 
     115     * 
     116     * @param string $table     database table name 
     117     * @return array 
     118     */ 
     119    public function listTableRelations($tableName) 
     120    { 
     121        $relations = array(); 
     122        $sql = "SELECT column_name, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.key_column_usage WHERE table_name = '" . $tableName . "' AND table_schema = '" . $this->conn->getDatabaseName() . "' and REFERENCED_COLUMN_NAME is not NULL"; 
     123        $results = $this->conn->fetchAssoc($sql); 
     124        foreach ($results as $result) 
     125        { 
     126            $result = array_change_key_case($result, CASE_LOWER); 
     127            $relations[$result['referenced_table_name']] = array('local'   => $result['column_name'], 
     128                                                                 'foreign' => $result['referenced_column_name']); 
     129        } 
     130        return $relations; 
    109131    } 
    110132