Home » CakePHP » Revisionable Behavior in CakePHP

There have been many times where I need to have some sort of revision ability in my cake apps.  I’ve looked through the bakery and never found a very good clean solution.  I thought about it for a bit and it seems like a pretty simple thing to do, so I made a light weight simple behavior that provides this

The behavior is built into a plugin because it also has an simple model(table) that it uses to store revisions for all the other tables.  The idea is that when ever you update your table this behavior will jump in, grab the current copy of the data and save it to the revisions table with the current timestamp.  It then adds a few functions to your model you’ve added the behavior on to make sure it can see the previous behaviors.

One of the best features of this plugin is that it will store multiple models revisions all in the same table.

I’ve written this for CakePHP 1.3.x so it make not work with older or newer versions. Let me know if you have any questions or feedback

You may now be asking your self, where can one find such a fine piece of code?  As with most of my code it resides on github  https://github.com/analogrithems/revisionable

So far it is very simple, all you have to do add re-visioning to your models is add the revisionable behavior to your plugin.


1) First you need to add the table to your project use the following to generate that
cake schema create Revisionable.revision

2) add the behavior to your model like so

	var $actAs = array('Revisionable.Revisionable');

Some Configure options

If you already have a table named revisions in your project and need to call the it something else then
make a new table/model in your project that has the same schema as this plugin and pass that model name
to the actAs setup like so

	var $actAs('Revisionable.Revisionable'=>array('revisionableModel'=>$newModelName));

To Do

So far aside from creating a revision everytime the data changes the only other feature of this plugin
is the listRevisions function. It works like this

	$revisions = $this->Model->listRevisions($this->Model->id);

		'2011-12-05 01:01:01'=> array(
			'Pages' => array(
				'id'=> '4edd8cc3-a628-490b-8cbf-6435ac1005e9',
				'name'=> 'foobar',
				'body'=> '....'

Need to create some more functions that add to this, like a a restore where you pass the id & date and
it will do a restore and perhaps a diff viewer. Patches are warmly welcome.
Also I use uuid as my primaryKeys I will try to find some time to ad a configuration option that will allow for int as the primaryKey. In all reality, nothing really prevents this from working with int except for the table schema, so if you just change the table schema to use int then the rest of the plugin will follow.

Leave a Reply

Your email address will not be published.