Cms: How to know the resave is fired from console

Created on 24 Feb 2021  路  1Comment  路  Source: craftcms/cms

Description

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

question

Most helpful comment

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;
        }

        // ...
    }
);

>All comments

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;
        }

        // ...
    }
);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mccombs picture mccombs  路  3Comments

mattstein picture mattstein  路  3Comments

angrybrad picture angrybrad  路  3Comments

michaelhue picture michaelhue  路  3Comments

angrybrad picture angrybrad  路  3Comments