XF 2.1 Is it possible to bypass preSave() of a custom entity?

asprin

Active member
Let's say via my addon, I have to update an entity and in this specific scenario when I call save(), I do not want the preSave() function to run that I've defined in the Entity's class. Is this doable?

PHP:
$foo = $this->assertRecordExists('Asprin\FB:Foo', 123456, null, null);
$foo->bar = 'all is well';
$foo->save(); // now this should not call the preSave() function defined on Asprin\FB:Foo
 
Solution
It would probably be easier to move that to a separate function and explicitly call it when you need it to happen, instead of putting it in preSave and then trying to suppress it. Or, define and set an option on the entity, and then check if it is true in preSave.
Overwrite preSave with an empty function, or check who is the caller and decide if you want to skip the function.
I've a preSave() function defined in my custom Entity class. It does something on its own and I want to stop that in certain cases. Would it be possible to pass any arguments to it to identify such situations?

PHP:
protected function _preSave()
{
    if($this->isUpdate())
    {
        $this->sys_updated_on = \XF::$time; // I do not want this to happen in certain scenarios
    }
}
 
It would probably be easier to move that to a separate function and explicitly call it when you need it to happen, instead of putting it in preSave and then trying to suppress it. Or, define and set an option on the entity, and then check if it is true in preSave.
 
Solution
Or, define and set an option on the entity, and then check if it is true in preSave.
This looks viable. I guess I'll have to take this option then.

Perhaps I can suggest this to the devs to add that option in future iterations to bypass these out of the box functions - say something like

PHP:
$foo = $this->assertRecordExists('Asprin\FB:Foo', 123456, null, null);
$foo->bar = 'all is well';
$foo->save(false); // indicator to not run pre or post save functions
 
Back
Top Bottom