mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-12 20:21:19 +08:00
Fixed QueryBuilder::buildCondition() to skip empty array on input
Fixes #15653
This commit is contained in:
@@ -1491,15 +1491,17 @@ class QueryBuilder extends \yii\base\BaseObject
|
||||
public function buildCondition($condition, &$params)
|
||||
{
|
||||
if (is_array($condition)) {
|
||||
if (empty($condition)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$condition = $this->createConditionFromArray($condition);
|
||||
}
|
||||
|
||||
if ($condition instanceof ExpressionInterface) {
|
||||
return $this->buildExpression($condition, $params);
|
||||
}
|
||||
if (empty($condition)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string) $condition;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,11 +28,15 @@ class ConjunctionConditionBuilder implements ExpressionBuilderInterface
|
||||
{
|
||||
$parts = $this->buildExpressionsFrom($condition, $params);
|
||||
|
||||
if (!empty($parts)) {
|
||||
return '(' . implode(") {$condition->getOperator()} (", $parts) . ')';
|
||||
if (empty($parts)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '';
|
||||
if (count($parts) === 1) {
|
||||
return reset($parts);
|
||||
}
|
||||
|
||||
return '(' . implode(") {$condition->getOperator()} (", $parts) . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2359,4 +2359,22 @@ abstract class QueryBuilderTest extends DatabaseTestCase
|
||||
$this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $this->replaceQuotes($expected)), $sql);
|
||||
$this->assertEquals($expectedParams, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/yiisoft/yii2/issues/15653
|
||||
*/
|
||||
public function testIssue15653()
|
||||
{
|
||||
$query = (new Query())
|
||||
->from('admin_user')
|
||||
->where(['is_deleted' => false]);
|
||||
|
||||
$query
|
||||
->where([])
|
||||
->andWhere(['in', 'id', ['1', '0']]);
|
||||
|
||||
list($sql, $params) = $this->getQueryBuilder()->build($query);
|
||||
$this->assertSame("SELECT * FROM `admin_user` WHERE `id` IN (:qp0, :qp1)", $sql);
|
||||
$this->assertSame([':qp0' => '1', ':qp1' => '0'], $params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user