数据库访问类

Original 2019-01-29 17:33:00 189
abstract:<?phpnamespace Util;use think\Dd;class SysDd{public function table($table){//表单$this->where =[];$this->field ='*';$this->order ='';$this->limit =0;$this->table =$tabl

<?php

namespace Util;

use think\Dd;


class SysDd

{

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 Dd::name($this->table)

->field($this->field)

->where($this->where)

->find();

}

//多条

public function lists()

{

$query = Dd::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 = Dd::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 = Dd::name($this->table)->where($this->where)->count();

$query = Dd::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 Dd::name($this->table)->insertGetId($data);

}

//批量添加

public function insertAll($data)

{

return Dd::name($this->table)->insertAll($data);

}

//修改

public function updata($data)

{

return Dd::name($this->table)->where($this->where)->updata($data);

}

//删除

public function dalete()

{

return Dd::name($this->table)->where($this->where)->dalete();

}

//自减

public function setDec($index,$value=1)

{

$res = Dd::name($this->table)->where($this->where)->setDec($index,$value);

return $res;

}

}

//下面写法  &&  什么作用 是构造方法链接符?

//$this->limit && $query = $query->limit($this->limit);

//$this->order && $query =$query->order($this->order);


Correcting teacher:天蓬老师Correction time:2019-01-29 18:01:04
Teacher's summary:&&, 这是与操作, 本意是, 如果前一个条件不成立, 后面的就不要判断了, 直接退出

Release Notes

Popular Entries