サマリー:<?php namespace Util; use think\Db; // 引入框架自帶的數(shù)據(jù)庫(kù)操作類:Db類; class SysDb{ // 指定表名 public function table($table){ //查詢表時(shí)將一切操作初始化; $this->where = [
<?php namespace Util; use think\Db; // 引入框架自帶的數(shù)據(jù)庫(kù)操作類:Db類; class SysDb{ // 指定表名 public function table($table){ //查詢表時(shí)將一切操作初始化; $this->where = []; $this->field = '*'; $this->order = ''; $this->limit = 0; $this->table = $table; return $this; } // 指定查詢字段 public function field($field = '*'){ $this->field = $field; return $this; } // 加載數(shù)量 public function limit($limit){ $this->limit = $limit; return $this; } // 排序 public function order($order){ $this->order = $order; return $this; } // 指定查詢條件 public function where($where = []){ $this->where = $where; return $this; } // 返回一條記錄 public function item(){ return Db::name($this->table)->field($this->field)->where($this->where)->find(); } // 返回多條數(shù)據(jù) public function lists(){ $query = Db::name($this->table)->field($this->field)->where($this->where); $this->limit && $query = $query->limit($this->limit); $this->order && $query = $query->order($this->order); return $query->select(); } // 自定義索引 public function cates($index){ $query = Db::name($this->table)->field($this->field)->where($this->where); $this->limit && $query = $query->limit($this->limit); $this->order && $query = $query->order($this->order); $lists = $query->select(); if(!$lists){ return $lists; } $result = []; foreach ($lists as $key => $value) { $result[$value[$index]] = $value; } return $result; } //總數(shù)量; public function count(){ return Db::name($this->table)->where($this->where)->count(); } // 分頁(yè) public function pages($pageSize = 10){ $total = Db::name($this->table)->where($this->where)->count(); $query = Db::name($this->table)->field($this->field)->where($this->where); $this->order && $query = $query->order($this->order); $data = $query->paginate($pageSize,$total); return array('total'=>$total,'lists'=>$data->items(),'pages'=>$data->render()); } // 添加一條數(shù)據(jù); public function insert($data){ return Db::name($this->table)->insertGetId($data); } // 批量添加數(shù)據(jù) public function insertAll($data){ return Db::name($this->table)->insertAll($data); } // 修改 public function update($data){ return Db::name($this->table)->where($this->where)->update($data); } // 刪除 public function delete(){ return Db::name($this->table)->where($this->where)->delete(); } // 自減 public function setDec($index,$value=1){ $res = Db::name($this->table)->where($this->where)->setDec($index,$value); return $res; } }
添削の先生:查無(wú)此人添削時(shí)間:2019-06-06 09:38:04
先生のまとめ:完成的不錯(cuò)。后臺(tái)cms管理系統(tǒng),就是對(duì)數(shù)據(jù)進(jìn)行操作。操作越簡(jiǎn)單越好。繼續(xù)加油。