码亚 / audit-bundle
使用Serializer组件记录Doctrine实体更改日志
v0.0.5
2023-11-15 15:20 UTC
Requires
- php: >=8.1.0
- ext-zlib: *
- knplabs/knp-paginator-bundle: ^5.0
- swaggest/json-diff: ^3.10
- symfony/flex: ^1.0 || ^2.0
- symfony/framework-bundle: ^5.0 || ^6.0
- symfony/orm-pack: ^1.0 || 2.0
- symfony/serializer: ^5.0 || ^6.0
README
Codyas Audit是一个允许跟踪Doctrine实体更改的Symfony Bundle。它专门设计用于跟踪一个或多个主类的更改,这样当检测到任何相关(之前已配置)实体的更改时,将从前置类进行序列化,并在数据库中创建修订版本。
特性
- 使用Doctrine生命周期订阅者检测和处理更改。
- 允许自定义实体序列化,审计触发后存储在修订索引中。
- 计算审计之间的差异。
- 使用压缩存储修订。
- 在请求完成后并发送响应给用户后计算更改。
需求
- PHP >= 8.1
- Symfony >= 5.0
安装
首先,您需要允许通过Flex配置bundle的额外贡献,在项目根目录中运行以下命令
composer config extra.symfony.allow-contrib true
然后安装bundle
composer require codyas/audit-bundle
配置
大多数项目配置应通过Flex完成。如果由于任何原因配置未自动完成,请手动配置bundle。
注册bundle
# config/bundles.php return [ // ... Codyas\Audit\AuditBundle::class => ['all' => true], // ... ];
配置bundle
# config/packages/audit.yaml audit: doctrine: ## Set the manager to use for the audits. If a non default manager/connection is preferred, you need to configure the ## Doctrine DBAL and ORM mappings settings to tell the bundle to use the desired manager/connection. manager: "default" serialization: ## You can customize the group name you want to use for serialize the revision. Defaults to "audit" group_name: "audit"
注册Doctrine压缩数据类型
doctrine: dbal: types: compressed_json: Codyas\Audit\Doctrine\DBAL\Types\CompressedJsonType ## ... Adjust the ORM mapping section to your needs orm: entity_managers: default: mappings: AuditBundle: is_bundle: true
概念
- 主实体:当检测到主实体或其相关实体中的数据更改时,用作序列化根的实体。
- 从实体:具有主实体依赖关系的实体。与主类的关联不需要是直接的,因为也可以检测和处理与主实体有任意程度关系的实体中的更改。
- 可审计实体:不遵循主从方法,但您希望记录其数据更改日志的实体。
用法
在主实体中,您必须实现接口AuditableInterface
和MasterAuditableInterface
,以及启用和定义要存储在修订中的字段上的序列化组。默认情况下,序列化组为audit
,但您可以在bundle设置中覆盖此设置。
<?php namespace App\Entity; use Codyas\Audit\Model\AuditableInterface; use Codyas\Audit\Model\MasterAuditableInterface; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; #[ORM\Entity(repositoryClass: AcmeRepository::class)] class Acme implements AuditableInterface, MasterAuditableInterface { } #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] #[Groups("audit")] private ?int $id = null; #[Groups("audit")] #[ORM\OneToMany(mappedBy: 'acmeInstance', targetEntity: AcmeDependant::class)] private Collection $dependants;
在从实体中,您必须配置序列化组并实现AuditableInterface
和SlaveAuditableInterface
。最后,您需要实现getMaster()
和getMasterNamespace()
方法,分别返回主实体实例和命名空间。
<?php namespace App\Entity; use Codyas\Audit\Model\AuditableInterface; use Codyas\Audit\Model\SlaveAuditableInterface; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; #[ORM\Entity(repositoryClass: AcmeDependant::class)] class AcmeDependant implements AuditableInterface, SlaveAuditableInterface { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] #[Groups("audit")] private ?int $id = null; #[ORM\ManyToOne(inversedBy: 'dependants')] private ?Acme $acmeInstance = null; public function getMaster(): ?MasterAuditableInterface { return $this->acmeInstance; } public function getMasterNamespace(): ?string { return Acme::class; }