Base.php 10 KB

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