{"id":969,"date":"2011-12-05T20:40:01","date_gmt":"2011-12-06T03:40:01","guid":{"rendered":"http:\/\/www.analogrithems.com\/rant\/?p=969"},"modified":"2011-12-05T21:04:08","modified_gmt":"2011-12-06T04:04:08","slug":"revisionable-behavior-in-cakephp","status":"publish","type":"post","link":"https:\/\/www.analogrithems.com\/rant\/revisionable-behavior-in-cakephp\/","title":{"rendered":"Revisionable Behavior in CakePHP"},"content":{"rendered":"<p>There have been many times where I need to have some sort of revision ability in my cake apps. \u00c2\u00a0I&#8217;ve looked through the bakery and never found a very good clean solution. \u00c2\u00a0I 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<\/p>\n<p>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. \u00c2\u00a0The 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. \u00c2\u00a0It then adds a few functions to your model you&#8217;ve added the behavior on to make sure it can see the previous behaviors.<\/p>\n<p>One of the best features of this plugin is that it will store multiple models revisions all in the same table.<\/p>\n<p>I&#8217;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<\/p>\n<p>You may now be asking your self, where can one find such a fine piece of code? \u00c2\u00a0As with most of my code it resides on github \u00c2\u00a0<a href=\"https:\/\/github.com\/analogrithems\/revisionable\">https:\/\/github.com\/analogrithems\/revisionable<\/a><\/p>\n<p>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.<\/p>\n<h3>Setup<\/h3>\n<p>1) First you need to add the table to your project  use the following to generate that<br \/>\n\tcake schema create Revisionable.revision<\/p>\n<p>2) add the behavior to your model like so <\/p>\n<p>[php]<br \/>\n\tvar $actAs = array(&#8216;Revisionable.Revisionable&#8217;);<br \/>\n[\/php]<\/p>\n<h3>Some Configure options<\/h3>\n<p>If you already have a table named revisions in your project and need to call the it something else then<br \/>\nmake a new table\/model in your project that has the same schema as this plugin and pass that model name<br \/>\nto the actAs setup like so<\/p>\n<p>[php]<br \/>\n\tvar $actAs(&#8216;Revisionable.Revisionable&#8217;=&gt;array(&#8216;revisionableModel&#8217;=&gt;$newModelName));<br \/>\n[\/php]<\/p>\n<h2>To Do<\/h2>\n<p>So far aside from creating a revision everytime the data changes the only other feature of this plugin<br \/>\nis the listRevisions function.  It works like this<\/p>\n<p>[php]<br \/>\n\t$revisions = $this-&gt;Model-&gt;listRevisions($this-&gt;Model-&gt;id);<\/p>\n<p>\tprint_r($revisions);<br \/>\n\tarray(<br \/>\n\t\t&#8216;2011-12-05 01:01:01&#8217;=&gt; array(<br \/>\n\t\t\t&#8216;Pages&#8217; =&gt; array(<br \/>\n\t\t\t\t&#8216;id&#8217;=&gt; &#8216;4edd8cc3-a628-490b-8cbf-6435ac1005e9&#8217;,<br \/>\n\t\t\t\t&#8216;name&#8217;=&gt; &#8216;foobar&#8217;,<br \/>\n\t\t\t\t&#8216;body&#8217;=&gt; &#8216;&#8230;.&#8217;<br \/>\n\t\t\t)<br \/>\n\t\t)<br \/>\n\t);<br \/>\n[\/php]<\/p>\n<p>Need to create some more functions that add to this, like a a restore where you pass the id &amp; date and<br \/>\nit will do a restore and perhaps a diff viewer.  Patches are warmly welcome.<\/pre>\n<p>&nbsp;<br \/>\nAlso 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There have been many times where I need to have some sort of revision ability in my cake apps. \u00c2\u00a0I&#8217;ve looked through the bakery and never found a very good clean solution. \u00c2\u00a0I thought about it for a bit and it seems like a pretty simple thing to do, so I made a light weight [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,40],"tags":[],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-cakephp","category-web-development"],"_links":{"self":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":5,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":971,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/969\/revisions\/971"}],"wp:attachment":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}