Docs / Outputs / DBAL

DBAL

Store the entries in a SQL table through a Doctrine DBAL connection.

Overview

PackageReadsPurgeSetup
doctrine/dbal, doctrine/doctrine-bundleYesYesYes

The commands, the EasyAdmin viewer and ReaderInterface can read this output. audit-log:purge and audit-log:setup work with it. See Outputs.

Enable the output

config/packages/symfonycorp_audit_log.yaml
symfonycorp_audit_log:
    outputs:
        dbal:
            connection: ~                 # null = the default connection
            table_name: audit_log

Database account

Give the dbal output its own connection, with an account that can only insert and select rows. Then the application cannot change or delete an entry. Run audit-log:setup and audit-log:purge with an account that can also create tables and delete rows, for example with another database URL in their environment.

Create the table

The output does not create its table by default. Without it, a write can fail, and the bundle only logs the failure.

With Doctrine migrations, the output adds its table to the Doctrine schema of its connection. Generate a migration:

terminal
$ bin/console doctrine:migrations:diff

Otherwise, run the setup command when you deploy. It does not change a table that exists. It fails with a non-zero exit code on an error:

terminal
$ bin/console audit-log:setup --output=dbal

Use either Doctrine migrations or audit-log:setup, not both. The table has indexes on (occurred_at), (source, occurred_at) and (action, occurred_at).

In development and in tests, you can let the output create its table on the first write. Do not use it in production:

config/packages/dev/symfonycorp_audit_log.yaml
symfonycorp_audit_log:
    outputs:
        dbal:
            auto_setup: true

Indexed context - the table has one column for each key of indexed_context. When you change this list, generate a Doctrine migration before you deploy. Without the new columns, the writes fail. See Filter by context.