I want to ask a design opinion about how to build a functionality.
I have a model with Requirements, this is meant to gather software requirements in a structured way.
I also have many other entities.
Among them I have a Comment model. Each comment may be associated to a Requirement, or not.
Now I want to add this functionality:
- When someone adds a comment on a requirement, send an email to the requirement owner (user_id).
I managed to solve this using events, but somehow I think that this is not intended to be this way. How would you people integrate this? In a Model to Model fashion? Or Controller to Controller fashion?
This seems like exactly the kind of thing that events are for. Triggered from your Comments table’s
Hi Zuluru, thanks for your answer.
I in fact solved it with an event on Comments->afterSave. That custom event is dispatched via the global event manager.
Then, in Requirements model, I’m calling the global event manager’s “on” method to associate a callback with my event.
What didn’t left me so satisfied is the fact that I have to initialize Requirements model in order to attach the listening callback. And I do that in Comments model, because I didn’t find any other place to do it.
But I really didn’t want to make Comments aware of Requirements, in fact I’m planning to add more functionality to Comments and I want to keep coupling at bare minimum.
P.S.: I don’t speak english very well, so if something isn’t clear please tell me and I’ll rewrite.
Why not add the callback in the initialization of the Comments model, instead of Requirements? Since you can be quite sure that you’ll never be saving a comment without having initialized that model.
That’s right, but my intent was placing the logic in the Requirements model. Maybe that’s a bad design choice, but my thought was:
- My Comments model knows when a comment is posted, so it should notify
- The other models should process the notification and, if it’s about that model, do something (email, etc.) about it
If I put all the logic in the Comments model, I wouldn’t need to send events anyway, I could just call a method.