EntityAuditBundle is a free, open source compliance & risk management project written in PHP and released under MIT. It has 643 GitHub stars, 257 forks and 5 open issues, and was last pushed 5 months ago. On this registry it ranks #43 of 45 tracked projects in Compliance & Risk Management, with 5 head-to-head comparisons available.

What is EntityAuditBundle?

Sonata EntityAuditBundle is a Symfony bundle that adds full versioning and audit trails to Doctrine 2 entities and their associations, and it is aimed at PHP teams that need a durable, queryable history of database changes without hand-writing that history themselves.

What it is

EntityAuditBundle is an extension for Doctrine 2, the object-relational mapper used across the Symfony ecosystem. It is inspired by Hibernate Envers, the auditing library long used in the Java persistence world, and it brings the same idea to PHP: every audited entity gets a full revision history rather than only its current state. The bundle hooks into Doctrine's SchemaTool generation process so the necessary DDL statements for audited entities are produced automatically instead of being maintained by hand.

The concrete problem it solves is that a normal ORM table holds only the latest row. Once an UPDATE or DELETE runs, the previous values and the affected associations are gone. EntityAuditBundle addresses this by creating a mirroring table for each audited entity's table, suffixed with _audit. Each mirror row carries every column of the audited entity plus two additional fields: rev, which holds the global revision number generated from a revisions table, and revtype, which records one of INS, UPD or DEL to indicate which database operation produced that revision log entry. The global revision table stores an id, timestamp, username and change comment field, so a revision can be attributed to a person and a moment in time. This approach makes it possible to version an application together with its changes to associations at particular points in time, which is the thing an ordinary timestamp column cannot do.

Key capabilities

  • Creates a mirroring _audit table per audited entity, retaining all original columns.
  • Records rev and revtype on every revision row, with revtype set to INS, UPD or DEL.
  • Maintains a global revisions table holding id, timestamp, username and change comment.
  • Versions changes to entity associations, not only scalar columns.
  • Hooks into Doctrine SchemaTool generation to emit the DDL for audited entities automatically.
  • Configures the audited set through the simple_things_entity_audit extension with an audited_entities list.
  • Supports excluding some entity properties from triggering a revision.

Who uses it and how

  • Symfony and Doctrine 2 applications that must answer who changed a record, when, and to what value.
  • Regulated or compliance-sensitive back ends where a current-state-only table is not an acceptable audit record.
  • Teams migrating concepts already familiar from Hibernate Envers, who want the same revision and revtype model in PHP.
  • Projects that prefer an in-database audit trail, since history lives in _audit and revisions tables alongside the data rather than in a separate service.
  • The Sonata project ecosystem, including deployments that raise questions through the StackOverflow sonata tag and file issues against the contributing guide.

Getting started

Install with Composer using composer require sonata-project/entity-audit-bundle, then enable SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle::class in config/bundles.php and list your entities under audited_entities in config/packages/entity_audit.yaml.

How it compares

The facts name one comparable tool: Hibernate Envers, the Java auditing library this bundle is explicitly inspired by. Teams already using Envers will recognise the revision number and revtype model, while PHP projects that need equivalent behaviour get it without leaving Doctrine 2 or adopting a separate audit service.

When to use it — and when not to

A self-hoster must run a Doctrine-backed relational database and keep the generated _audit tables and revisions table in step with schema changes. Projects that are not on Symfony and Doctrine 2 should not pick it, and teams wanting audit data shipped to an external log or analytics store will find this design keeps it in the database. Two honest caveats: the project metadata states an MIT licence while the README states the LGPL license, so the licensing position should be confirmed before adoption, and the README text available here is cut off mid-sentence in the configuration section, so the exclusion-property documentation is incomplete. The bundle is maintained across parallel 1.x and 2.x branches with GitHub Actions tests and Codecov coverage reported, and a PHP 8-era Symfony stack should target the branch matching its Symfony version.

project readme (upstream, from github) — read inline

EntityAuditBundle

This extension for Doctrine 2 is inspired by Hibernate Envers and allows full versioning of entities and their associations.

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

Branch Github Actions Code Coverage
1.x Test Coverage Status
2.x. Test Coverage Status

Support

For general support and questions, please use StackOverflow.

If you think you found a bug or you have a feature idea to propose, feel free to open an issue after looking at the contributing guide.

License

This package is available under the LGPL license.

How does it work?

There are a bunch of different approaches to auditing or versioning of database tables. This extension creates a mirroring table for each audited entitys table that is suffixed with "_audit". Besides all the columns of the audited entity there are two additional fields:

  • rev - Contains the global revision number generated from a "revisions" table.
  • revtype - Contains one of 'INS', 'UPD' or 'DEL' as an information to which type of database operation caused this revision log entry.

