AR.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\helper;
  10. use fecshop\services\Service;
  11. use Yii;
  12. use yii\db\ActiveQuery;
  13. /**
  14. * AR services.(Active Record)
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class AR extends Service
  19. {
  20. public $numPerPage = 20;
  21. public $pageNum = 1;
  22. /**
  23. * example filter:
  24. * [
  25. * 'numPerPage' => 20,
  26. * 'pageNum' => 1,
  27. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  28. * 'where' => [
  29. * ['>','price',1],
  30. * ['<=','price',10]
  31. * ['sku' => 'uk10001'],
  32. * ],
  33. * 'asArray' => true,
  34. * 'fetchAll' => false, // 是否获取所有数据,如果为true,则传递的numPerPage和pageNum将会无效。
  35. * ]
  36. * 查询方面使用的函数,根据传递的参数,进行query
  37. * @return ActiveQuery
  38. */
  39. public function getCollByFilter($query, $filter)
  40. {
  41. $fetchAll = isset($filter['fetchAll']) ? $filter['fetchAll'] : false;
  42. $select = isset($filter['select']) ? $filter['select'] : '';
  43. $asArray = isset($filter['asArray']) ? $filter['asArray'] : true;
  44. $numPerPage = isset($filter['numPerPage']) ? $filter['numPerPage'] : $this->numPerPage;
  45. $pageNum = isset($filter['pageNum']) ? $filter['pageNum'] : $this->pageNum;
  46. $orderBy = isset($filter['orderBy']) ? $filter['orderBy'] : '';
  47. $where = isset($filter['where']) ? $filter['where'] : '';
  48. if ($asArray) {
  49. $query->asArray();
  50. }
  51. if (is_array($select) && !empty($select)) {
  52. $query->select($select);
  53. }
  54. if ($where) {
  55. if (is_array($where)) {
  56. $i = 0;
  57. foreach ($where as $w) {
  58. $i++;
  59. if ($i == 1) {
  60. $query->where($w);
  61. } else {
  62. $query->andWhere($w);
  63. }
  64. }
  65. }
  66. }
  67. if (!$fetchAll) {
  68. $offset = ($pageNum - 1) * $numPerPage;
  69. $query->limit($numPerPage)->offset($offset);
  70. }
  71. if ($orderBy) {
  72. $query->orderBy($orderBy);
  73. }
  74. return $query;
  75. }
  76. /**
  77. * @param $model | Object , 数据库model
  78. * @param $one | Array , 数据数组,对model进行赋值
  79. * 通过循环的方式,对$model对象的属性进行赋值。
  80. * 并保存,保存成功后,返回保存后的model对象
  81. */
  82. public function save($model, $one, $serialize = false)
  83. {
  84. if (!$model) {
  85. Yii::$service->helper->errors->add('ActiveRecord Save Error: $model is empty');
  86. return false;
  87. }
  88. $attributes = $model->attributes();
  89. if (is_array($attributes) && !empty($attributes)) {
  90. foreach ($attributes as $attr) {
  91. if (isset($one[$attr])) {
  92. if ($serialize && is_array($one[$attr])) {
  93. $model[$attr] = serialize($one[$attr]);
  94. } else {
  95. $model[$attr] = $one[$attr];
  96. }
  97. }
  98. }
  99. if ($model->save()) {
  100. return $model;
  101. } else {
  102. Yii::$service->helper->errors->add('model save fail');
  103. return false;
  104. }
  105. } else {
  106. Yii::$service->helper->errors->add('$attribute is empty or is not array');
  107. return false;
  108. }
  109. }
  110. }