Page 1 of 1

When does the Model come into play?

Posted: Fri Nov 19, 2010 12:59 pm
by s992
Yesterday, I went through the 20 minute tutorial on CodeIgniter's site(http://codeigniter.com/tutorials/watch/blog/), which details how to create a very simple blog using CI.

It's my understanding that the model is the data layer of the program, but I'm not really sure what exactly that means. The tutorial video that I posted does not use a model at all - just a single controller and a single view. The blog worked just fine, so I am a little confused as to the purpose of the model.

If I were writing an application with CodeIgniter(or any MVC framework), what sort of code would I be likely to place in the model? Database queries? Processing of data?

Further expanding on my question: Let's say, for the sake of example, I'm writing a calculator that, for some reason, requires an MVC framework. Would I process the calculations in the model?

Let's say that someone visits the site, would this be the "correct" chain of events?
1. Calculator_Controller calls Calculator_View, which presents a form.
2. The user enters 1+1.
3. Calculator_View submits the form back to Calculator_Controller.
4. Calculator_Controller sends the input to Calculator_Model to process.
5. Calculator_Model adds the numbers and sends them back to Calculator_Controller.
6. Calculator_Controller sends the solution to Calculator_View, which presents it to the user.

Thanks in advance for any insight that you can offer!

Re: When does the Model come into play?

Posted: Fri Nov 19, 2010 1:10 pm
by AbraCadaver
s992 wrote:Yesterday, I went through the 20 minute tutorial on CodeIgniter's site(http://codeigniter.com/tutorials/watch/blog/), which details how to create a very simple blog using CI.

It's my understanding that the model is the data layer of the program, but I'm not really sure what exactly that means. The tutorial video that I posted does not use a model at all - just a single controller and a single view. The blog worked just fine, so I am a little confused as to the purpose of the model.

If I were writing an application with CodeIgniter(or any MVC framework), what sort of code would I be likely to place in the model? Database queries? Processing of data?

Further expanding on my question: Let's say, for the sake of example, I'm writing a calculator that, for some reason, requires an MVC framework. Would I process the calculations in the model?

Let's say that someone visits the site, would this be the "correct" chain of events?
1. Calculator_Controller calls Calculator_View, which presents a form.
2. The user enters 1+1.
3. Calculator_View submits the form back to Calculator_Controller.
4. Calculator_Controller sends the input to Calculator_Model to process.
5. Calculator_Model adds the numbers and sends them back to Calculator_Controller.
6. Calculator_Controller sends the solution to Calculator_View, which presents it to the user.

Thanks in advance for any insight that you can offer!
I would say yes, your example makes sense. You should have skinny controllers. Controllers decide what actions should be taken and interface with the model which should be fat (most code here). The model holds all of the business/data logic. In your example, the validation of whether 1+1 is a valid calculator expression or not would be in the model.

Re: When does the Model come into play?

Posted: Fri Nov 19, 2010 1:20 pm
by s992
So, my calculator model might include two functions, one to validate that the submission is a legitimate calculation, and one to actually perform the calculation?

Let's move away from the calculator now and take a look at a real world example of a project I'm working on. I query the database for a number of records and then I have to format them a certain way(in this case, I'm formatting a table to look similar to an Excel worksheet we use). Would the query and the conditional formatting both take place in the model, or would I pass the raw results to the controller(which in turn passes them to the view) and format them in the view?

Re: When does the Model come into play?

Posted: Fri Nov 19, 2010 1:27 pm
by AbraCadaver
s992 wrote:So, my calculator model might include two functions, one to validate that the submission is a legitimate calculation, and one to actually perform the calculation?

Let's move away from the calculator now and take a look at a real world example of a project I'm working on. I query the database for a number of records and then I have to format them a certain way(in this case, I'm formatting a table to look similar to an Excel worksheet we use). Would the query and the conditional formatting both take place in the model, or would I pass the raw results to the controller(which in turn passes them to the view) and format them in the view?
Normally the controller would decide what to retrieve (one record, all records, random record, etc.) and then engage the model to do this. Then the controller would pass that out to the view which would format the data for display. Any loops or conditionals in the view are for presentation only, not business logic. You should design the app so that you can have multiple views that use the same data. Suppose you want an HTML page and an RSS feed. You use the same model and controller and produce the same data regardless. The HTML or RSS view is responsible for formating the data.

Re: When does the Model come into play?

Posted: Fri Nov 19, 2010 1:42 pm
by s992
Thanks so much for your responses - very clear and helpful! One more question comes to mind, regarding the initial tutorial that I referenced.

In the tutorial, they only made use of the controller and the view. The controller contained a simple query. I don't remember the exact syntax and I don't have the code in front of me, but it was something along the lines of $this->db->get("events"). It then stored the results in an array, which was passed to the view.

Would the correct way of achieving this functionality be to have a single function in your model that returns the results of the query to your controller? Again, the syntax here is probably off since I don't have anything to reference right now, but you should get the general idea.

Code: Select all

class SimpleController extends Controller {
	function index() {
		$data = array();
		$data["posts"] = $this->SimpleModel->getPosts();
		$this->load->view("posts.php",$data);
	}
}

class SimpleModel extends Model {
	function getPosts() {
		return $this->db->get("posts");
	}
}