Swoole框架Hyperf增加类似yii2的andFilterWhere功能以及打印sql执行语句dumpSql功能

点击数:42

swoole框架hyperf增加类似yii2的andFilterWhere功能以及打印sql执行语句dumpSql功能

本文地址http://yangjianyong.cn/?p=269转载无需经过作者本人授权

直接创建一个基础的ActiveRecord类,继承于Hyperf\DbConnection\Model\Model,然后使用Hyperf\Database\Query\Builder的方法macro进行添加。代码如下

<?php
declare(strict_types=1);

namespace Api\ApiBase;

use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Model\Model;

/**
 * Class BaseActiveRecord
 * @package Api\ApiBase
 */
class BaseActiveRecord extends Model
{
    /**
     * BaseActiveRecord constructor.
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        //添加andFilterWhere()方法
        Builder::macro('andFilterWhere', function ($key, $operator, $value) {
            if ($value === '' || $value === '%%' || $value === '%') {
                return $this;
            }
            return $this->where($key, $operator, $value);
        });
        //添加orFilterWhere()方法
        Builder::macro('orFilterWhere', function ($key, $operator, $value) {
            if ($value === '' || $value === '%%' || $value === '%') {
                return $this;
            }
            return $this->orWhere($key, $operator, $value);
        });
        //添加getRawSql()方法
        Builder::macro('getRawSql', function () {
            $sql = str_replace(['%', '?'], ['%%', '%s'], $this->toSql());

            $handledBindings = array_map(function ($binding) {
                if (is_numeric($binding)) {
                    return $binding;
                }

                $value = str_replace(['\\', "'"], ['\\\\', "\'"], $binding);

                return "'{$value}'";
            }, $this->getConnection()->prepareBindings($this->getBindings()));

            return vsprintf($sql, $handledBindings);
        });
        //添加dumpSql()方法
        Builder::macro('dumpSql', function () {
            echo '================DUMP SQL START=========================' . PHP_EOL . PHP_EOL;
            echo $this->getRawSql() . PHP_EOL . PHP_EOL;
            echo '================DUMP SQL END=========================' . PHP_EOL . PHP_EOL;
        });
        parent::__construct($attributes);
    }

}

使用示例

 /**
     * 获取权限列表
     * @param array $condition
     * @return array
     */
    public function getPermissionList(array $condition)
    {
        $model = PermissionActiveRecord::andFilterWhere('api_app', 'like', '%' . $condition['apiApp'] . '%')
            ->andFilterWhere('api_version', '=', $condition['apiVersion'])
            ->andFilterWhere('api_class', 'like', '%' . $condition['apiClass'] . '%')
            ->andFilterWhere('api_function', 'like', '%' . $condition['apiFunction'] . '%');
        $count = $model->count('api_route');
        $list = $model->offset($condition['offset'])->limit($condition['limit'])->get()->toArray();
        return [
            'count' => $count,
            'list' => $list,
        ];
    }

欢迎来我的博客逛一逛 杨建勇的个人博客http://yangjianyong.cn

发表评论