CodeIgniter Forums
1 controller = 1 model VS. 1 controller = multiple model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: 1 controller = 1 model VS. 1 controller = multiple model (/thread-71499.html)

Pages: 1 2


1 controller = 1 model VS. 1 controller = multiple model - kaitenz - 08-21-2018

I need a clarification on this. This made me more confuse.

Is it better to have multiple model in a single controller or should I stick with 1:1?

I do have so many method functions inside my model. It gets messier as my project progress..

And by the way, I'm using HMVC now, thanks to WireDesignz.

Thanks in advance.


RE: 1 controller = 1 model VS. 1 controller = multiple model - Pertti - 08-21-2018

You should make your models follow a domain or topic, like user model, product model, basket model, invoice model, etc.

Controllers are site functions that take input (current user session, product they wanted to add to basket, current open basket for current user) and interact with them (add selected product to open basket).

Different site functions might re-use some models (user session is used on every page, basket is used for adding products, viewing basket contents, completing an order, etc).


RE: 1 controller = 1 model VS. 1 controller = multiple model - kaitenz - 08-21-2018

Hmm.. I always use the 1:1.

Ex:
Forms.php (controller) paired with Forms_model.php (model)

Thank you for your advice. I'll follow that.


RE: 1 controller = 1 model VS. 1 controller = multiple model - kaitenz - 08-27-2018

Hi Pertti,

Any advice? How do you separate your models? Any examples based on your projects?


RE: 1 controller = 1 model VS. 1 controller = multiple model - InsiteFX - 08-27-2018

Admin AdminModel
User UserModel
Product ProductModel
etc;


RE: 1 controller = 1 model VS. 1 controller = multiple model - albertleao - 08-27-2018

Your controllers SHOULDN'T have a 1:1 relationship with models. They are taking care of different things.

A model should handle an entity, such as a single database table. Your controller calls upon your different models for data.

For example, let's say during registration, you have to create your user and create a profile he belongs to. Your registration controller should call the UserModel & the ProfileModel.

Any business logic relating to users or profiles should be in their models.


RE: 1 controller = 1 model VS. 1 controller = multiple model - skunkbad - 08-27-2018

(08-27-2018, 09:33 AM)albertleao Wrote: ...

such as a single database table.

...

THIS

Although, you could have a model for a group of related tables, if you choose. I do this for authentication, because the tables are all related, and most don't need their own model.


RE: 1 controller = 1 model VS. 1 controller = multiple model - albertleao - 08-27-2018

(08-27-2018, 01:20 PM)skunkbad Wrote:
(08-27-2018, 09:33 AM)albertleao Wrote: ...

such as a single database table.

...

THIS

Although, you could have a model for a group of related tables, if you choose. I do this for authentication, because the tables are all related, and most don't need their own model.

Personally, I find it a lot easier and more organized to keep my models in a 1 to 1 relationship with database tables, then if I have some functionality I want to reuse that uses multiple models, I create a library. I find it easier to test as well since you can easily inject stubs for models in the library.


RE: 1 controller = 1 model VS. 1 controller = multiple model - skunkbad - 08-27-2018

(08-27-2018, 01:30 PM)albertleao Wrote:
(08-27-2018, 01:20 PM)skunkbad Wrote:
(08-27-2018, 09:33 AM)albertleao Wrote: ...

such as a single database table.

...

THIS

Although, you could have a model for a group of related tables, if you choose. I do this for authentication, because the tables are all related, and most don't need their own model.

Personally, I find it a lot easier and more organized to keep my models in a 1 to 1 relationship with database tables, then if I have some functionality I want to reuse that uses multiple models, I create a library. I find it easier to test as well since you can easily inject stubs for models in the library.

So then how do you handle joins?


RE: 1 controller = 1 model VS. 1 controller = multiple model - kaitenz - 08-27-2018

I like the idea of 1 table = 1 model.

I created the following models:

  • Form_options_model.php  -  This will deal with the dropdown options from the database (table lookups). Also used in form validation's "in_list[]" (grabbing all the keys with the help of array_keys() so dropdown  values are still correct upon submission.
  • Forms_model.php  -  This is where all the magic happens, from form submission to form fetching.
  • Form_tables_model.php  -  For my datatables.

Is this correct? I need your opinions. Constructive criticisms are welcome.