Ejemplo 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();
    }
Ejemplo 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();
    }