swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 786 forks source link

数据库模型查询条件只支持and不支持or,看下能否做如下修改 #315

Closed weika520 closed 6 years ago

weika520 commented 6 years ago

现在用模型查询

TbAdmin::findOne([['admin_id','>','143'],['username','=','719']], ['fields' => ['admin_id', 'username']])->getResult();

生产的SQL:

SELECT `admin_id`, `username` FROM `tb_admin` WHERE `admin_id` > '143' AND  `username` = '719' LIMIT 0,1

这里的多条件只能是AND链接

把vendor\swoft\db\src\QueryBuilder.php(andCondition)这个方法改变下支持传递查询条件关系(and、or),最后一个参数传递

TbAdmin::findOne([['admin_id','>','143'],['username','=','719','or']], ['fields' => ['admin_id', 'username']])->getResult();

生产SQL:

SELECT `admin_id`, `username` FROM `tb_admin` WHERE `admin_id` > '143' OR  `username` = '719' LIMIT 0,1

改变如下:

 /**
     * @param array $condition
     */
    public function andCondition(array $condition)
    {
        list(, $operator) = $condition;
        $operator = strtoupper($operator);
        if(isset($condition[4]) && in_array($operator,[self::BETWEEN,self::NOT_BETWEEN]) &&  in_array(strtoupper($condition[4]),['AND','OR'])){
            $connector = strtoupper($condition[4]);
        }elseif(isset($condition[3]) && !in_array($operator,[self::BETWEEN,self::NOT_BETWEEN]) &&  in_array(strtoupper($condition[3]),['AND','OR'])){
            $connector = strtoupper($condition[3]);
        }else{
            $connector = 'AND';
        }
        switch ($operator) {
            case self::OPERATOR_EQ:
            case self::OPERATOR_GT:
            case self::OPERATOR_NE:
            case self::OPERATOR_LT:
            case self::OPERATOR_LTE:
            case self::OPERATOR_GTE:
            case self::LIKE:
            case self::NOT_LIKE:
                list($column, $operator, $value) = $condition;
                $this->where($column, $value, $operator, $connector);
                break;
            case self::IN:
                list($column, , $value) = $condition;
                $this->whereIn($column, $value, $connector);
                break;
            case self::NOT_IN:
                list($column, , $value) = $condition;
                $this->whereNotIn($column, $value, $connector);
                break;
            case self::BETWEEN:
                list($column, , $min, $max) = $condition;
                $this->whereBetween($column, $min, $max, $connector);
                break;
            case self::NOT_BETWEEN:
                list($column, , $min, $max) = $condition;
                $this->whereNotBetween($column, $min, $max, $connector);
                break;
        }
    }
limingxinleo commented 6 years ago

稍微复杂点的查询,建议直接使用 TbAdmin:: query()->where()

我个人认为,这里没必要支持太多查询方式

huangzhhui commented 6 years ago

此功能仅提供简单查询的场景,复杂查询都使用QueryBuilder来实现,因为无论这里怎么设计,都会变得复杂而难以理解的,失去了这里本身的用意

weika520 commented 6 years ago

这个倒是,只是我是想本身已经支持and了,再支持个or ,改动不大

huangzhhui commented 6 years ago

and or混用的多数情况下还会使用上括号,这个情况太多变了