Welcome Guest, Not a member yet? Register   Sign In
MVC Question RE Query Criteria in Controllers Instead of Models
#1

[eluser]al042077[/eluser]
I'm a rookie, so I apologize if this is just a stupid question. I've been searching the forum and Google for a couple hours, but can't find the answer.

In most of the CI examples, I see people setting query conditions in the controllers and then call the model function to run the actual query. For example, a controller may show:

Code:
$this->db->where('date >= ', $this->input->post('date_low'));

Then, in the model, it shows:

Code:
$rs = $this->db->get('expenses');

As I understand MVC, one advantage is to separate the data layer from the controller. So, it seems to me a more MVC-compliant way would be to pass the criteria information to model, which would then run:

Code:
$this->db->where('date >= ', $this->input->post('date_low'));
$rs = $this->db->get('expenses');

This way, all interaction with the database is contained in the model. If the purpose of the model is to separate data and control, then they should be separate. If the model is just running a simple "get" function, then the whole model seems a bit much for a single function.

Am I missing something?
#2

[eluser]Jan_1[/eluser]
don't understand where you've seen it alike that... you can arrange your processes clearly by putting all data modeling to the model.
controller.php
Code:
function index()
{
  $data['expenses']  = $this->calculation_model->get_expenses();
  $this->load->view('view_file', $data);
}

calculation_model.php
Code:
function get_expenses()
{
  $this->db->where('date >= ', $this->input->post('date_low'));
  $query = $this->db->get('expenses');
  if($query->num_rows() > 0)
  { $data = $query->result();
    return $data;
  }
}

view_file.php
Code:
<?php
if(isset($expenses)){ print_r($expenses); }
?>
#3

[eluser]al042077[/eluser]
The way I understand MVC is that data interaction is kept in the model. So, like Jan from Hamburg wrote, the $this->db->where clause is in the model, maintaining MVC separations. However, imagine the next time a user searches with other criteria such as:

Code:
$this->db->where('date >= ', $this->input->post('date_low'));
$this->db->where('amount >= ', $this->input->post('amount'));

Most examples I see show the controller looping over the $_POST-ed data and then adds $this->db->where for each search criterion. As far as I can tell, the way to maintain MVC separation would be:

controller.php
Code:
function process_search()
{
    $search_conditions = array();
    if(!empty($this->input->post('date_low')))
        $search_conditions[] = 'date >= ' => $this->input->post('date_low');
    if(!empty($this->input->post('amount')))
        $search_conditions[] = 'amount >= ' => $this->input->post('amount');
    // ... other conditions as needed
    $data['expenses'] = $this->calculation_model->get_expenses($search_conditions);
    $this->load->view('view_file', $data);
}

calculation_model.php
Code:
function get_expenses($conditions)
{
    foreach($conditions as $k => $v)
    {
        $this->db->where($k, $v);
    }
    $query = $this->db->get('expenses');
    if($query->num_rows() > 0)
    {
        $data = $query->result();
        return $data;
    }
}

view_file.php
Code:
if(isset($expenses)){ print_r($expenses); }

As I understand it in this way, the controller requests the data from the model but has no handling as to how the data is stored or retrieved. Thus, the controller controls the flow of processing, the model processes the request for data, and the view displays. Am I missing something? In this code above, it seems like a lot of extra work and makes the model activity redundant.

Thanks for your help and patience!!
#4

[eluser]bretticus[/eluser]
There is nothing wrong with your example above. Sending your 'search conditions' as an array built from the controller could be abstracted to the model instead. But to keep your model method more flexible, your implementation is perfectly adequate. I prefer to have model methods with a finite number of expected parameters however. For example, you would get an error in your model method when sending non-existent columns in your array. CodeIgniter doesn't handle this (queries are not checked for validity as that would be a huge performance hit no doubt.) For me, these query errors do not bubble out well. Better to control specifically what columns get search IMHO. Smile

In CodeIgniter, a model has pretty much the same access as a controller. IE: you can load libs, helpers, etc. This means that you may have model methods (and I'd encourage it) where you don't touch the database at all. The idea is that models are meant to be modular points in your application for grabbing data ( or just doing anything that doesn't involve application flow or layout. ) That's why you hear the notion of "Thin controllers and thick Models." This is better in the end, as you may have several controllers calling the same model.

So yes, controllers are an entry point into your application (they control application flow.) They control what arguments are sent to your models. Your models return data that the controller then sends to your views. Views define layout according to the data sent to them. Controllers control the data sent to the views and whether to show the results.

Quote:In this code above, it seems like a lot of extra work and makes the model activity redundant.
What extra work are you referring to? There are various ways you can process the data in the view.




Theme © iAndrew 2016 - Forum software by © MyBB