From 7018509126ce9358464e3bdea2111aa0290af7e8 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 13 Jun 2010 22:59:56 +0200 Subject: [PATCH] Fix a bug with Table Indexes on XML Driver. Added tests for setting indexes on tables for all the mapping drivers --- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 20 +++++++++------- .../ORM/Mapping/AbstractMappingDriverTest.php | 24 ++++++++++++++++++- .../php/Doctrine.Tests.ORM.Mapping.User.php | 3 +++ .../Doctrine.Tests.ORM.Mapping.User.dcm.xml | 5 ++++ .../Doctrine.Tests.ORM.Mapping.User.dcm.yml | 7 +++++- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index b038973a5..3d90cd122 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -106,21 +106,25 @@ class XmlDriver extends AbstractFileDriver // Evaluate if (isset($xmlRoot->indexes)) { + $metadata->table['indexes'] = array(); foreach ($xmlRoot->indexes->index as $index) { - if (is_string($index['columns'])) { - $columns = explode(',', $index['columns']); - } else { - $columns = $index['columns']; - } + $columns = explode(',', (string)$index['columns']); - $metadata->table['indexes'][$index['name']] = array( - 'columns' => $columns - ); + if (isset($index['name'])) { + $metadata->table['indexes'][(string)$index['name']] = array( + 'columns' => $columns + ); + } else { + $metadata->table['indexes'][] = array( + 'columns' => $columns + ); + } } } // Evaluate if (isset($xmlRoot->{'unique-constraints'})) { + $metadata->table['uniqueConstraints'] = array(); foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) { $columns = explode(',', (string)$unique['columns']); diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 270e16234..858a00eba 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -36,6 +36,21 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase return $class; } + /** + * @depends testEntityTableNameAndInheritance + * @param ClassMetadata $class + */ + public function testEntityIndexes($class) + { + $this->assertArrayHasKey('indexes', $class->table, 'ClassMetadata should have indexes key in table property.'); + $this->assertEquals(array( + 'name_idx' => array('columns' => array('name')), + 0 => array('columns' => array('user_email')) + ), $class->table['indexes']); + + return $class; + } + /** * @depends testEntityTableNameAndInheritance * @param ClassMetadata $class @@ -240,7 +255,11 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase /** * @Entity * @HasLifecycleCallbacks - * @Table(name="cms_users", uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})}) + * @Table( + * name="cms_users", + * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})}, + * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})} + * ) */ class User { @@ -411,6 +430,9 @@ class User $metadata->table['uniqueConstraints'] = array( 'search_idx' => array('columns' => array('name', 'user_email')), ); + $metadata->table['indexes'] = array( + 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) + ); $metadata->setSequenceGeneratorDefinition(array( 'sequenceName' => 'tablename_seq', 'allocationSize' => 100, diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php index 819a01109..4aadffb30 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php @@ -106,6 +106,9 @@ $metadata->mapManyToMany(array( $metadata->table['uniqueConstraints'] = array( 'search_idx' => array('columns' => array('name', 'user_email')), ); +$metadata->table['indexes'] = array( + 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) +); $metadata->setSequenceGeneratorDefinition(array( 'sequenceName' => 'tablename_seq', 'allocationSize' => 100, diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml index be905e470..948430c24 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml @@ -7,6 +7,11 @@ + + + + + diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml index 7dd6bfaed..b541c8877 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml @@ -58,4 +58,9 @@ Doctrine\Tests\ORM\Mapping\User: postPersist: [ doStuffOnPostPersist ] uniqueConstraints: search_idx: - columns: name,user_email \ No newline at end of file + columns: name,user_email + indexes: + name_idx: + columns: name + 0: + columns: user_email \ No newline at end of file