1 controller = 1 model VS. 1 controller = multiple model |
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.
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).
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.
Hi Pertti,
Any advice? How do you separate your models? Any examples based on your projects?
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 )
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!
(08-27-2018, 01:20 PM)skunkbad Wrote:(08-27-2018, 09:33 AM)albertleao Wrote: ... 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!
(08-27-2018, 01:30 PM)albertleao Wrote:(08-27-2018, 01:20 PM)skunkbad Wrote:(08-27-2018, 09:33 AM)albertleao Wrote: ... So then how do you handle joins?
I like the idea of 1 table = 1 model.
I created the following models:
Is this correct? I need your opinions. Constructive criticisms are welcome. |
Welcome Guest, Not a member yet? Register Sign In |