Changeset 4557
- Timestamp:
- 06/24/08 04:29:36 (13 months ago)
- Location:
- branches/0.11/lib/Doctrine
- Files:
-
- 2 modified
-
Import.php (modified) (4 diffs)
-
Import/Mysql.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.11/lib/Doctrine/Import.php
r4317 r4557 105 105 106 106 /** 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 /** 107 131 * lists table constraints 108 132 * … … 356 380 $builder->setOptions($options); 357 381 382 $definitions = array(); 358 383 $classes = array(); 359 384 foreach ($connection->import->listTables() as $table) { … … 362 387 363 388 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); 369 390 } else { 370 391 $classTable = Doctrine_Inflector::tableize($table); … … 372 393 373 394 $definition['className'] = Doctrine_Inflector::classify($classTable); 374 375 395 $definition['columns'] = $connection->import->listTableColumns($table); 376 396 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; 379 413 $classes[] = $definition['className']; 380 414 } 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 } 381 429 } 382 430 383 431 return $classes; 384 432 } 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 } 385 448 } -
branches/0.11/lib/Doctrine/Import/Mysql.php
r4252 r4557 99 99 100 100 /** 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; 109 131 } 110 132