When a user is saved (signup/updates his profile) then we call 2 APIs for some operation on EVENT_BEFORE_SAVE event but when we resave users through ./craft resave/users --update-search-index then also this triggers that event which we need to stop or else need to know how to detect that when the resave is fired in the controller so that we don't call the APIs or vice versa?
Thanks in advance
Your event listener can find out if the event was triggered by a bulk-resave via the resave property on the user:
Event::on(
User::class,
User::EVENT_BEFORE_SAVE,
function(ModelEvent $event) {
/* @var User $user */
$user = $event->sender;
// Ignore if this is a bulk resave
if ($user->resaving) {
return;
}
// ...
}
);
Most helpful comment
Your event listener can find out if the event was triggered by a bulk-resave via the
resaveproperty on the user: