XF 2.2 Does saveIfChanged check if custom fields have changed?

benFF

Well-known member
I know this sounds like a stupid question but I want to make sure.

If I have a function that calls $item->custom_fields->set and then after $item->saveIfChanged - does XF check if the set command actually made a difference, or will the fact I called set mean it will save?

IE:

PHP:
// $item->custom_fields->test is currently equal to 1

$item->custom_fields->set('test', 1, 'admin', true);

$item->saveIfChanged()

Will the entity save or skip (as the value even though set, was exactly the same as current)?
 
It saves if any local columns have changed. Setting a custom field value typically updates the local cache column, so yes as long as the set was successful.
 
So just to be clear, if I set to the same value as it already is, this will cause a save?

(In which case I should do manual checks and not rely on saveifchanged?)
 
Oh sorry I missed that. I don't think so, unless the cache value was stale for some reason. You can get an out-param to check if the entity was actually saved:

PHP:
$item->saveIfChanged($wasSaved);

\XF::dump($wasSaved);

// ... or ...
\XF::dump($item->hasChanges());
 
Back
Top Bottom