tourze/doctrine-track-bundle
最新稳定版本:1.1.0
Composer 安装命令:
composer require tourze/doctrine-track-bundle
包简介
Doctrine 实体变更跟踪和日志记录 Bundle
README 文档
README
A Symfony bundle for tracking and auditing entity changes with automatic logging of create, update, and delete operations to help maintain data integrity and compliance requirements.
Table of Contents
- Features
- Dependencies
- Required Bundles
- Installation
- Quick Start
- Configuration
- Advanced Usage
- Contributing
- License
- Changelog
Features
- Selective Tracking: Only tracks fields explicitly marked with
#[TrackColumn]attribute - Complete Audit Trail: Records entity class, ID, action type, changed data, user, IP, and request ID
- Asynchronous Logging: Non-blocking log persistence for better performance
- Automatic Cleanup: Built-in scheduled cleanup with configurable retention period
- User Context: Automatically captures user information when available
- Request Tracking: Associates changes with request IDs for better debugging
Dependencies
- PHP >= 8.1
- Symfony >= 6.4
- Doctrine ORM >= 3.0
- Doctrine Bundle >= 2.13
Required Bundles
tourze/doctrine-async-insert-bundle: For asynchronous log insertiontourze/request-id-bundle: For request ID trackingtourze/doctrine-user-bundle: For user context trackingtourze/doctrine-ip-bundle: For IP address trackingtourze/doctrine-timestamp-bundle: For timestamp management
Installation
Install via Composer:
composer require tourze/doctrine-track-bundle
If you're not using Symfony Flex, manually register the bundle in config/bundles.php:
return [ // ... Tourze\DoctrineTrackBundle\DoctrineTrackBundle::class => ['all' => true], ];
Run database migrations to create the tracking table:
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
Quick Start
- Mark fields to track with the
#[TrackColumn]attribute:
<?php use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrineTrackBundle\Attribute\TrackColumn; #[ORM\Entity] class User { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[TrackColumn] // This field will be tracked #[ORM\Column] private string $email; #[TrackColumn] // This field will be tracked #[ORM\Column] private string $username; #[ORM\Column] // This field will NOT be tracked private \DateTime $lastLogin; // ... getters and setters }
- Use your entities normally - tracking happens automatically:
// Create $user = new User(); $user->setEmail('user@example.com'); $user->setUsername('john_doe'); $entityManager->persist($user); $entityManager->flush(); // Logs: action='create', data=['email' => 'user@example.com', 'username' => 'john_doe'] // Update $user->setEmail('newemail@example.com'); $entityManager->flush(); // Logs: action='update', data=['email' => ['old' => 'user@example.com', 'new' => 'newemail@example.com']] // Delete $entityManager->remove($user); $entityManager->flush(); // Logs: action='remove', data=['email' => 'newemail@example.com', 'username' => 'john_doe']
- Query tracking logs:
use Tourze\DoctrineTrackBundle\Entity\EntityTrackLog; // Find all changes to a specific entity $logs = $entityManager->getRepository(EntityTrackLog::class)->findBy([ 'objectClass' => User::class, 'objectId' => '123' ]); // Find all changes by a specific action $createLogs = $entityManager->getRepository(EntityTrackLog::class)->findBy([ 'action' => 'create' ]);
Configuration
Environment Variables
# Log retention period (default: 180 days) ENTITY_TRACK_LOG_PERSIST_DAY_NUM=180
Service Configuration
No additional configuration is required. The bundle automatically:
- Registers Doctrine event listeners
- Configures async logging service
- Sets up cleanup scheduling
Advanced Usage
Custom Entity Requirements
Your entities must have an id property as the primary key for tracking to work:
#[ORM\Entity] class MyEntity { #[ORM\Id] // Required for tracking #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; // ... other properties }
Understanding Log Data
The tracking logs contain:
objectClass: Full class name of the tracked entityobjectId: Primary key value of the entityaction:create,update, orremovedata: JSON containing field changescreatedBy: User who made the change (if available)createdFromIp: IP address of the requestrequestId: Unique request identifiercreatedAt: Timestamp of the change
Performance Considerations
- Logs are written asynchronously to avoid blocking entity operations
- Only marked fields are tracked, reducing overhead
- Automatic cleanup prevents log table growth
- Consider indexing on
objectClassandobjectIdfor query performance
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Make your changes and add tests
- Ensure all tests pass (
./vendor/bin/phpunit) - Check code quality (
./vendor/bin/phpstan analyse) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
The MIT License (MIT). Please see License File for more information.
Changelog
Please see Releases for version history and upgrade guides.
统计信息
- 总下载量: 27.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 77
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-29