| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace common\components;
- use common\helpers\Tool;
- use Yii;
- use yii\data\Pagination;
- /**
- * Base active record class for models
- * @package common\components
- */
- class MongoActiveRecord extends \yii\mongodb\ActiveRecord {
- /**
- * Query对象
- * @var null
- */
- public static $query = null;
- /**
- * @var array
- */
- public static $argv = [];
- /**
- * 构造器
- *
- * @access public
- */
- public function init(){
- parent::init();
- }
- /**
- * 批量写入数据
- * @param $field
- * @param $data
- * @return mixed
- */
- public static function batchInsert($field, $data){
- $db = static::getDb();
- $query = $db->createCommand()->batchInsert(static::collectionName(), $field, $data);
- return $query->execute();
- }
- /**
- * @param string $condition
- * @param array $argv
- * @throws \yii\base\InvalidConfigException
- */
- public static function prepare($condition = [], $argv = []){
- $default = [
- 'page' => null,
- 'pageSize'=>0, //10
- 'orderBy'=>'_id desc',
- 'select'=>null,
- 'asArray'=>true,
- 'validatePage'=>true,
- 'filter' => null,
- 'indexBy' => null,
- 'count' => '*',
- ];
- self::$argv = Tool::deepParse($argv ,$default);
- if (!self::$argv['pageSize']) {
- $pageSize = \Yii::$app->request->all('pageSize', \Yii::$app->params['pageSize']);
- self::$argv['pageSize'] = $pageSize;
- }
- self::$query = static::find();
- if(self::$argv['select']){
- self::$query->select(self::$argv['select']);
- }
- if(self::$argv['indexBy']){
- self::$query->indexBy(self::$argv['indexBy']);
- }
- if(self::$argv['asArray']){
- self::$query->asArray();
- }
- self::$query->orderBy(self::$argv['orderBy'])->where($condition);
- unset($condition, $argv, $default);
- }
- /**
- * 获取列表
- * @param array $condition
- * @param array $argv
- * @return array
- * @throws \yii\base\InvalidConfigException
- */
- public static function lists($condition = [], $argv = []) {
- self::prepare($condition, $argv);
- $countQuery = clone self::$query;
- $count = $countQuery->count(self::$argv['count']); // 得到总数
- $pagination = new Pagination(['totalCount' => $count]);
- $pagination->setPageSize(self::$argv['pageSize']);
- if(self::$argv['page'] !== null){
- $pagination->setPage(self::$argv['page']);
- }
- self::$query->offset($pagination->offset)->limit($pagination->limit);
- if(self::$argv['filter']){
- $lists = self::$query->filter(self::$argv['filter'])->all();
- }else{
- $lists = self::$query->all();
- }
- self::$query = null;
- unset($pageParams, $countQuery);
- $startNum = $pagination->page * $pagination->pageSize + 1;
- return [
- 'list' => $lists ? $lists : [],
- 'pagination' => $pagination,
- 'currentPage'=>$pagination->page,
- 'totalPages'=>$pagination->pageCount,
- 'startNum' => $startNum,
- 'totalCount' => $pagination->totalCount,
- 'pageSize' => $pagination->pageSize,
- ];
- }
- }
|