Welcome Guest, Not a member yet? Register   Sign In
1 controller = 1 model VS. 1 controller = multiple model
#1

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.
Reply
#2

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).
Reply
#3

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.
Reply
#4

Hi Pertti,

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

Admin AdminModel
User UserModel
Product ProductModel
etc;
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

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.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#7

(This post was last modified: 08-27-2018, 01:23 PM by skunkbad.)

(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.
Reply
#8

(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.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#9

(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?
Reply
#10

(This post was last modified: 08-27-2018, 10:29 PM by kaitenz.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB