Docs / Sources / Doctrine ORM & ODM

Doctrine ORM & ODM

Record each insert, update and delete of your Doctrine entities and MongoDB documents.

Enable the sources

SourcePackagePut #[Auditable] on
doctrine_ormdoctrine/orm, doctrine/doctrine-bundleThe entity class
doctrine_odmdoctrine/mongodb-odm-bundleThe document class
config/packages/symfonycorp_audit_log.yaml
symfonycorp_audit_log:
    sources:
        doctrine_orm: true
        doctrine_odm: true
src/Entity/Book.php
use SymfonyCorp\AuditLog\Attribute\Auditable;

#[ORM\Entity]
#[Auditable]
class Book
{
    // each INSERT, UPDATE and DELETE is recorded
}

The entry

The actions are INSERT, UPDATE and DELETE. Each entry has subject_class and subject_id in its context. An INSERT or UPDATE entry also has a changeset, as field => [old, new]. On an INSERT, each old value is null, so the entry holds the initial state. A DELETE entry holds the identifier only.

Control which fields go into the changeset with transformations.

What is not recorded

Good to know - the bundle writes the entries after the commit. With silent_failures: false, a failing output makes flush() throw although the data is saved. Do not use it in production.

Entity and document managers

By default, the source audits every entity manager or document manager. To audit only some of them, list them:

config/packages/symfonycorp_audit_log.yaml
symfonycorp_audit_log:
    sources:
        doctrine_orm:
            entity_managers: [default]
        doctrine_odm:
            document_managers: [default]

A manager that is not listed must not share its connection with a listed one. The container compilation fails on an unknown name or a shared connection.