rasuvaeff/yii3-audit-log-db 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rasuvaeff/yii3-audit-log-db

Composer 安装命令:

composer require rasuvaeff/yii3-audit-log-db

包简介

Database-backed audit writer for rasuvaeff/yii3-audit-log

README 文档

README

Stable Version Total Downloads Build Static analysis Psalm Level PHP License

Database-backed AuditWriter for rasuvaeff/yii3-audit-log. Persists audit events to any yiisoft/db-compatible database (SQLite, MySQL, PostgreSQL, etc.).

Using an AI coding assistant? llms.txt has a compact API reference you can use.

Requirements

  • PHP 8.3+
  • rasuvaeff/yii3-audit-log ^1.0
  • yiisoft/db ^2.0

Installation

composer require rasuvaeff/yii3-audit-log rasuvaeff/yii3-audit-log-db

Migration

Run the bundled migration to create the audit_log table:

// Register the migration in your migration runner:
// migrations/M260620000000CreateAuditLogTable.php
// from vendor/rasuvaeff/yii3-audit-log-db/migrations/

The migration creates:

Column Type Notes
id VARCHAR(32) PK 32-char hex from AuditLogger
actor_type VARCHAR(32) user / system
actor_id VARCHAR(255) NULL null for system actor
actor_name VARCHAR(255) NULL display name, optional
action VARCHAR(64) create / update / delete / custom
subject_type VARCHAR(255) entity type, e.g. order
subject_id VARCHAR(255) entity id
changes TEXT JSON array of {field, old, new} objects
occurred_at VARCHAR(30) Y-m-d H:i:s UTC
request_id VARCHAR(255) NULL from AuditMetadata
ip VARCHAR(45) NULL from AuditMetadata
user_agent TEXT NULL from AuditMetadata

Indexes: (subject_type, subject_id, occurred_at), (actor_type, actor_id, occurred_at), (occurred_at).

Yii3 config-plugin

Install rasuvaeff/yii3-audit-log (core) and rasuvaeff/yii3-audit-log-db (adapter). The adapter's config/di.php automatically binds AuditWriter to DbAuditWriter.

The core's config/di.php wires AuditLogger. You only need to bind ClockInterface in your application config:

// config/common/di/clock.php
use Psr\Clock\ClockInterface;

return [
    ClockInterface::class => MySystemClock::class,
];

Custom table name via params:

// config/common/params.php
return [
    'rasuvaeff/yii3-audit-log-db' => [
        'table' => 'my_audit_log',
    ],
];

Usage

Write via AuditLogger

use Rasuvaeff\Yii3AuditLog\AuditActor;
use Rasuvaeff\Yii3AuditLog\AuditChangeSet;
use Rasuvaeff\Yii3AuditLog\AuditLogger;
use Rasuvaeff\Yii3AuditLog\AuditSubject;

// Injected via DI:
/** @var AuditLogger $logger */

$logger->logChange(
    actor: AuditActor::user(id: (string) $user->id, name: $user->name),
    subject: AuditSubject::of(type: 'order', id: (string) $order->id),
    changes: AuditChangeSet::fromArrays(old: $before, new: $after),
);

Use directly

use Rasuvaeff\Yii3AuditLogDb\DbAuditWriter;

$writer = new DbAuditWriter(db: $db, table: 'audit_log');
$writer->write(event: $auditEvent);

Security

  • DbAuditWriter validates the table name against /^[A-Za-z_]\w*(\.[A-Za-z_]\w*)?$/ — schema-qualified names like public.audit_log are allowed; arbitrary strings are rejected.
  • All event field values are passed as bound parameters via yiisoft/db — no SQL injection risk.
  • changes values are whatever AuditChangeSet contains. Apply SensitiveValueMasker in AuditLogger before this writer runs (default in core DI).

Examples

See examples/ for runnable scripts.

Development

# from monorepo root (/home/rasuvaeff/projects/rasuvaeff)
make -C yii3-audit-log-db install
make -C yii3-audit-log-db build
make -C yii3-audit-log-db cs-fix
make -C yii3-audit-log-db test

Or with Docker directly:

docker run --rm -v "$PWD":/repo -w /repo/yii3-audit-log-db composer:2 composer build

License

BSD-3-Clause. See LICENSE.md.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-06-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固