Exemplo n.º 1
0
    /**
     * Clean up the audit data according to the settings.
     */
    public function actionCleanup()
    {
        /** @var Audit $audit */
        $audit = Yii::$app->getModule('audit');
        if ($audit->maxAge === null) {
            return;
        }
        $entry = AuditEntry::tableName();
        $errors = AuditError::tableName();
        $data = AuditData::tableName();
        $javascript = AuditJavascript::tableName();
        $threshold = time() - $audit->maxAge * 86400;
        AuditEntry::getDb()->createCommand(<<<SQL
DELETE FROM {$entry}, {$errors}, {$data}, {$javascript} USING {$entry}
  INNER JOIN {$errors} ON {$errors}.entry_id = {$entry}.id
  INNER JOIN {$data} ON {$data}.entry_id = {$entry}.id
  INNER JOIN {$javascript} ON {$javascript}.entry_id = {$entry}.id
  WHERE {$entry}.created < FROM_UNIXTIME({$threshold})
SQL
)->execute();
    }
Exemplo n.º 2
0
    /**
     * Clean up the audit data according to the settings.
     * Can be handy if you are offloading the data somewhere and want to keep only the most recent entries readily available
     */
    public function truncate()
    {
        if ($this->maxAge === null) {
            return;
        }
        $entry = models\AuditEntry::tableName();
        $errors = models\AuditError::tableName();
        $data = models\AuditData::tableName();
        $javascript = models\AuditJavascript::tableName();
        $threshold = time() - $this->maxAge * 86400;
        models\AuditEntry::getDb()->createCommand(<<<SQL
DELETE FROM {$entry}, {$errors}, {$data}, {$javascript} USING {$entry}
  INNER JOIN {$errors} ON {$errors}.audit_id = {$entry}.id
  INNER JOIN {$data} ON {$data}.audit_id = {$entry}.id
  INNER JOIN {$javascript} ON {$javascript}.audit_id = {$entry}.id
  WHERE {$entry}.created < FROM_UNIXTIME({$threshold})
SQL
)->execute();
    }
Exemplo n.º 3
0
 public function addBatchData($batchData)
 {
     $columns = ['entry_id', 'type', 'data', 'packed'];
     $rows = [];
     foreach ($batchData as $type => $data) {
         $rows[] = [$this->id, $type, Helper::serialize($data), 1];
     }
     Yii::$app->db->createCommand()->batchInsert(AuditData::tableName(), $columns, $rows)->execute();
 }
Exemplo n.º 4
0
 public function addData($type, $data, $compact = true)
 {
     $record = ['entry_id' => $this->id, 'type' => $type, 'data' => Helper::serialize($data, $compact)];
     static::getDb()->createCommand()->insert(AuditData::tableName(), $record)->execute();
 }
Exemplo n.º 5
0
 /**
  * @param $type
  * @param $data
  * @param bool|true $compact
  * @throws \yii\db\Exception
  */
 public function addData($type, $data, $compact = true)
 {
     // Make sure to mark data as a large object so it gets escaped
     $record = ['entry_id' => $this->id, 'type' => $type, 'data' => [Helper::serialize($data, $compact), \PDO::PARAM_LOB]];
     static::getDb()->createCommand()->insert(AuditData::tableName(), $record)->execute();
 }