返回 数据库访问类... 登陆

数据库访问类

意外 2019-06-05 20:26:50 241
<?php
namespace Util;
use think\Db;
// 引入框架自带的数据库操作类: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();
	}

	// 返回多条数据
	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 count(){
		return Db::name($this->table)->where($this->where)->count();
	}


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

	// 批量添加数据
	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;
	}
}


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网