Skip to content

Conversation

@inxilpro
Copy link
Contributor

@inxilpro inxilpro commented Aug 24, 2024

Many events create or update models in their handle() method, and up until now, this could cause an N+1 issue if you were operating on a lot of events that all interact with the same model at the same time (either batch operations or replays).

This introduces a new #[EagerLoad] attribute that you can add to any protected model property of an event (they must be protected because the model shouldn't be serialized with the event data).

For example:

class UserPromotedToManager extends Event
{
  #[StateId(UserState::class)]
  public int $user_id;

  #[EagerLoad]
  protected User $user;

  public function handle()
  {
    // If the user wasn't eager-loaded, load it lazily
    $this->user ??= User::find($this->user_id);

    $this->user->update(['role' => 'manager']);
  }
}

When verbs encounters a batch of events that have EagerLoad attributes, it'll load them all in a single query and then push the results into the appropriate events.

🪄

@inxilpro
Copy link
Contributor Author

Talked with Daniel about this and the decision is to change the clone behavior and then merge in.

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

Successfully merging this pull request may close these issues.

2 participants