Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'asm89/ddc-2417'

* asm89/ddc-2417:
  [DDC-2471] Fix EQ/NEQ null handling of criteria
  • Loading branch information...
commit e5de0dad7ef502037a6f45eded0e02e8b5ebdf47 2 parents 6548947 + 66a842c
@asm89 asm89 authored
View
10 lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
@@ -85,7 +85,7 @@ class BasicEntityPersister
*/
static private $comparisonMap = array(
Comparison::EQ => '= %s',
- Comparison::IS => 'IS %s',
+ Comparison::IS => '= %s',
Comparison::NEQ => '!= %s',
Comparison::GT => '> %s',
Comparison::GTE => '>= %s',
@@ -1608,6 +1608,14 @@ public function getSelectConditionStatementSQL($field, $value, $assoc = null, $c
}
if ($comparison !== null) {
+
+ // special case null value handling
+ if (($comparison === Comparison::EQ || $comparison === Comparison::IS) && $value === null) {
+ return $condition . ' IS NULL';
+ } else if ($comparison === Comparison::NEQ && $value === null) {
+ return $condition . ' IS NOT NULL';
+ }
+
return $condition . ' ' . sprintf(self::$comparisonMap[$comparison], $placeholder);
}
View
9 lib/Doctrine/ORM/Persisters/SqlValueVisitor.php
@@ -52,7 +52,14 @@ public function walkComparison(Comparison $comparison)
{
$value = $this->getValueFromComparison($comparison);
$field = $comparison->getField();
-
+ $operator = $comparison->getOperator();
+
+ if (($operator === Comparison::EQ || $operator === Comparison::IS) && $value === null) {
+ return;
+ } else if ($operator === Comparison::NEQ && $value === null) {
+ return;
+ }
+
$this->values[] = $value;
$this->types[] = array($field, $value);
}
View
56 tests/Doctrine/Tests/ORM/Functional/EntityRepositoryCriteriaTest.php
@@ -84,4 +84,60 @@ public function testLteDateComparison()
$this->assertEquals(2, count($dates));
}
+
+ private function loadNullFieldFixtures()
+ {
+ $today = new DateTimeModel();
+ $today->datetime =
+ $today->date =
+ new \DateTime('today');
+
+ $this->_em->persist($today);
+
+ $tomorrow = new DateTimeModel();
+ $tomorrow->datetime =
+ $tomorrow->date =
+ $tomorrow->time =
+ new \DateTime('tomorrow');
+ $this->_em->persist($tomorrow);
+
+ $this->_em->flush();
+ $this->_em->clear();
+ }
+
+ public function testIsNullComparison()
+ {
+ $this->loadNullFieldFixtures();
+ $repository = $this->_em->getRepository('Doctrine\Tests\Models\Generic\DateTimeModel');
+
+ $dates = $repository->matching(new Criteria(
+ Criteria::expr()->isNull('time')
+ ));
+
+ $this->assertEquals(1, count($dates));
+ }
+
+ public function testEqNullComparison()
+ {
+ $this->loadNullFieldFixtures();
+ $repository = $this->_em->getRepository('Doctrine\Tests\Models\Generic\DateTimeModel');
+
+ $dates = $repository->matching(new Criteria(
+ Criteria::expr()->eq('time', null)
+ ));
+
+ $this->assertEquals(1, count($dates));
+ }
+
+ public function testNotEqNullComparison()
+ {
+ $this->loadNullFieldFixtures();
+ $repository = $this->_em->getRepository('Doctrine\Tests\Models\Generic\DateTimeModel');
+
+ $dates = $repository->matching(new Criteria(
+ Criteria::expr()->neq('time', null)
+ ));
+
+ $this->assertEquals(1, count($dates));
+ }
}
View
14 tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php
@@ -96,6 +96,18 @@ public function testStripNonAlphanumericCharactersFromSelectColumnListSQL()
public function testSelectConditionStatementIsNull()
{
$statement = $this->_persister->getSelectConditionStatementSQL('test', null, array(), Comparison::IS);
- $this->assertEquals('test IS ?', $statement);
+ $this->assertEquals('test IS NULL', $statement);
+ }
+
+ public function testSelectConditionStatementEqNull()
+ {
+ $statement = $this->_persister->getSelectConditionStatementSQL('test', null, array(), Comparison::EQ);
+ $this->assertEquals('test IS NULL', $statement);
+ }
+
+ public function testSelectConditionStatementNeqNull()
+ {
+ $statement = $this->_persister->getSelectConditionStatementSQL('test', null, array(), Comparison::NEQ);
+ $this->assertEquals('test IS NOT NULL', $statement);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.