gtt / doctrine-auditable-bundle
Doctrine 可审计包是 DoctrineExtensions Loggable 的替代方案
5.0.0
2022-04-04 14:53 UTC
Requires
- php: ~8.0
- doctrine/doctrine-bundle: ~2.0
- doctrine/orm: ~2.10
- symfony/config: ~5.4 || ~6.0
- symfony/dependency-injection: ~5.4 || ~6.0
- symfony/http-kernel: ~5.4 || ~6.0
- symfony/property-access: ~5.4 || ~6.0
Requires (Dev)
- mikey179/vfsstream: ^1.6.10
- phpunit/phpunit: ^9.5
- symfony/framework-bundle: ~5.4 || ~6.0
- symfony/phpunit-bridge: ^6.0
- symfony/proxy-manager-bridge: ~5.4 || ~6.0
- symfony/security-bundle: ~5.4 || ~6.0
Suggests
- symfony/security-core: For logging user made change
README
可审计行为实现有助于跟踪对象的变化和历史。这是一个 快速 和 轻量级 的 DoctrineExtensions Loggable 替代方案,具有一些功能。仅支持 ORM。
功能
- 变化组
- 变化注释
- 方便的存储(在单独的列中存储更改前后的跟踪值,而不是 Loggable 中的序列化实体数据)
- 支持自定义 DBAL 类型
- 支持类继承配置
安装
- 安装包
composer require "gtt/doctrine-auditable-bundle"
- 添加到 Kernel.php
public function registerBundles() { $bundles = array( ... new Gtt\Bundle\DoctrineAuditableBundle\DoctrineAuditableBundle(), ); ... }
- 为存储更改创建表
bin/console doctrine:schema:update --force
- 如有必要,配置映射。
使用
为跟踪属性添加属性
<?php use Gtt\Bundle\DoctrineAuditableBundle\Mapping\Annotation as Auditable; /** * My entity */ #[ORM\Entity] #[ORM\Table(name: 'entity')] #[Auditable\Entity] class Entity { #[ORM\Column(name: 'assigned_user', type: 'string', length: 255)] #[Auditable\Property] protected string $assignedUser; ... }
然后在某个服务中更改实体属性并提交更改。
<?php use Doctrine\ORM\EntityManagerInterface; use Gtt\Bundle\DoctrineAuditableBundle as Auditable; class PayloadService { private Auditable\Log\Store $auditable; private EntityManagerInterface $entityManager; /** * Operate! */ public function payloadMethod(YourDomain\Entity $entity): void { // 1. change some property that supposed to be logged to changelog $entity->updateProperty(); // ... just dummy example // 2. describe this change $this->auditable->describe($entity, 'Change description'); // 3. perform update $this->entityManager->flush(); } }