Page 1 of 1

How do I get the value of a field before it was updated?

PostPosted: Tue Oct 18, 2011 11:20 am
by rlevin
I'm using the function beforeSave(), In my case I need to get a reference of the value of a field before the save button was clicked. I used $record->val("fieldhere"), but that gives me the updated value rather than the value before it was updated. I need it to compare the value before it was updated to after the field was updated.

I was thinking, creating a function that accepts the value of the field in one of your functions before the beforeSave() occurs then call that custom function in beforeSave() to return the value to compare to the updated field. I'm not sure if this is the correct way to handle this. If I did do it this way I'm not sure which function to use to retain the value in.

beforeAddExistingRelatedRecord?
beforeAddNewRelatedRecord?
beforeAddRelatedRecord
beforeCopy
beforeDelete?
beforeRemoveRelatedRecord?
beforeSave
beforeInsert?
beforeUpdate?

Re: How do I get the value of a field before it was updated?

PostPosted: Tue Oct 18, 2011 11:39 am
by shannah
You can use the Dataface_Record::getSnapshot() method to get the records as they were before they were changed.

e.g.
Code: Select all
$snapshot = $record->getSnapshot();
$oldVal = $snapshot['myfield'];
$newVal = $record->val('myfield');


This is used in the methods
valueChanged() and recordChanged() to be able to tell if a field has changed or the whole record has changed since it was last saved.

-Steve

Re: How do I get the value of a field before it was updated?

PostPosted: Thu Oct 20, 2011 7:11 am
by rlevin
Thank you!