/** * @return ActiveDataProvider */ public function run($id = null) { $model = $id === null ? null : $this->findModel($id); if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $model); } $searchModel = new ActionSearch(); if ($searchModel->load(\Yii::$app->request->getQueryParams())) { $searchModel->validate(); } $dataProvider = $this->prepareDataProvider($model, $searchModel); if (\Yii::$app->response->format === \yii\web\Response::FORMAT_HTML) { return $this->controller->render($this->viewName, ['model' => $model, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]); } return $dataProvider; }
/** * @param string $modelClass * @param ActiveRecord $model * @param ActionSearch $searchModel * @param array $tablesMap * @return SqlDataProvider */ private static function getKeysDataprovider($modelClass, $model, $searchModel, $tablesMap) { /** @var \yii\db\ActiveRecord $staticModel */ $staticModel = new $modelClass(); /** @var TrackableBehavior $behavior */ $behavior = $staticModel->getBehavior('trackable'); $conditions = ['AND', 'a.statement_only = FALSE', ['IN', '(a.relation_id::regclass)', array_keys($tablesMap['related'])]]; $params = []; if ($model !== null) { $conditions[] = "a.row_data @> (:key)::jsonb"; $params[':key'] = json_encode($model->getPrimaryKey(true)); } if ($searchModel !== null) { list($conditions, $params) = $searchModel->getConditions($conditions, $params, $tablesMap['all']); } $subQuery = (new Query())->select(['key_type' => new Expression("'c'"), 'id' => 'a.changeset_id', 'action_date' => 'MAX(a.action_date)'])->from($behavior->auditTableName . ' a')->where($conditions)->andWhere("key_type = 'c'")->groupBy('changeset_id')->union((new Query())->select(['key_type' => new Expression("'t'"), 'id' => 'a.transaction_id', 'action_date' => 'MAX(a.action_date)'])->from($behavior->auditTableName . ' a')->where($conditions)->andWhere("key_type = 't'")->groupBy('transaction_id'), true)->union((new Query())->select(['key_type' => new Expression("'a'"), 'id' => 'a.action_id', 'action_date' => 'a.action_date'])->from($behavior->auditTableName . ' a')->where($conditions)->andWhere("key_type = 'a'"), true); $query = (new Query())->select(['key_type', 'id'])->from(['a' => $subQuery]); $query->addParams($params); $countQuery = clone $query; $countQuery->select(['COUNT(DISTINCT ROW(a.key_type, a.id))']); $countQuery->groupBy([]); $command = $query->orderBy('a.action_date DESC')->createCommand($staticModel->getDb()); return new SqlDataProvider(['sql' => $command->getSql(), 'params' => $command->params, 'totalCount' => $countQuery->scalar($staticModel->getDb()), 'pagination' => ['pageSize' => 20], 'key' => function ($model) { return $model['key_type'] . $model['id']; }]); }