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) }

Hi there! I recently wrote an ebook on web application security, currently sold on leanpub, the Amazon Kindle store and gumroad.

It contains 160+ pages of content dedicated to securing web applications and improving your security awareness when building web apps, with chapters ranging from explaining how to secure HTTP cookies with the right flags to understanding why it is important to consider joining a bug bounty program.

Feel free to skim through some of the free chapters published on this blog and, if the content seems interesting enough to you, grab a copy on leanpub, the Amazon Kindle store, gumroad or simply checkout right down below!

Buy the Web Application Security ebook for $6.99

In the mood for some more reading?

...or check the archives.