Fixed DDC-627 and DDC-616
This commit is contained in:
parent
f2aacf44c8
commit
b7db8df7ef
@ -73,11 +73,20 @@ class DatabaseDriver implements Driver
|
||||
$foreignKeys = array();
|
||||
}
|
||||
|
||||
$allForeignKeyColumns = array();
|
||||
foreach ($foreignKeys AS $foreignKey) {
|
||||
$allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
|
||||
}
|
||||
|
||||
$indexes = $this->_sm->listTableIndexes($tableName);
|
||||
|
||||
$ids = array();
|
||||
$fieldMappings = array();
|
||||
foreach ($columns as $column) {
|
||||
if (in_array($column->getName(), $allForeignKeyColumns)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldMapping = array();
|
||||
if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) {
|
||||
$fieldMapping['id'] = true;
|
||||
@ -123,7 +132,7 @@ class DatabaseDriver implements Driver
|
||||
$fkCols = $foreignKey->getForeignColumns();
|
||||
|
||||
$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());
|
||||
|
||||
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()
|
||||
{
|
||||
$classes = array();
|
||||
|
||||
foreach ($this->_sm->listTables() as $table) {
|
||||
// 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.
|
||||
$classes[] = $table->getName();
|
||||
foreach ($this->_sm->listTableNames() as $tableName) {
|
||||
$classes[] = $tableName;
|
||||
}
|
||||
|
||||
return $classes;
|
||||
|
@ -21,7 +21,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
$this->_sm = $this->_em->getConnection()->getSchemaManager();
|
||||
}
|
||||
|
||||
public function testCreateSimpleYamlFromDatabase()
|
||||
public function testLoadMetadataFromDatabase()
|
||||
{
|
||||
if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
||||
$this->markTestSkipped('Platform does not support foreign keys.');
|
||||
@ -34,7 +34,10 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
|
||||
$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->assertEquals('id', $metadata->fieldMappings['id']['fieldName']);
|
||||
@ -49,7 +52,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
$this->assertTrue($metadata->fieldMappings['bar']['nullable']);
|
||||
}
|
||||
|
||||
public function testCreateYamlWithForeignKeyFromDatabase()
|
||||
public function testLoadMetadataWithForeignKeyFromDatabase()
|
||||
{
|
||||
if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
||||
$this->markTestSkipped('Platform does not support foreign keys.');
|
||||
@ -69,15 +72,18 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
|
||||
$this->_sm->dropAndCreateTable($tableA);
|
||||
|
||||
$metadata = $this->extractClassMetadata("DbdriverBaz");
|
||||
$metadatas = $this->extractClassMetadata(array("DbdriverBar", "DbdriverBaz"));
|
||||
|
||||
$this->assertArrayNotHasKey('bar', $metadata->fieldMappings);
|
||||
$this->assertArrayHasKey('id', $metadata->fieldMappings);
|
||||
$this->assertArrayHasKey('dbdriver_baz', $metadatas);
|
||||
$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);
|
||||
$this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']);
|
||||
$bazMetadata->associationMappings = \array_change_key_case($bazMetadata->associationMappings, \CASE_LOWER);
|
||||
|
||||
$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
|
||||
* @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);
|
||||
foreach ($driver->getAllClassNames() as $dbTableName) {
|
||||
if (strtolower(Inflector::classify($dbTableName)) != strtolower($className)) {
|
||||
if (!in_array(strtolower(Inflector::classify($dbTableName)), $classNames)) {
|
||||
continue;
|
||||
}
|
||||
$class = new ClassMetadataInfo($dbTableName);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user