/** * Clean up the audit data according to the settings. */ public function actionCleanup() { $audit = Audit::getInstance(); if ($audit->maxAge === null) { return; } $entry = AuditEntry::tableName(); $errors = AuditError::tableName(); $data = AuditData::tableName(); $javascript = AuditJavascript::tableName(); $trail = AuditTrail::tableName(); $threshold = time() - $audit->maxAge * 86400; AuditEntry::getDb()->createCommand(<<<SQL DELETE FROM {$entry}, {$errors}, {$data}, {$javascript}, {$trail} 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 INNER JOIN {$trail} ON {$trail}.entry_id = {$entry}.id WHERE {$entry}.created < FROM_UNIXTIME({$threshold}) SQL )->execute(); }
/** * Save the audit trails for a delete action */ protected function saveAuditTrailDelete() { $audit = Audit::getInstance(); $audit->getDb()->createCommand()->insert(AuditTrail::tableName(), ['action' => 'DELETE', 'entry_id' => $this->getAuditEntryId(), 'user_id' => $this->getUserId(), 'model' => $this->owner->className(), 'model_id' => $this->getNormalizedPk(), 'created' => date($this->dateFormat)])->execute(); }
/** * @param $action * @param $newAttributes * @param array $oldAttributes * @throws \yii\db\Exception */ public function auditAttributes($action, $newAttributes, $oldAttributes = []) { // If we are not active the get out of here if (!$this->active) { return; } // Setup values outside loop $audit_id = $this->getAuditEntryId(); $user_id = $this->getUserId(); $model = $this->owner->className(); $model_id = $this->getNormalizedPk(); $stamp = date($this->dateFormat); // Build a list of fields to log $rows = array(); foreach ($newAttributes as $name => $new) { $old = isset($oldAttributes[$name]) ? $oldAttributes[$name] : ''; // If we are skipping nulls then lets see if both sides are null if ($this->skipNulls && empty($old) && empty($new)) { continue; } // If they are not the same lets write an audit log if ($new != $old) { $rows[] = [$audit_id, $user_id, $old, $new, $action, $model, $model_id, $name, $stamp]; } } // Record the field changes with a batch insert if ($rows) { $columns = ['audit_id', 'user_id', 'old_value', 'new_value', 'action', 'model', 'model_id', 'field', 'stamp']; Yii::$app->db->createCommand()->batchInsert(AuditTrail::tableName(), $columns, $rows)->execute(); } }
/** * @inheritDoc */ protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) { // Build a list of fields to log $rows = array(); $fields = array(); //字段 $newVal = array(); //新值 $oldVal = array(); //旧值 foreach ($newAttributes as $field => $new) { $old = isset($oldAttributes[$field]) ? $oldAttributes[$field] : ''; // If they are not the same lets write an audit log if ($new != $old) { $fields[] = $field; $newVal[] = $new; $oldVal[] = $old; $rows[] = [$entry_id, $user_id, $old, $new, $action, $model, $model_id, $field, $created]; } } //保存一条单独的记录 $rows[] = [$entry_id, $user_id, Json::encode($oldVal), Json::encode($newVal), $action, $model, $model_id, Json::encode($fields), $created]; // Record the field changes with a batch insert if (!empty($rows)) { $columns = ['entry_id', 'user_id', 'old_value', 'new_value', 'action', 'model', 'model_id', 'field', 'created']; $audit = Audit::getInstance(); $audit->getDb()->createCommand()->batchInsert(AuditTrail::tableName(), $columns, $rows)->execute(); } }