cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?

Home Page:https://komarev.com/sources/laravel-love

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double saving on registerAsLove{Reacter/Reactant}

volkv opened this issue · comments

Hi @antonkomarev ! just a question. why would we need to registerAsLove{Reacter/Reactant} on Created event observer? It causes double saving event firing and DB operation.
Can we hook on Creating event and just pass reacter/reactant_id before model is created?
It seems to be working for me. Just wondering is there can be any issues with such approach?

Good call! I don't remember right now why I made it on created event, but there definitely were a reasons. Will return back to this question in a free time.

If it will be done on creating event - we will be able to use non-nullable fields which you have asked for:

Sorry for the long delay @volkv.

I created a MR which implements what you've said before (and it works):

I found time to dive deeper to this question and tried to remember the reason why this this decision was made.

My motivation was simple: if creation of the Reactable/Reacterable model will fail, we will have empty Reactant/Reacter model not attached to anything.

Scenario:

  1. Reacterable/Reactable model save called (creating observer method called)
  2. Reacter/Reactant model created and its id set (but not persisted yet) to the Reacterable/Reactable model
  3. Something bad happens (database connection issue, the electricity was cut off, etc)

But I think it may be solved by adding a console command, which will find all orphan Reactant/Reacter models and delete them.

What do you think about it?