1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] DDC-169 - Fixed DatabaseDriver

This commit is contained in:
beberlei 2009-12-05 09:39:11 +00:00
parent ecfa0eee83
commit a0d26a8409
2 changed files with 48 additions and 23 deletions

View File

@ -82,7 +82,7 @@ class DatabaseDriver implements Driver
foreach ($columns as $column) { foreach ($columns as $column) {
// Skip columns that are foreign keys // Skip columns that are foreign keys
foreach ($foreignKeys as $foreignKey) { foreach ($foreignKeys as $foreignKey) {
if (in_array($column->getName(), $foreignKey->getColumns())) { if (in_array(strtolower($column->getName()), array_map('strtolower', $foreignKey->getColumns()))) {
continue(2); continue(2);
} }
} }
@ -142,7 +142,7 @@ class DatabaseDriver implements Driver
$foreignColumn = current($foreignKey->getForeignColumns()); $foreignColumn = current($foreignKey->getForeignColumns());
$associationMapping = array(); $associationMapping = array();
$associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', $localColumn)); $associationMapping['fieldName'] = Inflector::camelize(str_ireplace('_id', '', $localColumn));
$associationMapping['columnName'] = $localColumn; $associationMapping['columnName'] = $localColumn;
$associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName()); $associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName());
$associationMapping['joinColumns'][] = array( $associationMapping['joinColumns'][] = array(

View File

@ -29,25 +29,21 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_sm->dropAndCreateTable($table); $this->_sm->dropAndCreateTable($table);
$this->assertClassMetadataYamlEqualsFile(__DIR__."/DatabaseDriver/simpleYaml.yml", "DbdriverFoo"); $metadata = $this->extractClassMetadata("DbdriverFoo");
}
protected function assertClassMetadataYamlEqualsFile($file, $className) $this->assertArrayHasKey('id', $metadata->fieldMappings);
{ $this->assertEquals('id', $metadata->fieldMappings['id']['fieldName']);
$cm = new ClassMetadataExporter(); $this->assertEquals('id', strtolower($metadata->fieldMappings['id']['columnName']));
$cm->addMappingSource($this->_sm, 'database'); $this->assertEquals('integer', (string)$metadata->fieldMappings['id']['type']);
$exporter = $cm->getExporter('yaml'); $this->assertEquals('', $metadata->fieldMappings['id']['default']);
$metadatas = $cm->getMetadatasForMappingSources(); $this->assertTrue($metadata->fieldMappings['id']['notnull']);
$output = false; $this->assertArrayHasKey('bar', $metadata->fieldMappings);
foreach ($metadatas AS $metadata) { $this->assertEquals('bar', $metadata->fieldMappings['bar']['fieldName']);
if ($metadata->name == $className) { $this->assertEquals('bar', strtolower($metadata->fieldMappings['bar']['columnName']));
$output = $exporter->exportClassMetadata($metadata); $this->assertEquals('string', (string)$metadata->fieldMappings['bar']['type']);
} $this->assertEquals(200, $metadata->fieldMappings['bar']['length']);
} $this->assertTrue($metadata->fieldMappings['bar']['notnull']);
$this->assertTrue($output!==false, "No class matching the name '".$className."' was found!");
$this->assertEquals(strtolower(trim(file_get_contents($file))), strtolower(trim($output)));
} }
public function testCreateYamlWithForeignKeyFromDatabase() public function testCreateYamlWithForeignKeyFromDatabase()
@ -60,8 +56,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$tableB->createColumn('id', 'integer'); $tableB->createColumn('id', 'integer');
$tableB->setPrimaryKey(array('id')); $tableB->setPrimaryKey(array('id'));
$sm = $this->_em->getConnection()->getSchemaManager(); $this->_sm->dropAndCreateTable($tableB);
$sm->dropAndCreateTable($tableB);
$tableA = new \Doctrine\DBAL\Schema\Table("dbdriver_baz"); $tableA = new \Doctrine\DBAL\Schema\Table("dbdriver_baz");
$tableA->createColumn('id', 'integer'); $tableA->createColumn('id', 'integer');
@ -69,9 +64,39 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$tableA->createColumn('bar_id', 'integer'); $tableA->createColumn('bar_id', 'integer');
$tableA->addForeignKeyConstraint('dbdriver_bar', array('bar_id'), array('id')); $tableA->addForeignKeyConstraint('dbdriver_bar', array('bar_id'), array('id'));
$this->_sm = $this->_em->getConnection()->getSchemaManager();
$this->_sm->dropAndCreateTable($tableA); $this->_sm->dropAndCreateTable($tableA);
$this->assertClassMetadataYamlEqualsFile(__DIR__."/DatabaseDriver/fkYaml.yml", "DbdriverBaz"); $metadata = $this->extractClassMetadata("DbdriverBaz");
$this->assertArrayNotHasKey('bar', $metadata->fieldMappings);
$this->assertArrayHasKey('id', $metadata->fieldMappings);
$metadata->associationMappings = \array_change_key_case($metadata->associationMappings, \CASE_LOWER);
$this->assertArrayHasKey('bar', $metadata->associationMappings);
$this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']);
}
/**
*
* @param string $className
* @return ClassMetadata
*/
protected function extractClassMetadata($className)
{
$cm = new ClassMetadataExporter();
$cm->addMappingSource($this->_sm, 'database');
$exporter = $cm->getExporter('yaml');
$metadatas = $cm->getMetadatasForMappingSources();
$output = false;
foreach ($metadatas AS $metadata) {
if ($metadata->name == $className) {
return $metadata;
}
}
$this->fail("No class matching the name '".$className."' was found!");
} }
} }