dingerkingh
dingerkingh

Reputation: 448

cakephp update modified time on another table or model

Let's say that I have a Post table and an Update table. What I would like to know is how could I modify the 'modified' time field in the Post table when an update is added to the update table. I have searched - but can't seem to figure it out.

I hope this makes sense...

Thanks!

Upvotes: 0

Views: 590

Answers (1)

Luc Franken
Luc Franken

Reputation: 3014

Yes you can do so, for example you could do this in the model. Implement a afterSave() method in the Update table. There you can update the related Post. http://book.cakephp.org/2.0/en/models/callback-methods.html

Other way around would be removing modified in the Post model and creating an afterFind() method in the model. In most instances you should not do this because of data load but might be useful when using lots of inserts and a very low amount of reads.

The most nice way is to put those methods into a Behaviour which creates a separation of concerns and also allows re-use:

http://book.cakephp.org/2.0/en/models/behaviors.html

Addition while thinking about it: You could of course update the Post.modified field but it would be more clear and better to create a separate field for it. So Post.latestcomment datetime field. Then also edits to the post will be registered as an update. Also it allows more flexible scheduling. Next to that you know whether there are any posts also.

Upvotes: 1

Related Questions