Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Bug in WPOrm causes overwritting of model events. #14

Open
calvinalkan opened this issue Aug 31, 2021 · 1 comment
Open

[BUG] Bug in WPOrm causes overwritting of model events. #14

calvinalkan opened this issue Aug 31, 2021 · 1 comment

Comments

@calvinalkan
Copy link

Right now its only possible to register one callback per model event.

Ever successive call to Mode::registerModelEvent() will overwritte all previous callbacks.

Current code:

  public static function registerModelEvent($event, $callback)
    {
        $calledClass = static::getCalledClassName();
        static::$events["model.{$calledClass}.{$event}"] = $callback;
    }

What it should be:

public static function registerModelEvent($event, $callback)
    {
        $calledClass = static::getCalledClassName();
        static::$events["model.{$calledClass}.{$event}"][] = $callback;
    }

And then also something like this:

  public function fireModelEvent($event)
    {
        $calledClass = static::getCalledClassName();
        $eventName = "model.{$calledClass}.{$event}";

        $return = null;
        if (isset(static::$events[$eventName])) {
    
            foreach (static::$events[$eventName] as $callback) {
                
                $return = call_user_func($callback, $this);
                
                if( $return === false ) {
                    return false;
                }
            }
            
            return $return;
        }
    }

I assume you did not intend it to work this way to only be able to register one event per model action. Its kinda limiting and the fix is easy and non-breaking

@techjewel
Copy link
Contributor

techjewel commented Sep 16, 2021

Thanks for letting me know. I am tagging our CTO in this issue.
@heera

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants