From 760623346c9423dac76ff541d81066018ee28035 Mon Sep 17 00:00:00 2001 From: Jan Kramer Date: Tue, 5 Mar 2013 15:02:20 +0100 Subject: [PATCH 1/2] Added 'contains' comparison --- composer.json | 1 + composer.lock | 58 ++++++++++++------- .../ORM/Persisters/BasicEntityPersister.php | 19 +++--- .../ORM/Persisters/SqlValueVisitor.php | 18 +++++- .../ORM/Functional/EntityRepositoryTest.php | 16 +++++ 5 files changed, 80 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 2c00492dd..89d62f2f6 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "require": { "php": ">=5.3.2", "ext-pdo": "*", + "doctrine/collections": "~1.1", "doctrine/dbal": ">=2.4-dev,<2.5-dev", "symfony/console": "2.*" }, diff --git a/composer.lock b/composer.lock index 6b3fcb3d0..23205bf9a 100644 --- a/composer.lock +++ b/composer.lock @@ -1,5 +1,5 @@ { - "hash": "eff8840dfb1a83e6e1aef32b8031ac7c", + "hash": "acae8c9f654bd03696631d5eac8d16ad", "packages": [ { "name": "doctrine/annotations", @@ -22,7 +22,6 @@ "require-dev": { "doctrine/cache": "1.*" }, - "time": "2013-01-12 19:23:32", "type": "library", "autoload": { "psr-0": { @@ -65,7 +64,8 @@ "annotations", "docblock", "parser" - ] + ], + "time": "2013-01-12 19:23:32" }, { "name": "doctrine/cache", @@ -84,7 +84,6 @@ "require": { "php": ">=5.3.2" }, - "time": "2013-01-10 22:43:46", "type": "library", "autoload": { "psr-0": { @@ -126,27 +125,32 @@ "keywords": [ "cache", "caching" - ] + ], + "time": "2013-01-10 22:43:46" }, { "name": "doctrine/collections", - "version": "v1.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/doctrine/collections.git", - "reference": "v1.0" + "reference": "560f29c39cfcfbcd210e5d549d993a39d898b04b" }, "dist": { "type": "zip", - "url": "https://github.com/doctrine/collections/archive/v1.0.zip", - "reference": "v1.0", + "url": "https://api.github.com/repos/doctrine/collections/zipball/560f29c39cfcfbcd210e5d549d993a39d898b04b", + "reference": "560f29c39cfcfbcd210e5d549d993a39d898b04b", "shasum": "" }, "require": { "php": ">=5.3.2" }, - "time": "2013-01-12 16:36:50", "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, "autoload": { "psr-0": { "Doctrine\\Common\\Collections\\": "lib/" @@ -188,7 +192,8 @@ "array", "collections", "iterator" - ] + ], + "time": "2013-03-07 12:15:54" }, { "name": "doctrine/common", @@ -212,7 +217,6 @@ "doctrine/lexer": "1.*", "php": ">=5.3.2" }, - "time": "2013-01-29 12:48:56", "type": "library", "extra": { "branch-alias": { @@ -262,7 +266,8 @@ "eventmanager", "persistence", "spl" - ] + ], + "time": "2013-01-29 12:48:56" }, { "name": "doctrine/dbal", @@ -288,7 +293,6 @@ "suggest": { "symfony/console": "For helpful console commands such as SQL execution and import of files." }, - "time": "2013-02-09 23:28:29", "type": "library", "extra": { "branch-alias": { @@ -331,7 +335,8 @@ "dbal", "persistence", "queryobject" - ] + ], + "time": "2013-02-09 23:28:29" }, { "name": "doctrine/inflector", @@ -350,7 +355,6 @@ "require": { "php": ">=5.3.2" }, - "time": "2013-01-10 21:49:15", "type": "library", "autoload": { "psr-0": { @@ -394,7 +398,8 @@ "pluarlize", "singuarlize", "string" - ] + ], + "time": "2013-01-10 21:49:15" }, { "name": "doctrine/lexer", @@ -413,7 +418,6 @@ "require": { "php": ">=5.3.2" }, - "time": "2013-01-12 18:59:04", "type": "library", "autoload": { "psr-0": { @@ -446,7 +450,8 @@ "keywords": [ "lexer", "parser" - ] + ], + "time": "2013-01-12 18:59:04" }, { "name": "symfony/console", @@ -466,7 +471,6 @@ "require": { "php": ">=5.3.3" }, - "time": "2013-01-31 21:39:01", "type": "library", "extra": { "branch-alias": { @@ -493,15 +497,25 @@ } ], "description": "Symfony Console Component", - "homepage": "http://symfony.com" + "homepage": "http://symfony.com", + "time": "2013-01-31 21:39:01" } ], - "packages-dev": null, + "packages-dev": [ + + ], "aliases": [ ], "minimum-stability": "dev", "stability-flags": [ + ], + "platform": { + "php": ">=5.3.2", + "ext-pdo": "*" + }, + "platform-dev": [ + ] } diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index d5543dad4..649ad2e8e 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -84,15 +84,16 @@ class BasicEntityPersister * @var array */ static private $comparisonMap = array( - Comparison::EQ => '= %s', - Comparison::IS => 'IS %s', - Comparison::NEQ => '!= %s', - Comparison::GT => '> %s', - Comparison::GTE => '>= %s', - Comparison::LT => '< %s', - Comparison::LTE => '<= %s', - Comparison::IN => 'IN (%s)', - Comparison::NIN => 'NOT IN (%s)', + Comparison::EQ => '= %s', + Comparison::IS => 'IS %s', + Comparison::NEQ => '!= %s', + Comparison::GT => '> %s', + Comparison::GTE => '>= %s', + Comparison::LT => '< %s', + Comparison::LTE => '<= %s', + Comparison::IN => 'IN (%s)', + Comparison::NIN => 'NOT IN (%s)', + Comparison::CONTAINS => 'LIKE %s', ); /** diff --git a/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php b/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php index 1d805c041..79e5150e5 100644 --- a/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php +++ b/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php @@ -50,7 +50,7 @@ class SqlValueVisitor extends ExpressionVisitor */ public function walkComparison(Comparison $comparison) { - $value = $comparison->getValue()->getValue(); + $value = $this->getValueFromComparison($comparison); $field = $comparison->getField(); $this->values[] = $value; @@ -92,4 +92,20 @@ class SqlValueVisitor extends ExpressionVisitor { return array($this->values, $this->types); } + + /** + * Returns the value from a Comparison. In case of a CONTAINS comparison, + * the value is wrapped in %-signs, because it will be used in a LIKE clause. + * + * @param \Doctrine\Common\Collections\Expr\Comparison $comparison + * @return mixed + */ + protected function getValueFromComparison(Comparison $comparison) + { + $value = $comparison->getValue()->getValue(); + + return $comparison->getOperator() == Comparison::CONTAINS + ? "%{$value}%" + : $value; + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 5be4615fb..7874435bd 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -781,6 +781,22 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(4, count($users)); } + public function testMatchingCriteriaContainsComparison() + { + $this->loadFixture(); + + $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); + + $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar'))); + $this->assertEquals(0, count($users)); + + $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom'))); + $this->assertEquals(1, count($users)); + + $users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev'))); + $this->assertEquals(2, count($users)); + } + /** * @group DDC-2055 */ From 9d5e7eb6e9f8f422f3d7cb46d23f1ce50b0250d7 Mon Sep 17 00:00:00 2001 From: Jan Kramer Date: Thu, 7 Mar 2013 14:12:10 +0100 Subject: [PATCH 2/2] Added composer.lock to .gitignore --- .gitignore | 1 + composer.lock | 521 -------------------------------------------------- 2 files changed, 1 insertion(+), 521 deletions(-) delete mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index 240207aee..490413d15 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ lib/Doctrine/DBAL .project .idea vendor/ +composer.lock diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 23205bf9a..000000000 --- a/composer.lock +++ /dev/null @@ -1,521 +0,0 @@ -{ - "hash": "acae8c9f654bd03696631d5eac8d16ad", - "packages": [ - { - "name": "doctrine/annotations", - "version": "v1.0", - "source": { - "type": "git", - "url": "https://github.com/doctrine/annotations.git", - "reference": "v1.0" - }, - "dist": { - "type": "zip", - "url": "https://github.com/doctrine/annotations/archive/v1.0.zip", - "reference": "v1.0", - "shasum": "" - }, - "require": { - "doctrine/lexer": "1.*", - "php": ">=5.3.2" - }, - "require-dev": { - "doctrine/cache": "1.*" - }, - "type": "library", - "autoload": { - "psr-0": { - "Doctrine\\Common\\Annotations\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com", - "homepage": "http://www.jwage.com/" - }, - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com", - "homepage": "https://github.com/schmittjoh", - "role": "Developer of wrapped JMSSerializerBundle" - } - ], - "description": "Docblock Annotations Parser", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "annotations", - "docblock", - "parser" - ], - "time": "2013-01-12 19:23:32" - }, - { - "name": "doctrine/cache", - "version": "v1.0", - "source": { - "type": "git", - "url": "https://github.com/doctrine/cache.git", - "reference": "v1.0" - }, - "dist": { - "type": "zip", - "url": "https://github.com/doctrine/cache/archive/v1.0.zip", - "reference": "v1.0", - "shasum": "" - }, - "require": { - "php": ">=5.3.2" - }, - "type": "library", - "autoload": { - "psr-0": { - "Doctrine\\Common\\Cache\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com", - "homepage": "http://www.jwage.com/" - }, - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com", - "homepage": "https://github.com/schmittjoh", - "role": "Developer of wrapped JMSSerializerBundle" - } - ], - "description": "Caching library offering an object-oriented API for many cache backends", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "cache", - "caching" - ], - "time": "2013-01-10 22:43:46" - }, - { - "name": "doctrine/collections", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/doctrine/collections.git", - "reference": "560f29c39cfcfbcd210e5d549d993a39d898b04b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/collections/zipball/560f29c39cfcfbcd210e5d549d993a39d898b04b", - "reference": "560f29c39cfcfbcd210e5d549d993a39d898b04b", - "shasum": "" - }, - "require": { - "php": ">=5.3.2" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1.x-dev" - } - }, - "autoload": { - "psr-0": { - "Doctrine\\Common\\Collections\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com", - "homepage": "http://www.jwage.com/" - }, - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com", - "homepage": "https://github.com/schmittjoh", - "role": "Developer of wrapped JMSSerializerBundle" - } - ], - "description": "Collections Abstraction library", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "array", - "collections", - "iterator" - ], - "time": "2013-03-07 12:15:54" - }, - { - "name": "doctrine/common", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/doctrine/common", - "reference": "53859ae1c84ccf1a5aa58c8379c69cd9adedf03a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/common/zipball/53859ae1c84ccf1a5aa58c8379c69cd9adedf03a", - "reference": "53859ae1c84ccf1a5aa58c8379c69cd9adedf03a", - "shasum": "" - }, - "require": { - "doctrine/annotations": "1.*", - "doctrine/cache": "1.*", - "doctrine/collections": "1.*", - "doctrine/inflector": "1.*", - "doctrine/lexer": "1.*", - "php": ">=5.3.2" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.4.x-dev" - } - }, - "autoload": { - "psr-0": { - "Doctrine\\Common\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com", - "homepage": "http://www.jwage.com/" - }, - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com", - "homepage": "https://github.com/schmittjoh", - "role": "Developer of wrapped JMSSerializerBundle" - } - ], - "description": "Common Library for Doctrine projects", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "annotations", - "collections", - "eventmanager", - "persistence", - "spl" - ], - "time": "2013-01-29 12:48:56" - }, - { - "name": "doctrine/dbal", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/doctrine/dbal", - "reference": "eb6ee9a86421ba534d7c5514b190d1d06b30d4b1" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/dbal/zipball/eb6ee9a86421ba534d7c5514b190d1d06b30d4b1", - "reference": "eb6ee9a86421ba534d7c5514b190d1d06b30d4b1", - "shasum": "" - }, - "require": { - "doctrine/common": "2.4.x-dev", - "php": ">=5.3.2" - }, - "require-dev": { - "symfony/console": "2.*" - }, - "suggest": { - "symfony/console": "For helpful console commands such as SQL execution and import of files." - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.4.x-dev" - } - }, - "autoload": { - "psr-0": { - "Doctrine\\DBAL\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com", - "homepage": "http://www.jwage.com/" - }, - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - } - ], - "description": "Database Abstraction Layer", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "database", - "dbal", - "persistence", - "queryobject" - ], - "time": "2013-02-09 23:28:29" - }, - { - "name": "doctrine/inflector", - "version": "v1.0", - "source": { - "type": "git", - "url": "https://github.com/doctrine/inflector.git", - "reference": "v1.0" - }, - "dist": { - "type": "zip", - "url": "https://github.com/doctrine/inflector/archive/v1.0.zip", - "reference": "v1.0", - "shasum": "" - }, - "require": { - "php": ">=5.3.2" - }, - "type": "library", - "autoload": { - "psr-0": { - "Doctrine\\Common\\Inflector\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com", - "homepage": "http://www.jwage.com/" - }, - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com", - "homepage": "https://github.com/schmittjoh", - "role": "Developer of wrapped JMSSerializerBundle" - } - ], - "description": "Common String Manipulations with regard to casing and singular/plural rules.", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "inflection", - "pluarlize", - "singuarlize", - "string" - ], - "time": "2013-01-10 21:49:15" - }, - { - "name": "doctrine/lexer", - "version": "v1.0", - "source": { - "type": "git", - "url": "https://github.com/doctrine/lexer.git", - "reference": "v1.0" - }, - "dist": { - "type": "zip", - "url": "https://github.com/doctrine/lexer/archive/v1.0.zip", - "reference": "v1.0", - "shasum": "" - }, - "require": { - "php": ">=5.3.2" - }, - "type": "library", - "autoload": { - "psr-0": { - "Doctrine\\Common\\Lexer\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com", - "homepage": "http://www.instaclick.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com", - "homepage": "https://github.com/schmittjoh", - "role": "Developer of wrapped JMSSerializerBundle" - } - ], - "description": "Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.", - "homepage": "http://www.doctrine-project.org", - "keywords": [ - "lexer", - "parser" - ], - "time": "2013-01-12 18:59:04" - }, - { - "name": "symfony/console", - "version": "dev-master", - "target-dir": "Symfony/Component/Console", - "source": { - "type": "git", - "url": "https://github.com/symfony/Console", - "reference": "f65e34d058f0990a724f78e8d091dc0a20e439ac" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/f65e34d058f0990a724f78e8d091dc0a20e439ac", - "reference": "f65e34d058f0990a724f78e8d091dc0a20e439ac", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.3-dev" - } - }, - "autoload": { - "psr-0": { - "Symfony\\Component\\Console\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "http://symfony.com/contributors" - } - ], - "description": "Symfony Console Component", - "homepage": "http://symfony.com", - "time": "2013-01-31 21:39:01" - } - ], - "packages-dev": [ - - ], - "aliases": [ - - ], - "minimum-stability": "dev", - "stability-flags": [ - - ], - "platform": { - "php": ">=5.3.2", - "ext-pdo": "*" - }, - "platform-dev": [ - - ] -}