Shane
Shane

Reputation: 87

MVC: Model, Controller or Library?

I am building a CRM using a framework (codeigniter) for the first time and I am having trouble determining where a certain module should go while maintaining the MVC methodology. The module automatically generates a new user (when a new company is created) and emails the log in details out to the supplied email address.

I am familiar with the idea of skinny controllers and fat models but to compile all the information needed the module must request data from several different tables as well as inserting data into several tables.

The scenarios I have considered so far:

  1. The logic is in the model where most of the information comes from.
  2. Create a totally new model that deals with just this module and the multiple tables required.
  3. Place the logic in the controller that deals with creating a company.
  4. Create a new library or helper and call the module when it is needed.

Skinny controllers and fat models seem to suggest that one or two are the right options but I was lead to believe that a model should only deal with one table in the database.

What is the right approach to ensure adherence with MVC?

Upvotes: 3

Views: 1011

Answers (1)

Laurence
Laurence

Reputation: 60048

Codeigniter allows you to be flexible with your MVC approach. So the answer is which option is:

  • Easiest for you (or your team) to understand
  • Easiest to code maintain
  • Easiest for someone else to understand

There's no point putting your code into a library, if you dont have any other libraries and dont understand libraries. Same as if all your models are "fat", but only point to one table, do you want this model to be the only one that also points to 4 other tables?

Personally, if this "logic" only ever happens in one place, then I would place it into the controller, and call the 4x models you need to do each bit of the code.

If this "logic" occurs in multiple places, I would place it into a library and call it when needed.

Upvotes: 1

Related Questions