123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace app\index\model;
- use think\Model;
- class BaseModel extends Model
- {
- /**
- * 添加一条的操作
- * @param $data
- * @return int|string
- */
- public function addInfo($data){
- $id = $this->insertGetId($data);
- return $id;
- }
- /**
- * 添加多条的操作
- * @param $data
- * @return int|string
- */
- public function addAllInfo($data){
- $id = $this->insertAll($data);
- return $id;
- }
- /**
- * 更新操作
- * @param $where
- * @param $data
- * @return $this
- */
- public function upInfo($where,$data){
- $bool = $this->where($where)->update($data);
- return $bool;
- }
- /**
- * 通过条件获取列表
- * @param $where
- * @param $order
- * @param $limit
- * @return false|\PDOStatement|string|\think\Collection
- */
- public function getList($where=[],$order="",$limit=50){
- $list = $this->where($where);
- if(!empty($order)){
- $list = $list->order($order);
- }
- $list = $list->limit($limit)->select();
- return $list;
- }
- /**
- * 通过条件获取分页列表
- * @param array $where
- * @param string $order
- * @param int $page
- * @param int $pageSize
- * @return array
- */
- public function getPage($where=[],$order="",$page=1,$pageSize=2){
- $data = [
- 'count'=> $this->_getPage($where,$order),
- 'list'=>$this->_getPage($where,$order,$page,$pageSize,'list')
- ];
- return $data;
- }
- /**
- * 针对于分页的基方法
- * @param $where
- * @param $order
- * @param int $page
- * @param int $pageSize
- * @param string $type
- * @return false|int|\PDOStatement|string|\think\Collection
- */
- private function _getPage($where,$order,$page=1,$pageSize=2,$type='count'){
- $list = $this;
- if($where) {
- $list = $this->where($where);
- }
- if($order){
- $list = $this->order($order);
- }
- if($type == 'count') {
- return $list->count();
- }else{
- if(intval($pageSize) > 100){
- $pageSize = 100;
- }
- return $list->page(intval($page),intval($pageSize))->select();
- }
- }
- /**
- * 根据条件获取一条记录
- * @param $where
- * @return array|false|\PDOStatement|string|Model
- */
- public function getOne($where=[]){
- if($where){
- $list = $this->where($where)->find();
- }else{
- $list = $this->find();
- }
- return $list;
- }
- }
|