Base.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace biz\base\models;
  3. use common\components\util;
  4. use Yii;
  5. use yii\db\ActiveRecord;
  6. use yii\data\Pagination;
  7. class Base extends ActiveRecord
  8. {
  9. public function getModel()
  10. {
  11. $self = get_called_class();
  12. return new $self;
  13. }
  14. /**
  15. * 添加
  16. */
  17. public function add($data, $returnObject = false)
  18. {
  19. $model = $this->getModel();
  20. $attributes = $model->attributes;
  21. $fields = array_keys($attributes);
  22. foreach ($fields as $key) {
  23. if (isset($data[$key])) {
  24. $model->$key = $data[$key];
  25. }
  26. }
  27. $model->save();
  28. if ($returnObject) {
  29. return $model;
  30. }
  31. return $model->attributes;
  32. }
  33. /**
  34. * 批量添加
  35. */
  36. public function batchAdd($data)
  37. {
  38. $model = $this->getModel();
  39. foreach ($data as $attributes) {
  40. $otherModel = clone $model;
  41. // false 不检测字段安全 true 需要在model设置rules
  42. $otherModel->setAttributes($attributes, false);
  43. $otherModel->save();
  44. }
  45. }
  46. /**
  47. * 取表的字段
  48. */
  49. public function getFields()
  50. {
  51. $model = $this->getModel();
  52. $attributes = $model->attributes;
  53. return array_keys($attributes);
  54. }
  55. /**
  56. * 根据主键ID删除一条记录
  57. */
  58. public function deleteById($id)
  59. {
  60. $return = $this->conditionQuery(['id' => $id])->delete();
  61. return $return;
  62. }
  63. /**
  64. *根据条件删除一条或多条记录
  65. */
  66. public function deleteByCondition($condition)
  67. {
  68. if (empty($condition)) {
  69. return 0;
  70. }
  71. $where = array();
  72. $variable = array();
  73. foreach ($condition as $key => $val) {
  74. $where[] = $key . '=:' . $key;
  75. $variable[':' . $key] = $val;
  76. }
  77. $whereString = implode(' and ', $where);
  78. $count = self::deleteAll($whereString, $variable);
  79. return $count;
  80. }
  81. /**
  82. * 根据主键ID更新一条记录
  83. */
  84. public function updateById($id, $data)
  85. {
  86. $model = $this->getModel();
  87. $attributes = $model->attributes;
  88. $fields = array_keys($attributes);
  89. foreach ($data as $key => $val) {
  90. if (in_array($key, $fields) == false) {
  91. //删除表里不存在字段不进行保存
  92. unset($data[$key]);
  93. }
  94. }
  95. $re = self::updateAll($data, 'id=:id', ['id' => $id]);
  96. if ($re == 1) {
  97. return ['status' => true, 'data' => $data];
  98. }
  99. return ['status' => false, 'data' => $data];
  100. }
  101. //多个主键更新 shish 2019.9.3
  102. public function updateByIds($ids, $data)
  103. {
  104. self::updateAll($data, ['id' => $ids]);
  105. }
  106. /**
  107. * 根据条件更新一条或多条记录
  108. */
  109. public function updateByCondition($condition, $data)
  110. {
  111. if (empty($condition) || !is_array($condition)) {
  112. throw new \Exception("condition error");
  113. }
  114. $where = array();
  115. $variable = array();
  116. foreach ($condition as $key => $val) {
  117. if (is_array($val)) {
  118. util::fail('更新操作暂不支持此查询方式:' . json_encode($val));
  119. }
  120. $where[] = $key . '=:' . $key;
  121. $variable[':' . $key] = $val;
  122. }
  123. $whereString = implode(' and ', $where);
  124. $count = self::updateAll($data, $whereString, $variable);
  125. return $count;
  126. }
  127. /**
  128. * 根据主键ID查出一条记录
  129. */
  130. public function getById($id, $returnObject = false)
  131. {
  132. if ($returnObject == false) {
  133. return $this->conditionQuery(['id' => $id])->asArray()->one();
  134. } else {
  135. return $this->conditionQuery(['id' => $id])->one();
  136. }
  137. }
  138. /**
  139. *根据条件查出一条记录
  140. */
  141. public function getByCondition($condition, $returnObject = false, $order = false)
  142. {
  143. $query = $this->conditionQuery($condition);
  144. if (isset($order)) {
  145. $query->orderBy($order);
  146. }
  147. return $returnObject == true ? $query->one() : $query->asArray()->one();
  148. }
  149. /**
  150. * 支持查询条件包括
  151. * ['name'=>'john','id>'=>3,'id<'=>10,'id'=>['in',[10,11,12]],'age'=>['between',[20,30]],'name'=>['like','Jack']];
  152. */
  153. public function conditionQuery($condition = [])
  154. {
  155. $whereNum = 0;
  156. $query = self::find();
  157. if (!empty($condition)) {
  158. foreach ($condition as $key => $val) {
  159. $param = [$key => $val];
  160. if (strstr($key, '>')) {
  161. //暂不支持大于等于,等于请使用between
  162. $pos = strpos($key, '>');
  163. $currentKey = substr($key, 0, $pos);
  164. $param = ['>', $currentKey, $val];
  165. } elseif (strstr($key, '<')) {
  166. //暂不支持小于等于,等于请使用between
  167. $pos = strpos($key, '<');
  168. $currentKey = substr($key, 0, $pos);
  169. $param = ['<', $currentKey, $val];
  170. } elseif (is_array($val)) {
  171. if (count($val) != 2) {
  172. util::fail('参数不至2个,非法查询方式:' . json_encode($condition));
  173. }
  174. if ($val[0] == 'in') {
  175. $param = ['in', $key, $val[1]];
  176. } elseif ($val[0] == 'between') {
  177. $param = ['between', $key, $val[1][0], $val[1][1]];
  178. } elseif ($val[0] == 'like') {
  179. $param = ['like', $key, $val[1]];
  180. } else {
  181. util::fail('非法查询方式:' . json_encode($condition));
  182. }
  183. } else {
  184. }
  185. if ($whereNum == 0) {
  186. $query->where($param);
  187. } else {
  188. $query->andWhere($param);
  189. }
  190. $whereNum++;
  191. }
  192. }
  193. return $query;
  194. }
  195. //是否存在数据 shish 2019.
  196. public function exists($condition)
  197. {
  198. return $this->conditionQuery($condition)->exists();
  199. }
  200. /**
  201. *根据条件查出多条记录
  202. */
  203. public function getAllByCondition($condition, $order = null, $field, $indexBy = null)
  204. {
  205. $query = $this->conditionQuery($condition)->select($field);
  206. if (isset($indexBy)) {
  207. $query->indexBy($indexBy);
  208. }
  209. if (isset($order)) {
  210. $query->orderBy($order);
  211. }
  212. return $query->asArray()->all();
  213. }
  214. /**
  215. * 根据多个主键ID查询多条记录
  216. */
  217. public function getByIds($ids, $order = null, $indexBy = null)
  218. {
  219. $data = [];
  220. if (empty($ids)) {
  221. return $data;
  222. }
  223. $query = $this->conditionQuery(['id' => ['in', $ids]]);
  224. if (isset($indexBy)) {
  225. $query->indexBy($indexBy);
  226. }
  227. if (!empty($order)) {
  228. $query->order($order);
  229. }
  230. $data = $query->asArray()->all();
  231. return $data;
  232. }
  233. /**
  234. *根据条件查出数量
  235. */
  236. public function getCount($condition)
  237. {
  238. return $this->conditionQuery($condition)->count();
  239. }
  240. //分页查询返回json形式 shish 2019.9.21
  241. public function getList($field, $where, $page, $pageSize, $order = '', $with = '')
  242. {
  243. $offset = ($page - 1) * $pageSize;
  244. $query = $this->conditionQuery($where)->select($field);
  245. $clone = clone $query;
  246. $count = $clone->count();
  247. $totalPage = ceil($count / $pageSize);
  248. $data['totalPage'] = $totalPage;//总共页数
  249. $data['moreData'] = $totalPage > $page ? 1 : 0;//是否还有更多数据
  250. if (!empty($order)) {
  251. $query->orderBy($order);
  252. }
  253. if (!empty($with)) {
  254. $query->with($with);
  255. }
  256. $list = $query->offset($offset)->limit($pageSize)->asArray()->all();
  257. $data['list'] = $list;//数据
  258. return $data;
  259. }
  260. //分页查询返回数据 shish 2019.9.21
  261. public function getListData($field, $where, $page, $pageSize, $order = '', $with = '')
  262. {
  263. $offset = ($page - 1) * $pageSize;
  264. $query = $this->conditionQuery($where)->select($field);
  265. $cloneQuery = clone $query;
  266. $count = $cloneQuery->count();
  267. if (!empty($order)) {
  268. $query->orderBy($order);
  269. }
  270. if (!empty($with)) {
  271. $query->with($with);
  272. }
  273. $list = $query->offset($offset)->limit($pageSize)->asArray()->all();
  274. return ['count' => $count, 'list' => $list];
  275. }
  276. //没有分页
  277. public function getAllList($field, $where, $order = '', $with = '')
  278. {
  279. $query = $this->conditionQuery($where)->select($field);
  280. if (!empty($order)) {
  281. $query->orderBy($order);
  282. }
  283. if (!empty($with)) {
  284. $query->with($with);
  285. }
  286. $list = $query->asArray()->all();
  287. return $list;
  288. }
  289. //取后台的列表与分页 shish 2019.8.18
  290. public function getBackendList($field, $where, $page, $pageSize, $order = '', $with = '')
  291. {
  292. $offset = ($page - 1) * $pageSize;
  293. $query = $this->conditionQuery($where)->select($field);
  294. $cloneQuery = clone $query;
  295. $count = $cloneQuery->count();
  296. if (!empty($order)) {
  297. $query->orderBy($order);
  298. }
  299. if (!empty($with)) {
  300. $query->with($with);
  301. }
  302. $pages = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
  303. $list = $query->offset($offset)->limit($pageSize)->asArray()->all();
  304. return ['list' => $list, 'pages' => $pages];
  305. }
  306. }