Esempio n. 1
0
 public function addData($name, $data, $type = null)
 {
     if ($this->isNewRecord) {
         return null;
     }
     $auditData = new AuditData();
     $auditData->entry = $this;
     $auditData->name = $name;
     $auditData->data = $data;
     $auditData->type = $type;
     return $auditData->save() ? $auditData : null;
 }
    /**
     * 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();
    }
Esempio n. 3
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();
    }
Esempio n. 4
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();
 }
Esempio n. 5
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();
 }
Esempio n. 6
0
 /**
  * @param int|null $maxAge
  * @return int
  */
 public function cleanup($maxAge = null)
 {
     $maxAge = $maxAge !== null ? $maxAge : $this->maxAge;
     if ($maxAge === null) {
         return false;
     }
     return AuditData::deleteAll('type = :type AND created <= :created', [':type' => $this->id, ':created' => date('Y-m-d 23:59:59', strtotime("-{$maxAge} days"))]);
 }
Esempio n. 7
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();
 }