Skip to content

Commit fad0fce

Browse files
committed
- NULL-Values used in conjunction with Select::where and Select::having when conditions are used as a key-value-array are now projected with ISNULL instead of field=NULL
1 parent ba561c4 commit fad0fce

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Builder/Internal/ConditionBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ public static function build(Database $db, $query, array $conditions, $token) {
2121
list($expression, $arguments) = $condition;
2222
if(is_array($expression)) {
2323
foreach($expression as $key => $value) {
24-
$arr = self::buildCondition($arr, "`{$key}`=?", [$value], $db);
24+
if($value === null) {
25+
$arr = self::buildCondition($arr, "ISNULL(`{$key}`)", [$value], $db);
26+
} else {
27+
$arr = self::buildCondition($arr, "`{$key}`=?", [$value], $db);
28+
}
2529
}
2630
} else {
2731
$arr = self::buildCondition($arr, $expression, $arguments, $db);

tests/Builder/SelectTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public function testWhereAsArray() {
8585
$str = TestSelect::create()
8686
->field('a')
8787
->from('t', 'test')
88-
->where(['field1' => 1, 'field2' => 'aaa'])
88+
->where(['field1' => 1, 'field2' => 'aaa', 'field3' => null])
8989
->asString();
90-
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $str);
90+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n\tAND\n\t(ISNULL(`field3`))\n", $str);
9191
}
9292

9393
public function testHaving() {
@@ -101,9 +101,9 @@ public function testHaving() {
101101
$str = TestSelect::create()
102102
->field('a')
103103
->from('t', 'test')
104-
->having(['field1' => 1, 'field2' => 'aaa'])
104+
->having(['field1' => 1, 'field2' => 'aaa', 'field3' => null])
105105
->asString();
106-
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nHAVING\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $str);
106+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nHAVING\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n\tAND\n\t(ISNULL(`field3`))\n", $str);
107107
}
108108

109109
public function testDBExpr() {

0 commit comments

Comments
 (0)