fntneves / laravel-transactional-events

Transaction-aware Event Dispatcher for Laravel
MIT License
314 stars 28 forks source link

Hello Help needed On using it on Model Observers, not custom events #48

Closed Elshaden closed 2 years ago

Elshaden commented 3 years ago

Hi thanks for this package It looks like exactly what I want, the only problem is I am using model observers not events, so how can I use it in an Observer Class for a given model.

the idea is created method in the observer is only fired when a commit is successful, same with updated and others

Thanks

fntneves commented 3 years ago

Hello,

Thank you for using this package.

Observers are sort of a shortcut for attaching Event Listeners to Eloquent Model events.

The most that this package can do for you is allowing you to enable transactional behavior to all or a subset of Eloquent Events. According to the configuration, you can make all Eloquent events transacional-aware — uncommenting eloquent.* —, or if you want a specific one, you can simply comment the corresponding entry (e.g., eloquent.created).

However, be aware that such action will enable transactional behavior for all Eloquent Models, not only for a specific model. This may cause side effects you may not expect.

I see two alternatives to this:

'transactional' => [
    'App\Events',
    'App\User:eloquent.created,eloquent.saved'
]

Or adding such capabilities to the EventDispatcher, by adding a new method.

$dispatcher->setEloquentTransactionalEvents(/* map with all entries, for instance */)