tourze / doctrine-track-bundle
Doctrine 实体变更跟踪和日志记录 Bundle
Installs: 23 940
Dependents: 45
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-track-bundle
Requires
- doctrine/common: ^3.5
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- psr/log: ^3|^2|^1
- symfony/cache: ^7.3
- symfony/cache-contracts: ^3
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-foundation: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/security-bundle: ^7.3
- symfony/security-core: ^7.3
- symfony/service-contracts: ^3.6
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-async-insert-bundle: 1.0.*
- tourze/doctrine-helper: 0.0.*
- tourze/doctrine-indexed-bundle: 1.0.*
- tourze/doctrine-ip-bundle: 1.0.*
- tourze/doctrine-timestamp-bundle: 1.0.*
- tourze/doctrine-user-bundle: 1.0.*
- tourze/request-id-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.*
- tourze/symfony-schedule-entity-clean-bundle: 1.0.*
Requires (Dev)
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/persistence: ^4.1
- nesbot/carbon: ^2.72 || ^3
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-base: 1.*
- tourze/phpunit-doctrine-entity: 1.*
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
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.