基类
在application目录的model中新建Base基类
直接上代码
<?php /** * Created by PhpStorm. * User: Zhangyongfeng * Date: 2020/12/1 * Time: 11:32 * * ━━━━━━━━━神兽出没━━━━━━━━━ * * ┏┓ ┏┓+ + * ┏┛┻━━━┛┻┓ + + * ┃ ┃ * ┃ ━ ┃ ++ + + + * ████━████ ┃+ * ┃ ┃ + * ┃ ┻ ┃ * ┃ ┃ + + * ┗━┓ ┏━┛ * ┃ ┃ * ┃ ┃ + + + + * ┃ ┃ Code is far away from bug with the animal protecting * ┃ ┃ + 神兽保佑,代码无bug * ┃ ┃ * ┃ ┃ + * ┃ ┗━━━┓ + + * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ + + + + * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛+ + + + * * ━━━━━━━━━感觉萌萌哒━━━━━━━━━ */ namespace appasemodel; use thinkModel; abstract class Base extends Model { private $primary_key; // 主键 protected $order = ['create_time' => 'desc']; // 默认排序 protected $code = 4004; static public function showReturnCode($code = '', $data = [], $msg = ''){ return appasecontrollerBase::showReturnCode($code, $data, $msg); } static public function showReturnCodeWithOutData($code = '', $msg = '') { return appasecontrollerBase::showReturnCode($code, [], $msg); } protected function initialize() { parent::initialize(); // TODO: Change the autogenerated stub $this->primary_key = $this->getPk(); } /** * 查询数据表格内容 * Power: ZYF * Email:1322816443@qq.com * @param array $where 查询条件 (二维数组) * @param number $pageSize 限制一页多少条数据 * @param array $query ['limit' => 10, 'page' => 1] page请求的页数 limit 限制从第几条开始查 * @param array $order 排序 * @return array * */ public function baseGetTableList($where, $pageSize, $query, $order = []) { if(empty($order)){ $order = $this->order; } $data = $this->where($where) ->order($order) ->paginate($pageSize,false,['query' => $query])->toArray(); return $data; } /** * 查询带分页数据 * Power: ZYF * Email:1322816443@qq.com * @param array $where 查询条件 (二维数组) * @param number $pageNum 限制一页多少条数据 * @param array $query ['limit' => 10, 'page' => 1] page请求的页数 limit 限制从第几条开始查 * @param array $order 排序 * @return array * */ public function getPaginate($where, $pageNum, $query, $order = []) { if(empty($order)){ $order = $this->order; } return $this->where($where) ->order($order) ->paginate($pageNum,false,['query' => $query]); } /** * 获取数据条数 * Power: ZYF * Email:1322816443@qq.com * @param array $where 查询条件 (二维数组) * @param string $field 限制字段,默认为所有 * @return int|string * */ public function getCount($where = [], $field = "*") { return $this->where($where)->count($field); } /** * 查询所有数据 * Power: ZYF * Email:1322816443@qq.com * @param array $where 查询条件 (二维数组) * @param string $field 限制字段,默认为所有 * @param array $order 排序 * @return array * */ public function getAll($where = [], $field = "*", $order = []) { if(empty($order)){ $order = $this->order; } return $this->where($where)->order($order)->field($field)->all(); } /** * 查询所有数据转为数组 * Power: ZYF * Email:1322816443@qq.com * @param array $where 查询条件 (二维数组) * @param array $order 排序 * @return array * */ public function getArr($where = [], $order = []) { if(empty($order)){ $order = $this->order; } return $this->where($where)->order($order)->toArray(); } /** * 根据条件查询一条数据 * Power: ZYF * Email:1322816443@qq.com * @param array $where 查询条件 (二维数组) * @return array|string * */ public function getOneWhere($where = []) { if(empty($where)){ return self::showReturnCode(1003); } return $this->where($where)->findOrEmpty(); } /** * 根据主键查询一条数据 * Power: ZYF * Email:1322816443@qq.com * @param string|int $primary 主键 * @return array|string * */ public function getOnePrimary($primary) { if(!is_numeric($primary)){ return self::showReturnCode(1003); } return $this->find($primary); } /** * 添加数据 * Power: ZYF * Email:1322816443@qq.com * @param array $data 数据 * @param boolean $allow 是否过滤字段 * @return array|string * */ public function add($data, $allow = true) { $code = 1001; // 如果是一维数组 if(count($data) == count($data, 1)){ $primary_key = $this->primary_key; $this->allowField($allow)->save($data); $data['id'] = $this->$primary_key; }else{ $data = $this->allowField($allow)->saveAll($data); } if(!$data){ $code = 9002; } return self::showReturnCode($code, $data); } /** * 修改数据 * Power: ZYF * Email:1322816443@qq.com * @param array $data 数据 * @param boolean $allow 是否过滤字段 * @return array * */ public function edit($data, $allow = true) { $code = 1001; $data = $this->allowField($allow)->save($data, [$this->primary_key => $data[$this->primary_key]]); if(!$data){ $code = 9004; } return self::showReturnCode($code, $data); } /** * 删除数据 * Power: ZYF * Email:1322816443@qq.com * @param string|int $primary * @return array * */ public function del($primary) { $code = 1001; $data = $this->getOnePrimary($primary); if($data){ $this->destroy($primary); }else{ $code = 1010; } return self::showReturnCode($code, $data); } }
使用
model目录需要创建对应数据库表名的class文件,并继承Base基类
添加
$param = request()->param(); $bannerModel = new Banner(); $res = $bannerModel->add($this->param);
删除
$id = request()->param("id"); $bannerModel = new Banner(); $bannerModel->del($id);
修改
$param = request()->param(); $bannerModel = new Banner(); $bannerModel->edit($param);
查询分页数据
$uactionModel = new Uaction(); $data = $uactionModel->getPaginate([['level', '=', '1']], 1, request()->param(), 'path');