Skip to content

Commit

Permalink
fix where with operator null and value !== null
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Feb 10, 2017
1 parent fc6eb43 commit a4ee039
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Exceptions/NoOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ORM\Exceptions;

use ORM\Exception;

class NoOperator extends Exception
{
}
11 changes: 8 additions & 3 deletions src/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ORM\QueryBuilder;

use ORM\EntityManager;
use ORM\Exception;
use ORM\Exceptions\NoOperator;

/**
* Build a ansi sql query / select statement
Expand Down Expand Up @@ -137,8 +139,7 @@ public function getEntityManager()
* @param string $operator Operator or value if operator is omited
* @param string $value Value or array of values
* @return string
* @throws \ORM\Exceptions\NoConnection
* @throws \ORM\Exceptions\NotScalar
* @throws NoOperator
* @internal
*/
public function createWhereCondition($column, $operator = null, $value = null)
Expand All @@ -149,8 +150,12 @@ public function createWhereCondition($column, $operator = null, $value = null)
} elseif ($operator === null && $value === null) {
$expression = $column;
} else {
if (!$value) {
if ($value === null) {
$value = $operator;
$operator = null;
}

if ($operator === null) {
if (is_array($value)) {
$operator = 'IN';
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/QueryBuilder/WhereConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,13 @@ public function testCreatesWhereEmpty()

self::assertSame('SELECT * FROM foobar WHERE a = \'\'', $query->getQuery());
}

public function testUsesEqualityOperator()
{
$query = new QueryBuilder('foobar');

$query->where('a', null, '');

self::assertSame('SELECT * FROM foobar WHERE a = \'\'', $query->getQuery());
}
}

0 comments on commit a4ee039

Please sign in to comment.