From 7220c3c1251e149b268d73d7d09dc90b9f3656f9 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 9 May 2013 12:10:37 +0200 Subject: [PATCH] [DDC-2387] Fix DatabaseDriver not working with combinations of composite/association keys. --- .../ORM/Mapping/Driver/DatabaseDriver.php | 3 +- .../ORM/Functional/DatabaseDriverTest.php | 47 +--------------- .../ORM/Functional/DatabaseDriverTestCase.php | 55 +++++++++++++++++++ .../ORM/Functional/Ticket/DDC2387Test.php | 30 ++++++++++ 4 files changed, 89 insertions(+), 46 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTestCase.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2387Test.php diff --git a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php index 15a4e0d2c..007be9639 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php @@ -224,7 +224,8 @@ class DatabaseDriver implements MappingDriver } if ($ids) { - if (count($ids) == 1) { + // We need to check for the columns here, because we might have associations as id as well. + if (count($primaryKeyColumns) == 1) { $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); } diff --git a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php index 48140cc56..0c30a0bef 100644 --- a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php @@ -2,12 +2,9 @@ namespace Doctrine\Tests\ORM\Functional; -require_once __DIR__ . '/../../TestInit.php'; +use Doctrine\ORM\Mapping\ClassMetadataInfo; -use Doctrine\ORM\Mapping\ClassMetadataInfo, - Doctrine\Common\Util\Inflector; - -class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase +class DatabaseDriverTest extends DatabaseDriverTestCase { /** * @var \Doctrine\DBAL\Schema\AbstractSchemaManager @@ -148,44 +145,4 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(0, count($metadatas['DbdriverBaz']->associationMappings), "no association mappings should be detected."); } - - protected function convertToClassMetadata(array $entityTables, array $manyTables = array()) - { - $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm); - $driver->setTables($entityTables, $manyTables); - - $metadatas = array(); - foreach ($driver->getAllClassNames() AS $className) { - $class = new ClassMetadataInfo($className); - $driver->loadMetadataForClass($className, $class); - $metadatas[$className] = $class; - } - - return $metadatas; - } - - /** - * @param string $className - * @return ClassMetadata - */ - 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 $className) { - if (!in_array(strtolower($className), $classNames)) { - continue; - } - $class = new ClassMetadataInfo($className); - $driver->loadMetadataForClass($className, $class); - $metadatas[$className] = $class; - } - - 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; - } } diff --git a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTestCase.php b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTestCase.php new file mode 100644 index 000000000..35fd8b20c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTestCase.php @@ -0,0 +1,55 @@ +_em->getConnection()->getSchemaManager(); + $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($sm); + $driver->setTables($entityTables, $manyTables); + + $metadatas = array(); + foreach ($driver->getAllClassNames() AS $className) { + $class = new ClassMetadataInfo($className); + $driver->loadMetadataForClass($className, $class); + $metadatas[$className] = $class; + } + + return $metadatas; + } + + /** + * @param string $className + * @return ClassMetadata + */ + protected function extractClassMetadata(array $classNames) + { + $classNames = array_map('strtolower', $classNames); + $metadatas = array(); + + $sm = $this->_em->getConnection()->getSchemaManager(); + $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($sm); + + foreach ($driver->getAllClassNames() as $className) { + if (!in_array(strtolower($className), $classNames)) { + continue; + } + $class = new ClassMetadataInfo($className); + $driver->loadMetadataForClass($className, $class); + $metadatas[$className] = $class; + } + + 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; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2387Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2387Test.php new file mode 100644 index 000000000..f74d4e15c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2387Test.php @@ -0,0 +1,30 @@ +addColumn('id', 'integer'); + $product->setPrimaryKey(array('id')); + + $attributes = new \Doctrine\DBAL\Schema\Table('ddc2387_attributes'); + $attributes->addColumn('product_id', 'integer'); + $attributes->addColumn('attribute_name', 'string'); + $attributes->setPrimaryKey(array('product_id', 'attribute_name')); + $attributes->addForeignKeyConstraint('ddc2387_product', array('product_id'), array('product_id')); + + $metadata = $this->convertToClassMetadata(array($product, $attributes), array()); + + $this->assertEquals(ClassMetadataInfo::GENERATOR_TYPE_NONE, $metadata['Ddc2387Attributes']->generatorType); + $this->assertEquals(ClassMetadataInfo::GENERATOR_TYPE_AUTO, $metadata['Ddc2387Product']->generatorType); + } +}