MongoActiveRecord.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace common\components;
  3. use common\helpers\Tool;
  4. use Yii;
  5. use yii\data\Pagination;
  6. /**
  7. * Base active record class for models
  8. * @package common\components
  9. */
  10. class MongoActiveRecord extends \yii\mongodb\ActiveRecord {
  11. /**
  12. * Query对象
  13. * @var null
  14. */
  15. public static $query = null;
  16. /**
  17. * @var array
  18. */
  19. public static $argv = [];
  20. /**
  21. * 构造器
  22. *
  23. * @access public
  24. */
  25. public function init(){
  26. parent::init();
  27. }
  28. /**
  29. * 批量写入数据
  30. * @param $field
  31. * @param $data
  32. * @return mixed
  33. */
  34. public static function batchInsert($field, $data){
  35. $db = static::getDb();
  36. $query = $db->createCommand()->batchInsert(static::collectionName(), $field, $data);
  37. return $query->execute();
  38. }
  39. /**
  40. * @param string $condition
  41. * @param array $argv
  42. * @throws \yii\base\InvalidConfigException
  43. */
  44. public static function prepare($condition = [], $argv = []){
  45. $default = [
  46. 'page' => null,
  47. 'pageSize'=>0, //10
  48. 'orderBy'=>'_id desc',
  49. 'select'=>null,
  50. 'asArray'=>true,
  51. 'validatePage'=>true,
  52. 'filter' => null,
  53. 'indexBy' => null,
  54. 'count' => '*',
  55. ];
  56. self::$argv = Tool::deepParse($argv ,$default);
  57. if (!self::$argv['pageSize']) {
  58. $pageSize = \Yii::$app->request->all('pageSize', \Yii::$app->params['pageSize']);
  59. self::$argv['pageSize'] = $pageSize;
  60. }
  61. self::$query = static::find();
  62. if(self::$argv['select']){
  63. self::$query->select(self::$argv['select']);
  64. }
  65. if(self::$argv['indexBy']){
  66. self::$query->indexBy(self::$argv['indexBy']);
  67. }
  68. if(self::$argv['asArray']){
  69. self::$query->asArray();
  70. }
  71. self::$query->orderBy(self::$argv['orderBy'])->where($condition);
  72. unset($condition, $argv, $default);
  73. }
  74. /**
  75. * 获取列表
  76. * @param array $condition
  77. * @param array $argv
  78. * @return array
  79. * @throws \yii\base\InvalidConfigException
  80. */
  81. public static function lists($condition = [], $argv = []) {
  82. self::prepare($condition, $argv);
  83. $countQuery = clone self::$query;
  84. $count = $countQuery->count(self::$argv['count']); // 得到总数
  85. $pagination = new Pagination(['totalCount' => $count]);
  86. $pagination->setPageSize(self::$argv['pageSize']);
  87. if(self::$argv['page'] !== null){
  88. $pagination->setPage(self::$argv['page']);
  89. }
  90. self::$query->offset($pagination->offset)->limit($pagination->limit);
  91. if(self::$argv['filter']){
  92. $lists = self::$query->filter(self::$argv['filter'])->all();
  93. }else{
  94. $lists = self::$query->all();
  95. }
  96. self::$query = null;
  97. unset($pageParams, $countQuery);
  98. $startNum = $pagination->page * $pagination->pageSize + 1;
  99. return [
  100. 'list' => $lists ? $lists : [],
  101. 'pagination' => $pagination,
  102. 'currentPage'=>$pagination->page,
  103. 'totalPages'=>$pagination->pageCount,
  104. 'startNum' => $startNum,
  105. 'totalCount' => $pagination->totalCount,
  106. 'pageSize' => $pagination->pageSize,
  107. ];
  108. }
  109. }