views:

99

answers:

5

Can somebody explain to me when it's a good practice to use models in CI? An article in wikipedia was referring to CI models as "entirely optional and are rarely needed" is that true in any way?thanks in advance!

+1  A: 

No!! That's not true. Models are used as a layer between your controller and the database. MVC best practice is a subjective matter at best. People use it in different ways. A framework like CodeIgniter is great because it allows that level of flexibility. Other frameworks are much more strict. I'm general I try and keep my controllers very small and specific and put lots of code (db interaction and parsing results etc) in a model.

musoNic80
+1  A: 

Well a "model" reprensents data, most likely from a database (meaning your rows are returned from the database and the model holds the send data, simply said). There is no need to use the database layer given by CI - that is true- you can replace with another if you like, but that they are "rarely used" is simply not true.

DrColossos
A: 

You should be using models to avoid replicating code (don't repeat yourself). I think most (nearly all) applications built with codeigniter would use models of some sort.

Perhaps the article is referring to the active record class, which is optional to use in models. Active record I find to be good for create, update and deletion but you'll most likely need sql for any slightly complex reads. It's worth noting that it's not just databases you can use in models. Web services or flat files or whatever source of data can be used in a model.

I've been thinking of the models as a database layer only. They should not be, this creates bloating in your controllers. Using fat-controllers will slow you down in the end. Put your entire model structure in the model if you can and simply use the controller for logic.

Keyo
A: 

Models are for business logic(in ideal cases.. might be for debate, some people put buiness logic in controller) and DB connectivity in the MVC framework.

Read codeigniter user guide for more info on models.

http://codeigniter.com/user_guide/general/models.html

rahijain
+2  A: 

Say you needed to call a function called get_user_info that retrieves the users info from a database. You could have a function like this:

class Home extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $user = $this->get_user_info($_SESSION['user_id']);
        echo "Hello " . $user['first_name'];
    }

    function get_user_info($user_id) {
        $query = $this->db->query("SELECT * FROM users WHERE user_id = ?", array($user_id));
        return $query->row_array();
    }
}

However, what if you needed to call get_user_info on another page?

In this scenario you would have to copy and paste the function into every page making it difficult to maintain if you have lots of pages (what if you needed to change the query to JOIN onto another table?)

This also goes against the don't repeat yourself principle.

Models are meant to handle all data logic and representation, returning data already to be loaded into views. Most commonly they are used as a way of grouping database functions together making it easier for you to change them without modifying all of your controllers.

class User extends Model {

    function __construct() {
        parent::Model();
    }

    function get_user_info($user_id) {
        $query = $this->db->query("SELECT * FROM users WHERE user_id = ?", array($user_id));
        return $query->row_array();
    }

}

In the above we have now created a model called user. In our home controller we can now change the code to read:

class Home extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->load->model('user');
        $user = $this->user->get_user_info($_SESSION['user_id']);
        echo "Hello " . $user['first_name'];
    }
}

And now you can change your get_user_info function without having to change X number of controllers that rely on the same function.

fire
"Models are just a way of grouping database functions together making it easier for you to change them without modifying all of your controllers." --- Models are much more than a means of grouping database functions together; they are meant to handle all data logic and representation, returning data already to be loaded into views.
DRL
@DRL Yes, I totally agree. At first I thought they were just a database layer then I realized I was creating messy code thinking this way. Encapsulate all the model related logic in the model and everything is so much simpler.
Keyo
@DRL thanks I have updated my post!
fire