1
0
mirror of synced 2025-03-27 18:33:53 +03:00

Merge branch 'DDC-627'

This commit is contained in:
Benjamin Eberlei 2010-06-13 20:10:10 +02:00
commit c69b2d2b54
2 changed files with 43 additions and 19 deletions

View File

@ -73,11 +73,20 @@ class DatabaseDriver implements Driver
$foreignKeys = array(); $foreignKeys = array();
} }
$allForeignKeyColumns = array();
foreach ($foreignKeys AS $foreignKey) {
$allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
}
$indexes = $this->_sm->listTableIndexes($tableName); $indexes = $this->_sm->listTableIndexes($tableName);
$ids = array(); $ids = array();
$fieldMappings = array(); $fieldMappings = array();
foreach ($columns as $column) { foreach ($columns as $column) {
if (in_array($column->getName(), $allForeignKeyColumns)) {
continue;
}
$fieldMapping = array(); $fieldMapping = array();
if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) { if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) {
$fieldMapping['id'] = true; $fieldMapping['id'] = true;
@ -123,7 +132,7 @@ class DatabaseDriver implements Driver
$fkCols = $foreignKey->getForeignColumns(); $fkCols = $foreignKey->getForeignColumns();
$associationMapping = array(); $associationMapping = array();
$associationMapping['fieldName'] = Inflector::camelize(str_ireplace('_id', '', $localColumn)); $associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower($localColumn)));
$associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName()); $associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName());
for ($i = 0; $i < count($cols); $i++) { for ($i = 0; $i < count($cols); $i++) {
@ -146,16 +155,19 @@ class DatabaseDriver implements Driver
} }
/** /**
* {@inheritDoc} * Return all the class names supported by this driver.
*
* IMPORTANT: This method must return an array of table names because we need
* to know the table name after we inflect it to create the entity class name.
*
* @return array
*/ */
public function getAllClassNames() public function getAllClassNames()
{ {
$classes = array(); $classes = array();
foreach ($this->_sm->listTables() as $table) { foreach ($this->_sm->listTableNames() as $tableName) {
// This method must return an array of table names because we need $classes[] = $tableName;
// to know the table name after we inflect it to create the entity class name.
$classes[] = $table->getName();
} }
return $classes; return $classes;

View File

@ -21,7 +21,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_sm = $this->_em->getConnection()->getSchemaManager(); $this->_sm = $this->_em->getConnection()->getSchemaManager();
} }
public function testCreateSimpleYamlFromDatabase() public function testLoadMetadataFromDatabase()
{ {
if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Platform does not support foreign keys.'); $this->markTestSkipped('Platform does not support foreign keys.');
@ -34,7 +34,10 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_sm->dropAndCreateTable($table); $this->_sm->dropAndCreateTable($table);
$metadata = $this->extractClassMetadata("DbdriverFoo"); $metadatas = $this->extractClassMetadata(array("DbdriverFoo"));
$this->assertArrayHasKey('dbdriver_foo', $metadatas);
$metadata = $metadatas['dbdriver_foo'];
$this->assertArrayHasKey('id', $metadata->fieldMappings); $this->assertArrayHasKey('id', $metadata->fieldMappings);
$this->assertEquals('id', $metadata->fieldMappings['id']['fieldName']); $this->assertEquals('id', $metadata->fieldMappings['id']['fieldName']);
@ -49,7 +52,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertTrue($metadata->fieldMappings['bar']['nullable']); $this->assertTrue($metadata->fieldMappings['bar']['nullable']);
} }
public function testCreateYamlWithForeignKeyFromDatabase() public function testLoadMetadataWithForeignKeyFromDatabase()
{ {
if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Platform does not support foreign keys.'); $this->markTestSkipped('Platform does not support foreign keys.');
@ -69,15 +72,18 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_sm->dropAndCreateTable($tableA); $this->_sm->dropAndCreateTable($tableA);
$metadata = $this->extractClassMetadata("DbdriverBaz"); $metadatas = $this->extractClassMetadata(array("DbdriverBar", "DbdriverBaz"));
$this->assertArrayNotHasKey('bar', $metadata->fieldMappings); $this->assertArrayHasKey('dbdriver_baz', $metadatas);
$this->assertArrayHasKey('id', $metadata->fieldMappings); $bazMetadata = $metadatas['dbdriver_baz'];
$metadata->associationMappings = \array_change_key_case($metadata->associationMappings, \CASE_LOWER); $this->assertArrayNotHasKey('barId', $bazMetadata->fieldMappings, "The foreign Key field should not be inflected as 'barId' field, its an association.");
$this->assertArrayHasKey('id', $bazMetadata->fieldMappings);
$this->assertArrayHasKey('bar', $metadata->associationMappings); $bazMetadata->associationMappings = \array_change_key_case($bazMetadata->associationMappings, \CASE_LOWER);
$this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']);
$this->assertArrayHasKey('bar', $bazMetadata->associationMappings);
$this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $bazMetadata->associationMappings['bar']);
} }
/** /**
@ -85,18 +91,24 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
* @param string $className * @param string $className
* @return ClassMetadata * @return ClassMetadata
*/ */
protected function extractClassMetadata($className) protected function extractClassMetadata(array $classNames)
{ {
$classNames = array_map('strtolower', $classNames);
$metadatas = array();
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm); $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm);
foreach ($driver->getAllClassNames() as $dbTableName) { foreach ($driver->getAllClassNames() as $dbTableName) {
if (strtolower(Inflector::classify($dbTableName)) != strtolower($className)) { if (!in_array(strtolower(Inflector::classify($dbTableName)), $classNames)) {
continue; continue;
} }
$class = new ClassMetadataInfo($dbTableName); $class = new ClassMetadataInfo($dbTableName);
$driver->loadMetadataForClass($dbTableName, $class); $driver->loadMetadataForClass($dbTableName, $class);
return $class; $metadatas[strtolower($dbTableName)] = $class;
} }
$this->fail("No class matching the name '".$className."' was found!"); if (count($metadatas) != count($classNames)) {
$this->fail("Have not found all classes matching the names '" . implode(", ", $classNames) . "' only tables " . implode(", ", array_keys($metadatas)));
}
return $metadatas;
} }
} }