This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Crazymodder
Crazymodder

Hi everyone,

I will use the nice feature of model events. I start adding code into the afterUpdate Method and it works great. But if I softdelete or restore an item this event is also fired. How can I stop this? Can I deregister this event in the beforeRestore Event? Or do I have to check for deleted_at field changes and start my code or not?

Thanks for your help. Greets Crazymodder

Scott
Scott

You could check the fields you're concerned about before updating, and binding an event if any of the relevant ones have changed

public function beforeUpdate()
{
    if (array_intersect(['field_1', 'field_2', 'field_3'], $this->getDirty())) {
        $this->bindEvent('model.afterUpdate', function() {
            $this->doStuff();
        }
    }
}

public function doStuff()
{
    // do all the things
}

Last updated

Crazymodder
Crazymodder

Hi Scott,

thanks for your fast reply. Your solution works fine big thanks for that:)

Last updated

Scott
Scott

Nice, glad that worked!

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.