CodeIgniter Forums
Codeigniter MVC!? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Codeigniter MVC!? (/showthread.php?tid=57211)



Codeigniter MVC!? - El Forum - 02-26-2013

[eluser]SaSa[/eluser]
What is the difference between this three way and Which one is best? What do you suggest?

1.<br>
Use of query (db) directly in CI_Controller. as:
Code:
$id = $this->input->post('id');
    $query = $this->db->get_where('table1', array('id' => $id))->row();
             $this->db->get_where('table2', array('idre' => $query->idre));

OR

2.<br>
Use of query (db) in CI_Model and call it in CI_Controller. as:

CI_Model:
Code:
function GetWhere($table,$where){
        return $this->db->get_where($table, $where);
    }
CI_Controller:
Code:
$id = $this->input->post('id');
    $query = $this->model_name->GetWhere('table1', array('id' => $id))->row();
             $this->model_name->GetWhere('table2', array('idre' => $query->idre));

3. <br>
I use the following code true in model as mvc?
Code:
$id = $this->input->post('checked');
            foreach ($id as $val) {
                $query_check = $this->db->get_where(table3, array('id'=>$val));
                $row = $query_check->row();
                if($query_check->num_rows() > 0 || $row->id != $this->session->userdata('id')){
                    foreach ($query_check->result() as $value) {
                        $id_relation = $value->id_relation;
                        $this->db->where('idre', $idre)->delete(array(table3, 'table4'));
                    }
                }
            }
Whether this is a good example of mvc? How can fix 2 example above for good mvc? ( I want to know if this is correct MVC and, if not, how to change it)


Codeigniter MVC!? - El Forum - 02-27-2013

[eluser]boltsabre[/eluser]
Option 2 it the better way to do it. Basically your model should handle all your "data" related functionality, your controller acts as the "between man" interacting between view and model.

There are several approaches towards this:
- Fat Controller, Skinny Model (where you do all your form validation in the controller for example, and your model simply runs db queries)
- Skinny Controller, Fat Model (where the controller basically just loads the model and view, and sends the data to the model which does the form validation, db queries, etc).

I suggest you run through some of the online video tutorials if you're still a bit confused about it. While most wont go into the fat vs skinny debate, they give you a basic, yet solid, understanding of what / how MVC operates and what tasks should be delegated to your controller, model, etc.