Love Fuel?    Donate

Thoughts, ideas, random notes, ramblings...

Anything about PHP in general, and FuelPHP in particular. Sometimes serious, sometimes with a big wink. But always with a message. Do you have an opinion about an article? Don't forget to comment!

Over the past few months (on and off) I have been working on implementing a couple of models for Fuel’s ORM layer, temporal and soft delete. The later is pretty simple, instead of actually deleting an entity from the database it is marked as being deleted and filtered out of select statements automatically.

Temporal (or “revisions”) however is a little more complicated, but simple once you understand the basics. Put simply, temporal allows changes to a model to be tracked through time. I’ll get into how it works in a bit. What’s very exciting is that no other PHP based ORM seems to support this functionality right now so it’s not just a first for FuelPHP but also a first in the world of PHP based ORMs!

How it works

The implementation that we eventually chose uses two timestamps, one for the time the entity is valid from and one for the time the entity is valid until. The latest entity always has an “infinite” time (It’s actually 2038-01-19 03:14:07, the maximum timestamp that MySQL can handle, but this can be changed in the config file). This way it’s easy to get the most recent revision or a number of revisions in a given timeframe.

Whenever properties are changed and saved a new revision gets inserted and the previous revision’s end time becomes the start time of the new revision. And when the entity is deleted the latest simply gets its end timestamp set to the current time, and hey presto! no more entity from this point on.

For those of you who are familiar with versioning systems (git, svn, hg or whatever) it’s the same idea as committing each change to the repository and then being able to view those changes and even restore the entity (or commit) to a given point in time.

Soft delete was merged in with 1.5 and temporal with 1.6/develop, so both are available to use. Check out the documentation for more info.

Happy Fueling!