方便好用CRUD封装

核心类

trait CrudModel
{

    /**
     * 构建查询
     * @param Builder $q
     */
    public function crudSearch(&$q) {}

    /*
     * 获取列表
     * @param array|\Closure $where
     * @param array $with
     * @param string[] $columns
     * @return mixed
     */
    public function crudGetList($where = [], $with = [], $columns = ["*"]) {
        $q = static::query();
        if($where) {
            if(is_callable($where)) {
                $where($q);
            } else if(is_array($where)) {
                $q->where($where);
            }
        }
        if($with) {
            $q->with($with);
        }
        $this->crudSearch($q);
        return $this->paginate($q, $columns);
    }

    /*
     * 添加或编辑
     * @param array $update
     * @param array|\Closure $where
     * @return static|array
     * @throws ParamException
     */
    public function crudSave($update, $where = null) {
        if(is_string($update)) {
            $update = $update::M();
        }
        if(isset($update[$this->primaryKey])) {
            $q = static::query()->where($this->primaryKey, $update[$this->primaryKey]);
            if($where) {
                if(is_callable($where)) {
                    $where($q);
                } else if(is_array($where)) {
                    $q->where($where);
                }
            }
            $q = $q->first();
            if(!$q) {
                throw new QueryException;
            }
            if($q->fill($update)->save()) {
                return $q;
            }
            return null;
        } else {
            $m = new static;
            if($where) {
                $update = array_merge($update, $where);
            }
            if($m->fill($update)->save()) {
                return $m;
            }
            return null;
        }
    }


    /*
     * 获取详情
     * @param array|\Closure $where
     * @param array $with
     * @param string[] $columns
     * @return mixed
     * @throws ParamException
     */
    public function crudGetDetail($where = [], $with = [], $columns = ["*"]) {
        $q = static::query();
        if($where) {
            if(is_callable($where)) {
                $where($q);
            } else if(is_array($where)) {
                $q->where($where);
            }
        } else {
            $q->where($this->primaryKey, Request::input("id"));
        }
        if($columns) {
            $q->select($columns);
        }
        if($with) {
            $q->with($with);
        }
        $m = $q->first();
        if(!$m) {
            throw new ParamException;
        }
        return $m->toArray();
    }

    /*
     * 删除
     * @param null|\Closure $callback
     */
    public function crudDel($callback = null) {
        DB::transaction(function () use($callback) {
            $ids = IdValidate::M()["id"];
            if($callback) {
                $callback($ids);
            } else {
                static::query()
                    ->whereIn($this->primaryKey, $ids)
                    ->delete();
            }
        });
    }
}

统一模型继承

class Model extends \Illuminate\Database\Eloquent\Model
{
    use PaginateFormat, CrudModel;
}

模拟调用

    /**
     * @public 获取车辆标签列表
     * @return JsonResponse
     * @throws Throwable
     */
    public function getCarTagList() {
        $r = new CarTagModel;
        $r = $r->crudGetList(function($q) {

            $all = Request::all();
            foreach ($all as $k => $v) {
                if($k === "key") {
                    $q->where("name", "LIKE", "%{$v}%");
                }
            }
        });
        return $this->json($r);
    }

    /**
     * @public 获取车辆标签详情
     * @return JsonResponse
     * @throws Throwable
     */
    public function getCarTagDetail() {
        $r = new CarTagModel;
        $r = $r->crudGetDetail();
        return $this->json($r);
    }

    /**
     * @public 保存车辆标签
     * @return JsonResponse
     * @throws Throwable
     */
    public function setCarTag() {
        $r = new CarTagModel;
        $r->crudSave(SetCarTagValidate::class);
        return $this->json();
    }

    /**
     * @public 删除车辆标签
     * @return JsonResponse
     * @throws Throwable
     */
    public function delCarTag() {
        $r = new CarModel;
        $r->crudDel();
        return $this->json();
    }

后记

  • 如果觉得方便了开发请把 害怕 打在评论上
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 8
fatrbaby

害怕😨

3年前 评论

规范的 api 路由配合 laravel 的隐性路由绑定,可以说让你这边的大多数代码属于多余的。

3年前 评论
h6play (楼主) 3年前

局限性很高,如果是多对多的curd 呢 多态多对多的curd呢

3年前 评论
xiaoAgiao

这种封装主要出现在使用PHP-MySQLi-Database-Class这种东西上,大概是几年前的做法吧,在现代开发框架中做这种封装的思想真的是LOW,新手不要被误导,Laravel提供的数据库查询构造器和ORM已经很方便好用了

3年前 评论
xiaoAgiao (作者) 3年前
panda-sir

:joy: 看了 觉得有点尴尬

3年前 评论
❤seven 3年前

有点绕懵了,可能不太适合我这样的新手,一般封装是统一规范达到容易维护的目的,从维护的角度即使代码冗余也是可取的。

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!