Versioning other behaviours' columns with Doctrine Versionable

Using doctrine inside symfony is a relief: behaviours, particularly, can save your days.

The Versionable behaviour, for example, has the ability to manage versions of your model’s entities.

1
2
3
4
5
Entity:
  actAs:
    Versionable: ~
  columns:
    name:  { type: varchar(25) }

In this case every time you’re gonna update an entity, it’s name is stored in a table called entity_version, and your object is ready to be reverted to a previous version by simply calling:

1
2
3
<?php

$entity->revert(X);

where X is the revision number.

But, but, but…

…when using this behaviour with other behaviours you must pay attention at the order of the behaviours in your schema.yml.

1
2
3
4
5
6
Entity:
  actAs:
    Versionable: ~
    Timestampable: ~
  columns:
    name:  { type: varchar(25) }

In this case we added the timestampable behaviour, another built-in behaviour which adds created_at and updated_at attributes/columns.

But with this YML, the versioning table will not store the additional columns, because the Versionable behaviour is declared before the timestampable one, so you need to put it after all the behaviours which add columns you want to version:

1
2
3
4
5
6
Entity:
  actAs:
    Timestampable: ~
    Versionable:  ~
  columns:
    name:  { type: varchar(25) }

In the mood for some more reading?

...or check the archives.