Welcome Guest, Not a member yet? Register   Sign In
Just what is a "Model" supposed to be in CI?
#3

[eluser]Yorick Peterse[/eluser]
I'm not an expert when it comes to the theoretical part of OOP and the MVC structure. As far as I understand Controllers are your main "things", they load the models and views, perhaps do some validation etc. The models are being used to work with data retrieved from a database. The views simply display the results.

For example you would have an index() function that calls a model which will fetch all users:

Controller
Code:
<?php
controller Test extends Controller {
  function index() {

    //First load a model
    $this->load->model('test_model');
    
    //Execute the get_users() function that's stored inside the model and store the results into an array
    $data = array (
            'result'    => $this->test_model->get_users()            
    );

    //Load the view with the results
    $this->load->view('some_view',$data);
  }
}
?>

Controller
Code:
<?php
class Test_model extends Model {
  function get_users() {

  //Create a query
  $query = $this->db->get('users');

  //Return the results
  return $query->result();
  }
}
?>


Messages In This Thread
Just what is a "Model" supposed to be in CI? - by El Forum - 05-01-2009, 03:02 PM
Just what is a "Model" supposed to be in CI? - by El Forum - 05-03-2009, 05:22 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 05-03-2009, 07:11 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 05-03-2009, 07:17 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 08-19-2009, 07:53 PM
Just what is a "Model" supposed to be in CI? - by El Forum - 08-20-2009, 05:47 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 08-20-2009, 06:25 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 07:42 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 07:46 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 07:53 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 09:12 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 09:45 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 09:56 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 10:07 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 11:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB