gtt/doctrine-auditable-bundle

Doctrine 可审计包是 DoctrineExtensions Loggable 的替代方案

5.0.0 2022-04-04 14:53 UTC

README

可审计行为实现有助于跟踪对象的变化和历史。这是一个 快速轻量级 的 DoctrineExtensions Loggable 替代方案,具有一些功能。仅支持 ORM。

功能

  • 变化组
  • 变化注释
  • 方便的存储(在单独的列中存储更改前后的跟踪值,而不是 Loggable 中的序列化实体数据)
  • 支持自定义 DBAL 类型
  • 支持类继承配置

安装

  1. 安装包
composer require "gtt/doctrine-auditable-bundle"
  1. 添加到 Kernel.php
public function registerBundles()
{
    $bundles = array(
        ...
        new Gtt\Bundle\DoctrineAuditableBundle\DoctrineAuditableBundle(),
    );
    ...
}
  1. 为存储更改创建表
bin/console doctrine:schema:update --force
  1. 如有必要,配置映射。

使用

为跟踪属性添加属性

<?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();
    }
}