Fixes #1809: Added support for building "EXISTS" and "NOT EXISTS" query conditions

This commit is contained in:
Qiang Xue
2014-01-11 08:35:43 -05:00
parent fa85447749
commit 7d08a09620
3 changed files with 103 additions and 0 deletions

View File

@ -55,6 +55,7 @@ Yii Framework 2 Change Log
- Enh #1681: Added support for automatically adjusting the "for" attribute of label generated by `ActiveField::label()` (qiangxue)
- Enh #1706: Added support for registering a single JS/CSS file with dependency (qiangxue)
- Enh #1773: keyPrefix property of Cache is not restricted to alnum characters anymore, however it is still recommended (cebe)
- Enh #1809: Added support for building "EXISTS" and "NOT EXISTS" query conditions (abdrasulov)
- Enh #1852: ActiveRecord::tableName() now returns table name using DbConnection::tablePrefix (creocoder)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)

View File

@ -799,6 +799,8 @@ class QueryBuilder extends \yii\base\Object
'NOT LIKE' => 'buildLikeCondition',
'OR LIKE' => 'buildLikeCondition',
'OR NOT LIKE' => 'buildLikeCondition',
'EXISTS' => 'buildExistsCondition',
'NOT EXISTS' => 'buildExistsCondition',
];
if (!is_array($condition)) {
@ -1071,4 +1073,21 @@ class QueryBuilder extends \yii\base\Object
return implode($andor, $parts);
}
/**
* Creates an SQL expressions with the `EXISTS` operator.
* @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`)
* @param array $operands contains only the one element. It is sub-query
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
*/
public function buildExistsCondition($operator, $operands, &$params)
{
$subQuery = $operands[0];
list($subQuerySql, $subQueryParams) = $this->build($subQuery);
foreach ($subQueryParams as $name => $value) {
$params[$name] = $value;
}
return "$operator ($subQuerySql)";
}
}