From 6d02c7e1a59a6d19d695b3c564a452d138478e1a Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 1 May 2013 23:10:13 +0200 Subject: [PATCH] [DDC-2136] Fix exporting to YAML and XML with assocation keys. --- .travis.yml | 4 ++-- composer.json | 3 +++ lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php | 1 + .../ORM/Tools/Export/Driver/XmlExporter.php | 14 +++++++++++++- .../ORM/Tools/Export/Driver/YamlExporter.php | 8 +++++--- .../Export/AbstractClassMetadataExporterTest.php | 2 +- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 177b5ba6c..a97513344 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,6 @@ before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database doctrine_tests;' -U postgres; fi" - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database doctrine_tests_tmp;' -U postgres; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS doctrine_tests_tmp;create database IF NOT EXISTS doctrine_tests;'; fi" - - composer install --prefer-source + - composer install --prefer-source --dev -script: phpunit --configuration tests/travis/$DB.travis.xml \ No newline at end of file +script: phpunit --configuration tests/travis/$DB.travis.xml diff --git a/composer.json b/composer.json index 52960133d..f74058bac 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,9 @@ "doctrine/dbal": ">=2.4-beta,<2.5-dev", "symfony/console": "2.*" }, + "require-dev": { + "symfony/yaml": "2.1" + }, "suggest": { "symfony/yaml": "If you want to use YAML Metadata Mapping Driver" }, diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index 5bd58ef79..9e8818a16 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -307,6 +307,7 @@ class YamlDriver extends FileDriver } } + // Evaluate oneToOne relationships if (isset($element['oneToOne'])) { foreach ($element['oneToOne'] as $name => $oneToOneElement) { diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php index 2d9cb4d3c..ccbbfc3be 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php @@ -129,6 +129,15 @@ class XmlExporter extends AbstractExporter } } + foreach ($metadata->associationMappings as $name => $assoc) { + if (isset($assoc['id']) && $assoc['id']) { + $id[$name] = array( + 'fieldName' => $name, + 'associationKey' => true + ); + } + } + if ( ! $metadata->isIdentifierComposite && $idGeneratorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { $id[$metadata->getSingleIdentifierFieldName()]['generator']['strategy'] = $idGeneratorType; } @@ -137,7 +146,10 @@ class XmlExporter extends AbstractExporter foreach ($id as $field) { $idXml = $root->addChild('id'); $idXml->addAttribute('name', $field['fieldName']); - $idXml->addAttribute('type', $field['type']); + + if (isset($field['type'])) { + $idXml->addAttribute('type', $field['type']); + } if (isset($field['columnName'])) { $idXml->addAttribute('column', $field['columnName']); diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php index ab5e5f968..45c431925 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php @@ -110,9 +110,7 @@ class YamlExporter extends AbstractExporter $ids[$metadata->getSingleIdentifierFieldName()]['generator']['strategy'] = $idGeneratorType; } - if ($ids) { - $array['fields'] = $ids; - } + $array['id'] = $ids; if ($fieldMappings) { if ( ! isset($array['fields'])) { @@ -152,6 +150,10 @@ class YamlExporter extends AbstractExporter 'cascade' => $cascade, ); + if (isset($mapping['id']) && $mapping['id'] === true) { + $array['id'][$name]['associationKey'] = true; + } + if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) { $joinColumns = $associationMapping['joinColumns']; $newJoinColumns = array(); diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php index 37e0f0ca2..060345c83 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php @@ -178,7 +178,7 @@ abstract class AbstractClassMetadataExporterTest extends \Doctrine\Tests\OrmTest */ public function testIdentifierIsExported($class) { - $this->assertEquals(ClassMetadataInfo::GENERATOR_TYPE_IDENTITY, $class->generatorType); + $this->assertEquals(ClassMetadataInfo::GENERATOR_TYPE_IDENTITY, $class->generatorType, "Generator Type wrong"); $this->assertEquals(array('id'), $class->identifier); $this->assertTrue(isset($class->fieldMappings['id']['id']) && $class->fieldMappings['id']['id'] === true);