The global revision table contains an id, timestamp, username and change comment field.

With this approach it is possible to version an application with its changes to associations at the particular points in time.

This extension hooks into the SchemaTool generation process so that it will automatically create the necessary DDL statements for your audited entities.

Installation

Installing the bundle

Simply run assuming you have composer:

$ composer require sonata-project/entity-audit-bundle

Enable the bundle

Finally, enable the bundle in the kernel:

// config/bundles.php

return [
    //...
    SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle::class => ['all' => true],
    //...
];

Configuration

Load extension "simple_things_entity_audit" and specify the audited entities

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    audited_entities:
        - MyBundle\Entity\MyEntity
        - MyBundle\Entity\MyEntity2

If you need to exclude some entity properties from triggering a revision use:

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    global_ignore_columns:
        - created_at
        - updated_at

In order to work with other connection or entity manager than "default", use these settings:

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    connection: custom
    entity_manager: custom

If you need to explicitly discard the foreign keys inferred from the audited entities, you can use the disable_foreign_keys parameter:

simple_things_entity_audit:
    disable_foreign_keys: true

Creating new tables

Call the command below to see the new tables in the update schema queue.

./bin/console doctrine:schema:update --dump-sql

Installation (Standalone)

For standalone usage you have to pass the entity class names to be audited to the MetadataFactory instance and configure the two event listeners.

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\EventManager;
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditManager;
use SimpleThings\EntityAudit\Tests\ArticleAudit;
use SimpleThings\EntityAudit\Tests\UserAudit;

$auditConfig = new AuditConfiguration();
$auditConfig->setAuditedEntityClasses([ArticleAudit::class, UserAudit::class]);
$auditConfig->setGlobalIgnoreColumns(['created_at', 'updated_at']);

$eventManager = new EventManager();
$auditManager = new AuditManager($auditConfig);
$auditManager->registerEvents($eventManager);

$config = new Configuration();
// $config ...
$connection = [];
$entityManager = EntityManager::create($connection, $config, $eventManager);

Usage

Querying the auditing information is done using a SimpleThings\EntityAudit\AuditReader instance.

use SimpleThings\EntityAudit\AuditReader;

class DefaultController extends Controller
{
    public function indexAction(AuditReader $auditReader)
    {
    }
}

In a standalone application you can create the audit reader from the audit manager:

$auditReader = $auditManager->createAuditReader($entityManager);

Find entity state at a particular revision

This command also returns the state of the entity at the given revision, even if the last change to that entity was made in a revision before the given one:

$articleAudit = $auditReader->find(
    SimpleThings\EntityAudit\Tests\ArticleAudit::class,
    $id = 1,
    $rev = 10
);

Instances created through AuditReader#find() are NOT injected into the EntityManagers UnitOfWork, they need to be merged into the EntityManager if it should be reattached to the persistence context in that old version.

Find Revision History of an audited entity

$revisions = $auditReader->findRevisions(
    SimpleThings\EntityAudit\Tests\ArticleAudit::class,
    $id = 1
);

A revision has the following API:

class Revision
{
    public function getRev();
    public function getTimestamp();
    public function getUsername();
}

Find Changed Entities at a specific revision

$changedEntities = $auditReader->findEntitiesChangedAtRevision(10);

A changed entity has the API:

class ChangedEntity
{
    public function getClassName();
    public function getId();
    public function getRevisionType();
    public function getEntity();
}

Find Current Revision of an audited Entity

$revision = $auditReader->getCurrentRevision(
    'SimpleThings\EntityAudit\Tests\ArticleAudit',
    $id = 3
);

Setting the Current Username

Each revision automatically saves the username that changes it. For this to work, the username must be resolved.

In the Symfony web context the username is resolved from the one in the current security context token.

You can override this with your own behaviour by configuring the username_callable service in the bundle configuration. Your custom service must be a callable and should return a string or null.

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    service:
        username_callable: acme.username_callable

In a standalone app or Symfony command you can set an username callable to a specific value using the AuditConfiguration.

$auditConfig = new \SimpleThings\EntityAudit\AuditConfiguration();
$auditConfig->setUsernameCallable(function () {
	$username = //your customer logic
    return username;
});

Viewing auditing

A default Symfony controller is provided that gives basic viewing capabilities of audited data.

To use the controller, import the routing (don't forget to secure the prefix you set so that only appropriate users can get access)

## config/routes.yam

readme truncated — read the full docs on github

Frequently asked questions

Is EntityAuditBundle free to use?

EntityAuditBundle is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does EntityAuditBundle do?

Audit for Doctrine Entities

What is EntityAuditBundle written in?

EntityAuditBundle is primarily written in PHP. Its source is publicly available at https://github.com/sonata-project/EntityAuditBundle, and it has 643 GitHub stars.