摘要:<?php /** * Created by PhpStorm. * User: 普通用户 * Date: 2019/7/24 * Time: 21:22 */ namespace Util; use think\Db;
<?php
/**
* Created by PhpStorm.
* User: 普通用户
* Date: 2019/7/24
* Time: 21:22
*/
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();
}
// 查找多条记录
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 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;
}
}
批改老师:查无此人批改时间:2019-07-25 13:06:06
老师总结:完成的不错。多看thinkphp的文档,多练习功能,很快就能上手。继续加油。