/**
  * Creates the data-provider for searching audit trails
  *
  * @param mixed $params the params as used by yiis search methods
  * @param \yii\db\ActiveRecord $subject the model to get the audit trail entries for
  * @return \yii\data\ActiveDataProvider
  */
 public function search($params, $subject = null)
 {
     /* @var $query \asinfotrack\yii2\audittrail\models\AuditTrailEntryQuery */
     //prepare data provider
     $query = AuditTrailEntry::find();
     if ($subject !== null) {
         $query->subject($subject)->orderNewestFirst(true);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     //if no query data, return it
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     //apply filtering
     $query->andFilterWhere(['id' => $this->id, 'happened_at' => $this->happened_at, 'user_id' => $this->user_id, 'type' => $this->type]);
     $query->andFilterWhere(['like', 'foreign_pk', $this->foreign_pk])->andFilterWhere(['like', 'data', $this->data]);
     return $dataProvider;
 }
 /**
  * Saves the entry and outputs an exception describing the problem if necessary
  *
  * @param \asinfotrack\yii2\audittrail\models\AuditTrailEntry $entry
  * @throws InvalidValueException if entry couldn't be saved (validation error)
  */
 protected static function saveEntry($entry)
 {
     //do nothing if successful
     if ($entry->save()) {
         return;
     }
     //otherwise throw exception
     $lines = [];
     foreach ($entry->errors as $attr => $errors) {
         foreach ($errors as $err) {
             $lines[] = $err;
         }
     }
     throw new InvalidValueException(sprintf('Error while saving audit-trail-entry: %s', implode(', ', $lines)));
 }