Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Automatically update date field when changing another field value


Reply


2 Posts   519 Views

Avatar
Bronwyn

Community Member, 20 Posts

20 July 2012 at 4:52pm

Hi, I want to keep a record of when a field was last changed. I have a site where I want to "feature" an object (it's a publication) every couple of weeks on the front page of the site. I plan on having a group of publications that are worth featuring and rotate them. I will either flag all of the featurable publication records and have one randomly appear - if anyone can tell me how to do this I would be grateful - or I will manually change the flag on one of the papers every couple of weeks. This, I know I can do! If I manually change the flag, I optimally want to have a date field that will change to the current date every time I change the flag to "on", so I can see when the last time it was featured was. I can't figure out how to do it - can anyone help?

Avatar
swaiba

Forum Moderator, 1799 Posts

21 July 2012 at 12:09am

Hi,

something like this might help...

public function onAfterWrite() {
   parent::onAfterWrite();
   $arrChanged = $this->getChangedFields(true,1);
   //Log $arrChanged - this structure contains all information about changes made in the last write store this information where you like
}