Cannot create and then update the same record in a projector because of the $exists property in Model class #488
Replies: 1 comment 2 replies
-
Is your player joined game event a storable event? If so calling it here will cause duplicates if the events ever get replayed. Presumably you have a PlayerProjector that will write to the database onPlayerJoined. You need a PlayerReactor (or GameReactor depending on how you want to structure your dependencies/domains) weighted with a 1 or higher so it runs after the game projector. Give it an onGameCreated method. In there you can record your PlayerJoinedEvent. Then your third piece of logic, the part that wasn't working, should be moved to a onPlayerJoined method in your game projector which takes in your PlayerJoinedEvent. Unless I'm misunderstanding your dependency chain. As for the other thing, you could try calling fresh() on the instance before attempting to update. |
Beta Was this translation helpful? Give feedback.
-
Hello all, first post here!
For my side project, I have 2 tables games & players, and I have a GameProjector to listen to the GameCreatedEvent like below:
These tables have a circular foreign key constraints host_id and game_id, so that's why I need to dispatch the PlayerJoinedEvent event to insert the player record before I update the host_id value.
I have figured out the reason is because the
exists
property of Laravel Model never gets updated to true so callingupdate
on the model object does nothing (line 1009 in Model class source code). The workaround can beGame::find($event->gameId)->writable()->update(...)
but this feels still dirty because of an additional DB query.Does anyone have any idea how to solve this? Thanks!
Summary:
$model->writeable()->save()
does not update theexists
property of the $model object to true, so subsequentupdate()
calls cannot update the record.Beta Was this translation helpful? Give feedback.
All reactions