Skip to content

Commit f738a47

Browse files
committed
Merge pull request #1 from rbergDrox/1.0
Fix Parameters with IN condition
2 parents 0b8d5a6 + 1706a23 commit f738a47

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/SQLParser/Node/Parameter.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,25 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
155155
{
156156
if (isset($parameters[$this->name])) {
157157
if ($dbConnection) {
158-
return $dbConnection->quote($this->autoPrepend.$parameters[$this->name].$this->autoAppend);
158+
if(is_array($parameters[$this->name])){
159+
return '('.implode(',',array_map(function($item) use ($dbConnection) {
160+
return $dbConnection->quote($this->autoPrepend.$item.$this->autoAppend);
161+
}, $parameters[$this->name])).')';
162+
} else{
163+
return $dbConnection->quote($this->autoPrepend.$parameters[$this->name].$this->autoAppend);
164+
}
159165
} else {
160166
if ($parameters[$this->name] === null) {
161-
return null;
167+
return "null";
162168
} else {
163-
return "'".addslashes($this->autoPrepend.$parameters[$this->name].$this->autoAppend)."'";
169+
if(is_array($parameters[$this->name])){
170+
return '('.implode(',',array_map(function($item) {
171+
return "'".addslashes($this->autoPrepend.$item.$this->autoAppend)."'";
172+
}, $parameters[$this->name])).')';
173+
} else{
174+
return "'".addslashes($this->autoPrepend.$parameters[$this->name].$this->autoAppend)."'";
175+
}
176+
164177
}
165178
}
166179
} else {

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function testStandardSelect()
1818
$sql = 'SELECT * FROM users WHERE name LIKE :name AND company LIKE :company';
1919
$this->assertEquals("SELECT * FROM users WHERE (name LIKE 'foo')", self::simplifySql($magicQuery->build($sql, ['name' => 'foo'])));
2020
$this->assertEquals("SELECT * FROM users WHERE (name LIKE 'foo') AND (company LIKE 'bar')", self::simplifySql($magicQuery->build($sql, ['name' => 'foo', 'company' => 'bar'])));
21+
22+
$sql = 'SELECT * FROM users WHERE status in :status';
23+
$this->assertEquals("SELECT * FROM users WHERE status IN ('2','4')", self::simplifySql($magicQuery->build($sql, ['status' => [2,4]])));
2124
}
2225

2326
/**

0 commit comments

Comments
 (0)