Skip to content

Loading…

Add 'contains' comparison #601

Merged
merged 2 commits into from

5 participants

@jankramer

As mentioned by @stof in doctrine/collections#5, the 'contains' comparison for matching collections should also be added to the ORM.

The first commit is only present in this PR because the doctrine/collections 1.0 release does not contain the recent addition. It should be removed/altered after a new version has been tagged, I think?

@doctrinebot

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DDC-2333

@stof stof commented on an outdated diff
composer.json
@@ -16,7 +16,8 @@
"php": ">=5.3.2",
"ext-pdo": "*",
"doctrine/dbal": ">=2.4-dev,<2.5-dev",
- "symfony/console": "2.*"
+ "symfony/console": "2.*",
+ "doctrine/collections": "dev-master as 1.1"
@stof Doctrine member
stof added a note

Requiring 1.1 does not make sense here as 1.0 has not been released yet for collections

As there was a v1.0 tag on the collections repository, I thought 1.0 was released. However, if this should still be changed to something else, I'm happy to do so.

@stof Doctrine member
stof added a note

ah, I missed the tag. But then, you should send a PR to add the branch alias in the library and simply require ~1.1 here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jankramer

Added doctrine/collections ~1.1 dependency. Unless there are other comments, this should be good to go.

@Ocramius
Doctrine member

@beberlei do you think we can put the composer.lock in .gitignore? Otherwise it will land in every next PR (and we really don't have a use for it here) /cc @stof

@stof
Doctrine member

I think it could indeed be ignored. Thus, it will allow the ORM to be tested with the current version of DBAL and Common without having to update the lock file each time.

@Ocramius
Doctrine member

@jankramer can you add it to the PR?

@Ocramius
Doctrine member

:+1:

@beberlei beberlei merged commit ba47051 into doctrine:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 7, 2013
  1. @jankramer
  2. @jankramer
View
1 .gitignore
@@ -11,3 +11,4 @@ lib/Doctrine/DBAL
.project
.idea
vendor/
+composer.lock
View
1 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.*"
},
View
507 composer.lock
@@ -1,507 +0,0 @@
-{
- "hash": "eff8840dfb1a83e6e1aef32b8031ac7c",
- "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.*"
- },
- "time": "2013-01-12 19:23:32",
- "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"
- ]
- },
- {
- "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"
- },
- "time": "2013-01-10 22:43:46",
- "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"
- ]
- },
- {
- "name": "doctrine/collections",
- "version": "v1.0",
- "source": {
- "type": "git",
- "url": "https://github.com/doctrine/collections.git",
- "reference": "v1.0"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/doctrine/collections/archive/v1.0.zip",
- "reference": "v1.0",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.2"
- },
- "time": "2013-01-12 16:36:50",
- "type": "library",
- "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"
- ]
- },
- {
- "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"
- },
- "time": "2013-01-29 12:48:56",
- "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"
- ]
- },
- {
- "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."
- },
- "time": "2013-02-09 23:28:29",
- "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"
- ]
- },
- {
- "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"
- },
- "time": "2013-01-10 21:49:15",
- "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"
- ]
- },
- {
- "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"
- },
- "time": "2013-01-12 18:59:04",
- "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"
- ]
- },
- {
- "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"
- },
- "time": "2013-01-31 21:39:01",
- "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"
- }
- ],
- "packages-dev": null,
- "aliases": [
-
- ],
- "minimum-stability": "dev",
- "stability-flags": [
-
- ]
-}
View
19 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',
);
/**
View
18 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 @@ public function getParamsAndTypes()
{
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;
+ }
}
View
16 tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php
@@ -781,6 +781,22 @@ public function testMatchingCriteriaGteComparison()
$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
*/
Something went wrong with that request. Please try again.