From eeb7ff4a6d1e038a577739d91dcdeffdb21c4a11 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Wed, 7 May 2014 23:36:40 +0900 Subject: [PATCH 1/4] Support for Partial Indexes for PostgreSql and Sqlite Support for Partial Indexes was available in Doctrine 1 following http://www.doctrine-project.org/jira/browse/DC-82. This commit reintroduce support for Doctrine 2. We use the same syntax with an optionnal "where" attribute for Index and UniqueConstraint. --- docs/en/reference/annotations-reference.rst | 12 +++ doctrine-mapping.xsd | 92 ++++++++++--------- .../ORM/Mapping/Driver/AnnotationDriver.php | 10 +- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 26 +++--- .../ORM/Mapping/Driver/YamlDriver.php | 25 +++-- lib/Doctrine/ORM/Mapping/Index.php | 5 + lib/Doctrine/ORM/Mapping/UniqueConstraint.php | 5 + lib/Doctrine/ORM/Tools/SchemaTool.php | 6 +- .../ORM/Mapping/AbstractMappingDriverTest.php | 15 +-- .../Doctrine.Tests.ORM.Mapping.Comment.php | 2 +- .../php/Doctrine.Tests.ORM.Mapping.User.php | 4 +- ...Doctrine.Tests.ORM.Mapping.Comment.dcm.xml | 2 +- .../Doctrine.Tests.ORM.Mapping.User.dcm.xml | 2 +- ...Doctrine.Tests.ORM.Mapping.Comment.dcm.yml | 1 + .../Doctrine.Tests.ORM.Mapping.User.dcm.yml | 3 +- 15 files changed, 127 insertions(+), 83 deletions(-) diff --git a/docs/en/reference/annotations-reference.rst b/docs/en/reference/annotations-reference.rst index e3000ea10..f4705cea4 100644 --- a/docs/en/reference/annotations-reference.rst +++ b/docs/en/reference/annotations-reference.rst @@ -418,6 +418,12 @@ Required attributes: - **name**: Name of the Index - **columns**: Array of columns. +Optional attributes: + +- **options**: Array of platform specific options + + - **where**: SQL WHERE condition to be used for partial indexes. It will only have effect on supported platforms. + Example: .. code-block:: php @@ -1151,6 +1157,12 @@ Required attributes: - **name**: Name of the Index - **columns**: Array of columns. +Optional attributes: + +- **options**: Array of platform specific options + + - **where**: SQL WHERE condition to be used for partial indexes. It will only have effect on supported platforms. + Example: .. code-block:: php diff --git a/doctrine-mapping.xsd b/doctrine-mapping.xsd index fea44a691..2dca26257 100644 --- a/doctrine-mapping.xsd +++ b/doctrine-mapping.xsd @@ -5,9 +5,9 @@ xmlns:orm="http://doctrine-project.org/schemas/orm/doctrine-mapping" elementFormDefault="qualified"> - - + @@ -23,27 +23,27 @@ - + - + - - - - - + + + + + - + @@ -64,7 +64,7 @@ - + @@ -73,7 +73,7 @@ - + @@ -199,7 +199,7 @@ - + @@ -245,7 +245,7 @@ - + @@ -253,33 +253,33 @@ - - - + + + - - - - - + + + + + - + - - - - - + + + + + - + - - - - + + + + - + @@ -299,13 +299,13 @@ - + - + @@ -317,16 +317,17 @@ - + + - + @@ -334,7 +335,7 @@ - + @@ -342,9 +343,10 @@ + - + @@ -352,7 +354,7 @@ - + @@ -361,7 +363,7 @@ - + @@ -509,7 +511,7 @@ - + @@ -528,7 +530,7 @@ - + diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index 3f46e2c95..89d215a3a 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -102,11 +102,15 @@ class AnnotationDriver extends AbstractAnnotationDriver if ($tableAnnot->indexes !== null) { foreach ($tableAnnot->indexes as $indexAnnot) { $index = array('columns' => $indexAnnot->columns); - + if ( ! empty($indexAnnot->flags)) { $index['flags'] = $indexAnnot->flags; } + if (! empty($indexAnnot->where)) { + $index['where'] = $indexAnnot->where; + } + if ( ! empty($indexAnnot->name)) { $primaryTable['indexes'][$indexAnnot->name] = $index; } else { @@ -118,6 +122,10 @@ class AnnotationDriver extends AbstractAnnotationDriver if ($tableAnnot->uniqueConstraints !== null) { foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) { $uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns); + + if ( ! empty($uniqueConstraintAnnot->where)) { + $uniqueConstraint['where'] = $uniqueConstraintAnnot->where; + } if ( ! empty($uniqueConstraintAnnot->name)) { $primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint; diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index e08ee7ae6..a07f5b36b 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -196,11 +196,15 @@ class XmlDriver extends FileDriver $metadata->table['indexes'] = array(); foreach ($xmlRoot->indexes->index as $indexXml) { $index = array('columns' => explode(',', (string) $indexXml['columns'])); - + if (isset($indexXml['flags'])) { $index['flags'] = explode(',', (string) $indexXml['flags']); } - + + if (isset($indexXml['where'])) { + $index['where'] = $indexXml['where']; + } + if (isset($indexXml['name'])) { $metadata->table['indexes'][(string) $indexXml['name']] = $index; } else { @@ -212,17 +216,17 @@ class XmlDriver extends FileDriver // Evaluate if (isset($xmlRoot->{'unique-constraints'})) { $metadata->table['uniqueConstraints'] = array(); - foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) { - $columns = explode(',', (string)$unique['columns']); + foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $uniqueXml) { + $unique = array('columns' => explode(',', (string) $uniqueXml['columns'])); - if (isset($unique['name'])) { - $metadata->table['uniqueConstraints'][(string)$unique['name']] = array( - 'columns' => $columns - ); + if (isset($uniqueXml['where'])) { + $unique['where'] = $uniqueXml['where']; + } + + if (isset($uniqueXml['name'])) { + $metadata->table['uniqueConstraints'][(string)$uniqueXml['name']] = $unique; } else { - $metadata->table['uniqueConstraints'][] = array( - 'columns' => $columns - ); + $metadata->table['uniqueConstraints'][] = $unique; } } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index 2772913cc..6146667a0 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -220,27 +220,32 @@ class YamlDriver extends FileDriver } } + if (isset($indexYml['where'])) { + $index['where'] = $indexYml['where']; + } + $metadata->table['indexes'][$indexYml['name']] = $index; } } // Evaluate uniqueConstraints if (isset($element['uniqueConstraints'])) { - foreach ($element['uniqueConstraints'] as $name => $unique) { - if ( ! isset($unique['name'])) { - $unique['name'] = $name; + foreach ($element['uniqueConstraints'] as $name => $uniqueYml) { + if ( ! isset($uniqueYml['name'])) { + $uniqueYml['name'] = $name; } - if (is_string($unique['columns'])) { - $columns = explode(',', $unique['columns']); - $columns = array_map('trim', $columns); + if (is_string($uniqueYml['columns'])) { + $unique = array('columns' => array_map('trim', explode(',', $uniqueYml['columns']))); } else { - $columns = $unique['columns']; + $unique = array('columns' => $uniqueYml['columns']); } - $metadata->table['uniqueConstraints'][$unique['name']] = array( - 'columns' => $columns - ); + if (isset($uniqueYml['where'])) { + $unique['where'] = $uniqueYml['where']; + } + + $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique; } } diff --git a/lib/Doctrine/ORM/Mapping/Index.php b/lib/Doctrine/ORM/Mapping/Index.php index ff4532d47..378dd4d67 100644 --- a/lib/Doctrine/ORM/Mapping/Index.php +++ b/lib/Doctrine/ORM/Mapping/Index.php @@ -39,4 +39,9 @@ final class Index implements Annotation * @var array */ public $flags; + + /** + * @var string + */ + public $where; } diff --git a/lib/Doctrine/ORM/Mapping/UniqueConstraint.php b/lib/Doctrine/ORM/Mapping/UniqueConstraint.php index 95d99293f..364cb547c 100644 --- a/lib/Doctrine/ORM/Mapping/UniqueConstraint.php +++ b/lib/Doctrine/ORM/Mapping/UniqueConstraint.php @@ -34,4 +34,9 @@ final class UniqueConstraint implements Annotation * @var array */ public $columns; + + /** + * @var string + */ + public $where; } diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 9bca3ed66..8792c5257 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -267,14 +267,14 @@ class SchemaTool if( ! isset($indexData['flags'])) { $indexData['flags'] = array(); } - - $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array)$indexData['flags']); + + $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array)$indexData['flags'], isset($indexData['where']) ? $indexData['where'] : null); } } if (isset($class->table['uniqueConstraints'])) { foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) { - $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName); + $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['where']) ? $indexData['where'] : null); } } diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 7a22dc1da..704fa1804 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -73,14 +73,15 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase return $class; } - public function testEntityIndexFlags() + public function testEntityIndexFlagsAndPartialIndexes() { $class = $this->createClassMetadata('Doctrine\Tests\ORM\Mapping\Comment'); $this->assertEquals(array( 0 => array( 'columns' => array('content'), - 'flags' => array('fulltext') + 'flags' => array('fulltext'), + 'where' => 'content IS NOT NULL', ) ), $class->table['indexes']); } @@ -95,7 +96,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase 'ClassMetadata should have uniqueConstraints key in table property when Unique Constraints are set.'); $this->assertEquals(array( - "search_idx" => array("columns" => array("name", "user_email")) + "search_idx" => array("columns" => array("name", "user_email"), 'where' => 'name IS NOT NULL') ), $class->table['uniqueConstraints']); return $class; @@ -938,7 +939,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase * @HasLifecycleCallbacks * @Table( * name="cms_users", - * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})}, + * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"}, where="name IS NOT NULL")}, * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})}, * options={"foo": "bar", "baz": {"key": "val"}} * ) @@ -1122,7 +1123,7 @@ class User 'orderBy' => NULL, )); $metadata->table['uniqueConstraints'] = array( - 'search_idx' => array('columns' => array('name', 'user_email')), + 'search_idx' => array('columns' => array('name', 'user_email'), 'where' => 'name IS NOT NULL'), ); $metadata->table['indexes'] = array( 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) @@ -1281,7 +1282,7 @@ class Group {} /** * @Entity - * @Table(indexes={@Index(columns={"content"}, flags={"fulltext"})}) + * @Table(indexes={@Index(columns={"content"}, flags={"fulltext"}, where="content IS NOT NULL")}) */ class Comment { @@ -1295,7 +1296,7 @@ class Comment $metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE); $metadata->setPrimaryTable(array( 'indexes' => array( - array('columns' => array('content'), 'flags' => array('fulltext')) + array('columns' => array('content'), 'flags' => array('fulltext'), 'where' => 'content IS NOT NULL') ) )); diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php index 124aafe1d..e5ac0f8b0 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php @@ -5,7 +5,7 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo; $metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE); $metadata->setPrimaryTable(array( 'indexes' => array( - array('columns' => array('content'), 'flags' => array('fulltext')) + array('columns' => array('content'), 'flags' => array('fulltext'), 'where' => 'content IS NOT NULL') ) )); 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 1795793d3..2e32ee229 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 @@ -112,11 +112,11 @@ $metadata->mapManyToMany(array( 'orderBy' => NULL, )); $metadata->table['options'] = array( - 'foo' => 'bar', + 'foo' => 'bar', 'baz' => array('key' => 'val') ); $metadata->table['uniqueConstraints'] = array( - 'search_idx' => array('columns' => array('name', 'user_email')), + 'search_idx' => array('columns' => array('name', 'user_email'), 'where' => 'name IS NOT NULL'), ); $metadata->table['indexes'] = array( 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml index 659ddccd8..5c061a94a 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml @@ -8,7 +8,7 @@ - + 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 728425a71..c61962539 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 @@ -19,7 +19,7 @@ - + diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml index 2186f6a27..e9e46dece 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml @@ -7,3 +7,4 @@ Doctrine\Tests\ORM\Mapping\Comment: 0: columns: content flags: fulltext + where: "content IS NOT NULL" 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 6b87472d1..db93190b6 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 @@ -69,11 +69,12 @@ Doctrine\Tests\ORM\Mapping\User: cascade: - all lifecycleCallbacks: - prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ] + prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ] postPersist: [ doStuffOnPostPersist ] uniqueConstraints: search_idx: columns: name,user_email + where: name IS NOT NULL indexes: name_idx: columns: name From 27adf8d6e9723c86e3725de18b5e1a955996cc11 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Mon, 21 Jul 2014 17:42:49 +0900 Subject: [PATCH 2/4] Refactor partial into options array This coherent with what is done for Table. All platform specific things are grouped into an options array. Eventually flags should be migrated into options as well. --- doctrine-mapping.xsd | 4 ++-- lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php | 10 +++++----- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 9 +++++---- lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php | 8 ++++---- lib/Doctrine/ORM/Mapping/Index.php | 4 ++-- lib/Doctrine/ORM/Mapping/UniqueConstraint.php | 4 ++-- lib/Doctrine/ORM/Tools/SchemaTool.php | 4 ++-- .../Tests/ORM/Mapping/AbstractMappingDriverTest.php | 12 ++++++------ .../php/Doctrine.Tests.ORM.Mapping.Comment.php | 2 +- .../Mapping/php/Doctrine.Tests.ORM.Mapping.User.php | 2 +- .../xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml | 6 +++++- .../xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml | 6 +++++- .../yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml | 3 ++- .../yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml | 3 ++- 14 files changed, 44 insertions(+), 33 deletions(-) diff --git a/doctrine-mapping.xsd b/doctrine-mapping.xsd index 2dca26257..a6440a2e8 100644 --- a/doctrine-mapping.xsd +++ b/doctrine-mapping.xsd @@ -320,11 +320,11 @@ + - @@ -338,12 +338,12 @@ + - diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index 89d215a3a..d68204058 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -107,8 +107,8 @@ class AnnotationDriver extends AbstractAnnotationDriver $index['flags'] = $indexAnnot->flags; } - if (! empty($indexAnnot->where)) { - $index['where'] = $indexAnnot->where; + if (! empty($indexAnnot->options)) { + $index['options'] = $indexAnnot->options; } if ( ! empty($indexAnnot->name)) { @@ -122,9 +122,9 @@ class AnnotationDriver extends AbstractAnnotationDriver if ($tableAnnot->uniqueConstraints !== null) { foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) { $uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns); - - if ( ! empty($uniqueConstraintAnnot->where)) { - $uniqueConstraint['where'] = $uniqueConstraintAnnot->where; + + if ( ! empty($uniqueConstraintAnnot->options)) { + $uniqueConstraint['options'] = $uniqueConstraintAnnot->options; } if ( ! empty($uniqueConstraintAnnot->name)) { diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index a07f5b36b..45836a6b9 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -201,8 +201,8 @@ class XmlDriver extends FileDriver $index['flags'] = explode(',', (string) $indexXml['flags']); } - if (isset($indexXml['where'])) { - $index['where'] = $indexXml['where']; + if (isset($indexXml->options)) { + $index['options'] = $this->_parseOptions($indexXml->options->children()); } if (isset($indexXml['name'])) { @@ -219,8 +219,9 @@ class XmlDriver extends FileDriver foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $uniqueXml) { $unique = array('columns' => explode(',', (string) $uniqueXml['columns'])); - if (isset($uniqueXml['where'])) { - $unique['where'] = $uniqueXml['where']; + + if (isset($uniqueXml->options)) { + $unique['options'] = $this->_parseOptions($uniqueXml->options->children()); } if (isset($uniqueXml['name'])) { diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index 6146667a0..c492c0f54 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -220,8 +220,8 @@ class YamlDriver extends FileDriver } } - if (isset($indexYml['where'])) { - $index['where'] = $indexYml['where']; + if (isset($indexYml['options'])) { + $index['options'] = $indexYml['options']; } $metadata->table['indexes'][$indexYml['name']] = $index; @@ -241,8 +241,8 @@ class YamlDriver extends FileDriver $unique = array('columns' => $uniqueYml['columns']); } - if (isset($uniqueYml['where'])) { - $unique['where'] = $uniqueYml['where']; + if (isset($uniqueYml['options'])) { + $unique['options'] = $uniqueYml['options']; } $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique; diff --git a/lib/Doctrine/ORM/Mapping/Index.php b/lib/Doctrine/ORM/Mapping/Index.php index 378dd4d67..45953a804 100644 --- a/lib/Doctrine/ORM/Mapping/Index.php +++ b/lib/Doctrine/ORM/Mapping/Index.php @@ -41,7 +41,7 @@ final class Index implements Annotation public $flags; /** - * @var string + * @var array */ - public $where; + public $options; } diff --git a/lib/Doctrine/ORM/Mapping/UniqueConstraint.php b/lib/Doctrine/ORM/Mapping/UniqueConstraint.php index 364cb547c..f117d1873 100644 --- a/lib/Doctrine/ORM/Mapping/UniqueConstraint.php +++ b/lib/Doctrine/ORM/Mapping/UniqueConstraint.php @@ -36,7 +36,7 @@ final class UniqueConstraint implements Annotation public $columns; /** - * @var string + * @var array */ - public $where; + public $options; } diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 8792c5257..db9438c9f 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -268,13 +268,13 @@ class SchemaTool $indexData['flags'] = array(); } - $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array)$indexData['flags'], isset($indexData['where']) ? $indexData['where'] : null); + $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array)$indexData['flags'], isset($indexData['options']) ? $indexData['options'] : array()); } } if (isset($class->table['uniqueConstraints'])) { foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) { - $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['where']) ? $indexData['where'] : null); + $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['options']) ? $indexData['options'] : array()); } } diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 704fa1804..b81b3ba1e 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -81,7 +81,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase 0 => array( 'columns' => array('content'), 'flags' => array('fulltext'), - 'where' => 'content IS NOT NULL', + 'options' => array('where' => 'content IS NOT NULL'), ) ), $class->table['indexes']); } @@ -96,7 +96,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase 'ClassMetadata should have uniqueConstraints key in table property when Unique Constraints are set.'); $this->assertEquals(array( - "search_idx" => array("columns" => array("name", "user_email"), 'where' => 'name IS NOT NULL') + "search_idx" => array("columns" => array("name", "user_email"), 'options' => array('where' => 'name IS NOT NULL')) ), $class->table['uniqueConstraints']); return $class; @@ -939,7 +939,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase * @HasLifecycleCallbacks * @Table( * name="cms_users", - * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"}, where="name IS NOT NULL")}, + * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"}, options={"where": "name IS NOT NULL"})}, * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})}, * options={"foo": "bar", "baz": {"key": "val"}} * ) @@ -1123,7 +1123,7 @@ class User 'orderBy' => NULL, )); $metadata->table['uniqueConstraints'] = array( - 'search_idx' => array('columns' => array('name', 'user_email'), 'where' => 'name IS NOT NULL'), + 'search_idx' => array('columns' => array('name', 'user_email'), 'options'=> array('where' => 'name IS NOT NULL')), ); $metadata->table['indexes'] = array( 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) @@ -1282,7 +1282,7 @@ class Group {} /** * @Entity - * @Table(indexes={@Index(columns={"content"}, flags={"fulltext"}, where="content IS NOT NULL")}) + * @Table(indexes={@Index(columns={"content"}, flags={"fulltext"}, options={"where": "content IS NOT NULL"})}) */ class Comment { @@ -1296,7 +1296,7 @@ class Comment $metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE); $metadata->setPrimaryTable(array( 'indexes' => array( - array('columns' => array('content'), 'flags' => array('fulltext'), 'where' => 'content IS NOT NULL') + array('columns' => array('content'), 'flags' => array('fulltext'), 'options' => array('where' => 'content IS NOT NULL')) ) )); diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php index e5ac0f8b0..85cbed4dc 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.Comment.php @@ -5,7 +5,7 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo; $metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE); $metadata->setPrimaryTable(array( 'indexes' => array( - array('columns' => array('content'), 'flags' => array('fulltext'), 'where' => 'content IS NOT NULL') + array('columns' => array('content'), 'flags' => array('fulltext'), 'options'=> array('where' => 'content IS NOT NULL')) ) )); 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 2e32ee229..67dbd752d 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 @@ -116,7 +116,7 @@ $metadata->table['options'] = array( 'baz' => array('key' => 'val') ); $metadata->table['uniqueConstraints'] = array( - 'search_idx' => array('columns' => array('name', 'user_email'), 'where' => 'name IS NOT NULL'), + 'search_idx' => array('columns' => array('name', 'user_email'), 'options' => array('where' => 'name IS NOT NULL')), ); $metadata->table['indexes'] = array( 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml index 5c061a94a..8f02ca852 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.Comment.dcm.xml @@ -8,7 +8,11 @@ - + + + + + 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 c61962539..28b1e0571 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 @@ -19,7 +19,11 @@ - + + + + + diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml index e9e46dece..f37bfdcc6 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.Comment.dcm.yml @@ -7,4 +7,5 @@ Doctrine\Tests\ORM\Mapping\Comment: 0: columns: content flags: fulltext - where: "content IS NOT NULL" + options: + where: "content IS NOT NULL" 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 db93190b6..457b24eea 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 @@ -74,7 +74,8 @@ Doctrine\Tests\ORM\Mapping\User: uniqueConstraints: search_idx: columns: name,user_email - where: name IS NOT NULL + options: + where: name IS NOT NULL indexes: name_idx: columns: name From bb5345b33036cd9f9d4705657f28bb80dc4a474b Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Tue, 22 Jul 2014 10:44:45 +0900 Subject: [PATCH 3/4] Adapt formatting to pre-existing content --- docs/en/reference/annotations-reference.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/en/reference/annotations-reference.rst b/docs/en/reference/annotations-reference.rst index f4705cea4..2b61584f0 100644 --- a/docs/en/reference/annotations-reference.rst +++ b/docs/en/reference/annotations-reference.rst @@ -420,9 +420,10 @@ Required attributes: Optional attributes: -- **options**: Array of platform specific options +- **options**: Array of platform specific options: - - **where**: SQL WHERE condition to be used for partial indexes. It will only have effect on supported platforms. + - ``where``: SQL WHERE condition to be used for partial indexes. It will + only have effect on supported platforms. Example: @@ -1159,9 +1160,10 @@ Required attributes: Optional attributes: -- **options**: Array of platform specific options +- **options**: Array of platform specific options: - - **where**: SQL WHERE condition to be used for partial indexes. It will only have effect on supported platforms. + - ``where``: SQL WHERE condition to be used for partial indexes. It will + only have effect on supported platforms. Example: From 1003466a5f801759603a793b82aa79f2f352bd31 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Tue, 5 Aug 2014 18:07:00 +0900 Subject: [PATCH 4/4] Surround ! with spaces according to code style --- lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index d68204058..85f40b2f5 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -107,7 +107,7 @@ class AnnotationDriver extends AbstractAnnotationDriver $index['flags'] = $indexAnnot->flags; } - if (! empty($indexAnnot->options)) { + if ( ! empty($indexAnnot->options)) { $index['options'] = $indexAnnot->options; }