zoukankan      html  css  js  c++  java
  • 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',//小于等于
  • 相关阅读:
    linux安装教程
    html学习
    vscode编程nodejs初始安装
    Python 字符串前面加u,r,b的含义
    vs code中自动添加注释插件koroFileHeader
    *args和**kwargs用法
    python中map函数的用法
    Python中虚拟环境venv的基本用法
    ubuntu下安装git提示无root权限
    git连接gitee笔记
  • 原文地址:https://www.cnblogs.com/whatstone/p/12699174.html
Copyright © 2011-2022 走看看