Doctrine ORM & ODM
Record each insert, update and delete of your Doctrine entities and MongoDB documents.
Enable the sources
| Source | Package | Put #[Auditable] on |
|---|---|---|
doctrine_orm | doctrine/orm, doctrine/doctrine-bundle | The entity class |
doctrine_odm | doctrine/mongodb-odm-bundle | The document class |
symfonycorp_audit_log:
sources:
doctrine_orm: true
doctrine_odm: trueuse 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.
- A to-one relation is recorded as
{class, id}. To-many relations are not recorded: mark the child class#[Auditable]to record its own changes. - Dates are RFC 3339 strings with milliseconds. Binary values are recorded as
[binary].
Control which fields go into the changeset with transformations.
What is not recorded
- With the ORM, a failed flush writes no entry. With the ODM, this is true only for a flush in a transaction (
flush(['withTransaction' => true])). - The sources record only the changes that
flush()writes. A DQLUPDATEorDELETEquery, a native SQL query and an ODM query builder update are not recorded. - A soft delete (Gedmo SoftDeleteable) is not recorded as a DELETE. With the ORM, record it yourself from the
postSoftDeleteevent with manual logging.
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:
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.