美文网首页
让Phalcon的Model支持关联数组条件查询

让Phalcon的Model支持关联数组条件查询

作者: 许文同学 | 来源:发表于2019-02-27 00:21 被阅读0次

    用TP的同学一定非常熟悉数组批量字段查询,在一些条件较多但并不复杂的查询中,这种方式非常快捷。但Phalcon没有自带,所以我在代码中对Model做了一层封装,用以处理数组查询。

    <?php
    namespace app\models;
    
    use Phalcon\Mvc\Model;
    
    class MyBaseModel extends Model
    {
    
        // 关联数组查询
        static public function arrFind($arr, $relation='and', $assist=[])
        {
            $parameters = self::dealWithWhereArr($arr, $relation, $assist);
            // 查询返回
            return self::find($parameters);
        }
    
        // 关联数组查询
        static public function arrFindFirst($arr, $relation='and', $assist=[])
        {
            $parameters = self::dealWithWhereArr($arr, $relation, $assist);
            // 查询返回
            return self::findFirst($parameters);
        }
    
        // 分页查询
        static public function arrFindPage($pageSize, $pageNum, $arr, $relation='and', $assist=[])
        {
            $count = self::count(self::dealWithWhereArr($arr, $relation, $assist));
            $offset = $pageSize*($pageNum-1);
            $assist['limit'] = [(int)$pageSize, (int)$offset];
            $list = self::arrFind($arr, $relation, $assist)->toArray();
            $meta = [
                'total' => $count,
                'pageNum' => $pageNum,
                'pageSize' => $pageSize
            ];
            self::keyToHump($list);
            return [
                'list' => $list,
                'meta' => $meta
            ];
        }
    
        /**
         * 组装关联数组查询
         * @param $arr 关联数组,支持类TP的[k=>['>',6], k=>['not null'], k=>['IN', Arr]]
         * @param string $relation
         * @param array $assist
         * @return array
         */
        static public function dealWithWhereArr($arr, $relation='and', $assist=[])
        {
            if (isset($arr['bind']) && is_array($arr['bind'])){
                return $arr;
            }
            if (is_array($relation)){
                $assist = array_merge($relation, $assist);
                $relation = 'and';
            }
            // 预处理conditions
            $conditions = [];
            $bind = [];
            foreach ($arr as $k => $v){
                $bindKey = str_replace('.', '__', $k); // SQL绑定参数变量名中不可有符号.
                if (!is_array($v)){
                    $conditions[] = $k.' = :'.$bindKey.':';
                    $bind[$bindKey] = $v;
                    continue;
                }
                if (isset($v[1]) && is_array($v[1])){
                    $conditions[] = "{$k} {$v[0]} ({{$bindKey}:array})";
                    $bind[$bindKey] = $v[1];
                    continue;
                }
                $conditions[] = $k.' '.implode(' ', $v);
            }
            $conditions = implode(" {$relation} ", $conditions);
            // 查询返回
            return array_merge([
                'conditions' => $conditions,
                'bind' => $bind,
            ], $assist);
        }
    
        /**
         * 数组键名下划线转驼峰
         * @param $list
         * @return array
         */
        static public function keyToHump(&$list){
            if (!is_array($list)){
                return $list;
            }
            $newList = [];
            foreach ($list as $k => $item){
                if (is_numeric($k)){
                    $newList[] = self::keyToHump($item);
                    continue;
                }
                $humpKey = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
                    return strtoupper($matches[2]);
                },$k);
                $newList[$humpKey] = $item;
            }
            $list = $newList;
            return $newList;
        }
    
        /*// 捕捉验证器失败结果到全局变量
        protected function validate($validator){
            $bol = parent::validate($validator);
            if (!$bol){
                $Messages = $this->getMessages();
                foreach ($Messages as $msg){
                    $GLOBALS['ExceptionTipsMsg'][] = $msg->getMessage();
                }
            }
            return $bol;
        }*/
    }
    

    表继承后就可以使用类似下面的查询方式

    // 单表查询
    Drivers::arrFind([
        'id' => ['IN', $driverIdList],
        'age' => ['>', 6],
        'test' => ['not null'],
    ], 'and', [
        'columns' => 'id, real_name AS realName'
    ])->toArray();
    
    // 多表条件预处理
    $parameter = [
        'a.age' => ['>', 12],
        'b.status' => ['IN', [2, 3]],
    ];
    $where = MyBaseModel::dealWithWhereArr($parameter);
    $model =  $this->modelsManager->createBuilder()
        ->addfrom('app\models\Test\A','a')
        ->join('app\models\Test\B', 'b.a_id = a.id','b')
        ->where($where['conditions'], $where['bind'])
        ->getQuery()
        ->execute()
        ->toArray();
    

    相关文章

      网友评论

          本文标题:让Phalcon的Model支持关联数组条件查询

          本文链接:https://www.haomeiwen.com/subject/uaetuqtx.html