1

I only recently started Using CodeIgniter I have a simple question is it normal to use multiple models and controllers for the same view ? What I want to do is to have a controller and a model for pagination and display records from database and then have a separate model and controller for update and delete functions. Would that be appropriate or is it better to have one model and controller for all of these functions ?

1 Answer 1

2

In most cases you're going to have one model per database table that handles all the database functionality for that particular table. When it comes to controllers the opinions differ here, I know some people that have one controller per view, personally I make my controllers functionality specific. So if I am dealing with membership functions they all go in the membership controller, sales functions go in a sales controller and so on.

If you have functions that a lot of controllers are going to use repeatedly look at creating a MY_Controller and extending the base controller with it. Basically you create a controller that extends the base CI controller and then all your other controllers extend that giving all your controller the functionality that is in MY_Controller.

Read more here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html

Sign up to request clarification or add additional context in comments.

6 Comments

so its better to stick to one model but its OK to have multiple controllers ?
how would the project be effected if i had multiple controllers and models fro the same view page ?
If you have one database table then sure it's fine to stick to a single model. It could get very confusing if you have multiples though. I can't think of a reason to have multiple controllers for a single view, but you're going to find there are a lot of times you're getting data from multiple models for one view depending on how complex your application is.
currently as the project is it has a model and a controller for every view. I was tinkling to break it down so the coding will not get messy as i am a beginner and the code gets confusing sometimes.Currently i have 3 tables that i am using in my project. Would the efficient way of doing this be better to have a single model? or dose it not make it really of a difference ?
No you definitely don't want a single model for three tables. One per is fine and honestly other than using the MY_Model concept similar to the MY_Controller I mentioned above one per table is standard procedure. If you're worried about confusing code comment... A LOT. It helps later down the road trust me. By the same token you do not want a model per view either.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.