Have just started to get into CakePHP since a couple of weeks back. I have some experience with MVC-frameworks, but this problem holds me back a bit.
I am currently working on a model foo
, containing a primary id and some attributes. Since a complete history of the changes of foo
is necessary, the content of foo
is saved in the table foo_content
. The two tables are connected through foo_content.foo_id = foo.id
, in Cake with a foo hasMany foo_content
-relationship.
To track the versions of foo
, foo_content
also contains the column version
, and foo
itself the field currentVersion
. The version is an number incremented by one everytime the user updates foo
. This is an older native PHP-app btw, to be rewritten on top of Cake. 9 times out of 10 in the app, the most recent version (foo.currentVersion
) is the db-entry that need to be represented in the frontend.
My question is simply: is there someway of representing this directly in the model? Or does this kind of logic simply need to be defined in the controller?
Most grateful for your help!
For always getting the latest version automatically, you can easily make another association:
That, or you add a
'order' => array('FooContent.version' => 'desc')
clause to yourhasMany
relationship and always work with$foo['FooContent'][0]
.For automatically creating archived versions, you can work with
beforeSave
and/orafterSave
callbacks.