CakePHP: Has anyone ever seen email functionality inside a model of MVC?
-
I am using CakePHP, one of my co-worker has added email functionality inside a model function while writing database queries. My co-worker argues that models should be fat and so this functionality should go inside model. Has anyone ever seen email functionality inside a model of MVC? Shouldn't email functionality go inside the controller because models are only required to fetch data from data-source?
-
Answer:
An email is a view, you should respect MVC pattern ; select data to put in the email from the model, send it to the view through controller, render the view as HTML or plain text, and tweak your controller to send the rendered view.
Xavier Laumonier at Quora Visit the source
Other answers
DISCLOSURE: I am a CakePHP core developer, but my answers are my own. They are not shared/reviewed by any CakePHP team member before posting and only represent my personal opinion. Both are considered 'bad design'. I would suggest the use of 'events' in CakePHP. Furthermore, sending emails should be a worker's task and so the event should only push the job to a queue that can later be processed, thus moving email sending to the shell (not controller and definitely not model). Now, because I understand that some applications don't need the most advanced setups, the email could be sent directly from the event. This makes for well organized/structured code that can be easily tested.
Jad Bitar
Email functionality should not go into the model OR the controller. The functionality should be a separate service, an internal application API of sorts which can be called from absolutely wherever - model OR controller. Compartmentalization and independent components are key to the success of any big enterprise system.
Bruno Skvorc
Typically, the model encapsulates the data layer and nothing else. This can be problematic in some cases, e.g, if you have multiple controllers that each need to trigger the same e-mail logic. In small systems, moving that logic into the model itself is potentially fine, but the concept of "fat models" is not likely to scale well, as it breaks encapsulation. I generally encourage such logic to be encapsulated in an adapter (sometimes called a "mediating controller"). Your controllers now only talk to the adapter, which sends the e-mail and then calls into the model.
Neal Kanodia
You are right. Email functionality should be implement in controller. As example if ( false !== doModelInstruction() ) { sendEmail() }. Model is dedicated stricte for database manipulation, nothing more.
Roman Piekarski
It all depends on how you interpret MVC. In some cases it's perfectly fine to have email functionality in the Model. In others you need to have a different approach as suggested. It should never be in the controller, however. At least specifically in terms of CakePHP, it's good to have in the model if you understand the implications and your use and interpretation of MVC is not broken.
Anonymous
Related Q & A:
- Has anyone ever started a business?Best solution by Yahoo! Answers
- Has anyone ever had a Cerebral Angiogram?Best solution by Yahoo! Answers
- How do I open a 'photo' inside a Yahoo email (rather than just attaching it?Best solution by uk.answers.yahoo.com
- Has anyone ever had a group interview at Macy's recently?Best solution by Yahoo! Answers
- Have you ever seen a mountain lion in the wild?Best solution by Answerbag.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.