亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

實現(xiàn)數(shù)據(jù)庫訪問類封裝

asal 2019-05-15 16:27:24 338
abstrak:由于thinkphp5.1的數(shù)據(jù)庫訪問類方法較多,我們并用不到那么多,防止對數(shù)據(jù)庫產(chǎn)生過多的訪問,自己編寫數(shù)據(jù)庫訪問方法:<?php /**  * Created by PhpStorm.  * User: Administrator  * Date: 2019/5/15  *&nb

由于thinkphp5.1的數(shù)據(jù)庫訪問類方法較多,我們并用不到那么多,防止對數(shù)據(jù)庫產(chǎn)生過多的訪問,自己編寫數(shù)據(jù)庫訪問方法:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/15
 * Time: 16:08
 */
namespace Util;
use think\Db;

class SysDb {
    public function table($table){
        $this->where = [];
        $this->field = '*';
        $this->order = '';
        $this->limit = 0;

        $this->table = $table;
        return $this;
    }

    public function field($field){
        $this->field = $field;
        return $this;
    }

    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;
    }

    //分頁
    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());
    }

    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();
    }

}


Guru membetulkan:查無此人Masa pembetulan:2019-05-16 09:21:46
Rumusan guru:完成的不錯。自己封裝,條件要多判斷,防止別人傳值注入。繼續(xù)加油。

Nota Keluaran

Penyertaan Popular