Base.php 7.8 KB

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