Yii2 mongodb 扩展的where的条件加入大于小于

1. mongodb的where中有比较丰富的 条件,如下:

 static $builders = [
  'NOT' => 'buildNotCondition',
  'AND' => 'buildAndCondition',
  'OR' => 'buildOrCondition',
  'BETWEEN' => 'buildBetweenCondition',
  'NOT BETWEEN' => 'buildBetweenCondition',
  'IN' => 'buildInCondition',
  'NOT IN' => 'buildInCondition',
  'REGEX' => 'buildRegexCondition',
  'LIKE' => 'buildLikeCondition',

];

 

 


但是没有大于和小于等,因此我们需要加入大于,大于等于, 小于,小于等于

 

加入函数部分为:

在vendor/yiisoft/yii2-mongodb/src/QueryBuilder.php

加入函数:

public function buildGtCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
return [$column => ['$gt' => $value]];
}


public function buildGteCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
return [$column => ['$gte' => $value]];
}


public function buildLtCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
return [$column => ['$lt' => $value]];
}

public function buildLteCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
return [$column => ['$lte' => $value]];
}
 

buildCondition函数里面加入下面的部分代码

public function buildCondition($condition)
{
static $builders = [
'NOT' => 'buildNotCondition',
'AND' => 'buildAndCondition',
'OR' => 'buildOrCondition',
'BETWEEN' => 'buildBetweenCondition',
'NOT BETWEEN' => 'buildBetweenCondition',
'IN' => 'buildInCondition',
'NOT IN' => 'buildInCondition',
'REGEX' => 'buildRegexCondition',
'LIKE' => 'buildLikeCondition',
'GT' => 'buildGtCondition',//大于
'GTE' => 'buildGteCondition',//大于等于
'LT' => 'buildLtCondition',//小于
'LTE' => 'buildLteCondition',//小于等于

 

posted @ 2020-04-14 17:01  星星下的石头  阅读(1306)  评论(0编辑  收藏  举报