返回SysDb的实......登陆

SysDb的实现

1Pong2019-06-04 16:45:05261

通过SysDb类的封装,把常用的增删改查封装到类中,去除了tp自带的较为臃肿的方法库。代码如下:

<?php

namespace Util;

use think\Db;

class SysDb
{
    //设置查询的业务表
    public  function  table($table)
    {
        $this->field='*';
        $this->where=[];
        $this->limit=0;
        $this->order='';

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

    //字段
    public  function  field($field='*')
    {
        $this->field=$field;
        return $this;
    }

    //条件
    public  function where($where=[])
    {
        $this->where=$where;
        return $this;
    }

    //限制的条数
    public  function  limit($limit)
    {
        $this->limit=$limit;
        return $this;
    }

    //排序
    public  function  order($order='')
    {
        $this->order=$order;
        return $this;
    }

    //返回一条记录
    public function  item()
    {
        $query=Db::name($this->table)->field($this->field)->where($this->where);

        if($this->order)
        {
            $query=$query->order($this->order);
        }

        return $query=$query->find();
    }

    //返回多条记录
    public function lists()
    {
        $query=Db::name($this->table)->field($this->field)->where($this->where);

        if($this->limit)
        {
            $query=$query->order($this->limit);
        }

        if($this->order)
        {
            $query=$query->order($this->order);
        }

        return $query->select();
    }

    //返回指定条件的数据条数
    public  function  count()
    {
        return Db::name($this->table)->where($this->where)->count();
    }

    //分页
    public function  pages($pageSize=10)
    {
        //数据总数
        $count=$this->count();

        $query=Db::name($this->table)->field($this->field)->where($this->where);

        if($this->order)
        {
            $query=$query->order($this->order);
        }

        //分页数据
        $data=$query->paginate($pageSize,$count);

        return ['count'=>$count,'data'=>$data,'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();
    }


}


最新手记推荐

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

全部回复(0)我要回复

暂无评论~
  • 取消回复发送