Beispiel #1
0
 /**
  * @param string $modelClass
  * @param array $keys each item has two keys: key_type and id
  * @param array $tablesMap maps fully qualified table names to model classes
  * @return Action[] indexed by concatenated key_type and id from keys
  */
 public static function getChanges($modelClass, $keys, $tablesMap)
 {
     /** @var \yii\db\ActiveRecord $staticModel */
     $staticModel = new $modelClass();
     /** @var TrackableBehavior $behavior */
     $behavior = $staticModel->getBehavior('trackable');
     $idExpr = "(CASE a.key_type WHEN 'c' THEN a.changeset_id WHEN 't' THEN a.transaction_id ELSE a.action_id END)";
     $rows = (new Query())->select(['*', "{$idExpr} AS id"])->from($behavior->auditTableName . ' a')->leftJoin($behavior->changesetTableName . ' c', 'c.id = a.changeset_id')->where(['AND', 'a.statement_only = FALSE', ['OR', "a.key_type != 't'", ['IN', '(a.relation_id::regclass)', array_keys($tablesMap)]], ['IN', ['a.key_type', $idExpr], array_map(function ($model) use($idExpr) {
         return ['a.key_type' => $model['key_type'], $idExpr => $model['id']];
     }, $keys)]])->orderBy('a.action_date DESC')->all($staticModel->getDb());
     $models = [];
     foreach ($rows as $row) {
         if (!isset($models[$row['key_type'] . $row['id']])) {
             $models[$row['key_type'] . $row['id']] = ['key_type' => $row['key_type'], 'id' => $row['id'], 'actions' => []];
         }
         $action = new Action();
         $action->setRowData($row, $tablesMap);
         $models[$row['key_type'] . $row['id']]['actions'][$action->action_id] = $action;
     }
     return $models;
 }
 /**
  * Prepares the data provider that should return the requested collection of the models.
  * @param ActiveRecord $model
  * @param \nineinchnick\audit\models\ActionSearch $searchModel
  * @return DataProviderInterface
  */
 protected function prepareDataProvider($model, $searchModel)
 {
     if ($this->prepareDataProvider !== null) {
         return call_user_func($this->prepareDataProvider, $this, $model, $searchModel);
     }
     return Action::getDataProvider($this->modelClass, $this->getTablesMap(), $model, $searchModel);
 }