1 在model里创建Common.php
2 Common.php里的内容
<?phpnamespace app\\index\\model;use think\\Model;use think\\Db;class Common extends Model{/** 公共类变量* */protected $table;public function __construct($data = []){parent::__construct($data);}/** 添加数据* */public function addData($data = array()){return Db::name($this -> table)->insert($data);}/** 修改数据* */public function saveData($where=array(),$data=array()){return Db::name($this -> table)->where($where) ->update($data);}/** 删除数据* */public function delData($where){return Db::name($this -> table)->where($where) -> delete();}/** 连表查询* */public function selectLinkedList($alias,$join,$condition,$type,$field,$order=\'\',$tol=\'\',$limit=\'\'){return Db::name($this -> table)->alias($alias)->join($join,$condition,$type)->field($field)->order($order)->limit($tol,$limit)->select();}/** 连表条件查询* */public function selectWhereLinkedList($alias,$join,$condition,$type,$field,$where,$order,$tol=\'\',$limit=\'\'){return Db::name($this -> table)->alias($alias)->join($join,$condition,$type)->field($field)->where($where)->order($order)->limit($tol,$limit)->select();}/*连表查询单个数据**/public function selectWhereLinkedFind($alias,$join,$condition,$type,$field,$where){return Db::name($this -> table)->alias($alias)->join($join,$condition,$type)->field($field)->where($where)->find();}/** 查询多条数据* */public function getMultipleData(){return Db::name($this -> table)->select();}/** 条件查询查询多条数据* */public function getWhereMultipleData($where){return Db::name($this -> table)->where($where)->select();}/** 根据条件查询一条数据* */public function getOne($where){return Db::name($this -> table)->where($where)->find();}}
3 model继承
我们的类要继承建立的Common.php
<?phpnamespace app\\index\\model;use think\\Model;class User extends Common{protected $table;public function __construct($data = []){parent::__construct($data);$this -> table = \"User\";}}
4 ,Controller 需要继承我们的Model
<?phpnamespace app\\index\\controller;use app\\index\\model\\user;class Users extends Controller{public function __construct(){parent::__construct();$this ->user = model(\'user\');}}
在这一个Controller里面就可以使用我们的 公共方法了
一个多删除案例
public function delUsers(){$id=input(\"id/a\");$id = implode(\",\",$id) ;$i=$this->user->delData(\"id in ($id)\");if ($i){return json([\"code\"=>0,\"msg\"=>\"删除成功\",\"data\"=>$i]);}else{return json([\"code\"=>400,\"msg\"=>\"删除失败\",\"data\"=>$i]);}}
连表查询的接口,附加分页,可以附带查询条件
public function selectUser(){$name = input(\'name\');//获取每页显示的条数$limit= $this->request->param(\'limit\');//获取当前页数$page= $this->request->param(\'page\');//计算出从那条开始查询$tol=($page-1)*$limit;// 查询出当前页数显示的数据$alias=\'u\'; //自己表的别名$join=\'company c\'; //连表的表名和别名$condition=\'u.company_id=c.id\'; //关联的字段id$type=\'LEFT\'; //类型$field=\'u.id,u.name,u.username,u.password,u.position,c.company_name,u.time\'; //查询的字段$order=\'id desc\'; //排序$where=[];//输入查询条件 以前的文章有;$list = $this->user->selectWhereLinkedList($alias,$join,$condition,$type,$field,$where,$order,$tol,$limit);$count=count($list);//长度//返回数据return json([\"code\"=>0,\"msg\"=>\"\",\"count\"=>$count,\"data\"=>$list]);}
总结:此方法为创建common.php,继承common,调用 model,
设置参数调用方法。
技术支持来源于朋友与同事
—– 日积月累,持之以恒