BaseModel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. class BaseModel extends Model
  5. {
  6. /**
  7. * 添加一条的操作
  8. * @param $data
  9. * @return int|string
  10. */
  11. public function addInfo($data){
  12. $id = $this->insertGetId($data);
  13. return $id;
  14. }
  15. /**
  16. * 添加多条的操作
  17. * @param $data
  18. * @return int|string
  19. */
  20. public function addAllInfo($data){
  21. $id = $this->insertAll($data);
  22. return $id;
  23. }
  24. /**
  25. * 更新操作
  26. * @param $where
  27. * @param $data
  28. * @return $this
  29. */
  30. public function upInfo($where,$data){
  31. $bool = $this->where($where)->update($data);
  32. return $bool;
  33. }
  34. /**
  35. * 通过条件获取列表
  36. * @param $where
  37. * @param $order
  38. * @param $limit
  39. * @return false|\PDOStatement|string|\think\Collection
  40. */
  41. public function getList($where=[],$order="",$limit=50){
  42. $list = $this->where($where);
  43. if(!empty($order)){
  44. $list = $list->order($order);
  45. }
  46. $list = $list->limit($limit)->select();
  47. return $list;
  48. }
  49. /**
  50. * 通过条件获取分页列表
  51. * @param array $where
  52. * @param string $order
  53. * @param int $page
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getPage($where=[],$order="",$page=1,$pageSize=2){
  58. $data = [
  59. 'count'=> $this->_getPage($where,$order),
  60. 'list'=>$this->_getPage($where,$order,$page,$pageSize,'list')
  61. ];
  62. return $data;
  63. }
  64. /**
  65. * 针对于分页的基方法
  66. * @param $where
  67. * @param $order
  68. * @param int $page
  69. * @param int $pageSize
  70. * @param string $type
  71. * @return false|int|\PDOStatement|string|\think\Collection
  72. */
  73. private function _getPage($where,$order,$page=1,$pageSize=2,$type='count'){
  74. $list = $this;
  75. if($where) {
  76. $list = $this->where($where);
  77. }
  78. if($order){
  79. $list = $this->order($order);
  80. }
  81. if($type == 'count') {
  82. return $list->count();
  83. }else{
  84. if(intval($pageSize) > 100){
  85. $pageSize = 100;
  86. }
  87. return $list->page(intval($page),intval($pageSize))->select();
  88. }
  89. }
  90. /**
  91. * 根据条件获取一条记录
  92. * @param $where
  93. * @return array|false|\PDOStatement|string|Model
  94. */
  95. public function getOne($where=[]){
  96. if($where){
  97. $list = $this->where($where)->find();
  98. }else{
  99. $list = $this->find();
  100. }
  101. return $list;
  102. }
  103. }