Welcome Guest, Not a member yet? Register   Sign In
Using multiple models with MY_Model - Best Practices?
#1

[eluser]Dan King[/eluser]
I've been using MY_Model to extend the core model class. In MY_Model I had a few bits of code in the constructor that would run, such as user auth, analytics etc. This is fine when there's only one model involved, but when loading several models, the code in the MY_Model constructor gets executed multiple times.

For example, I have the following models:

models/Blog_model.php
Code:
class Blog_model extends MY_Model {
    __construct(){
        parent::MY_Model();
    }
}

models/Events_model.php
Code:
class Events_model extends MY_Model {
    __construct(){
        parent::MY_Model();
    }
}

libraries/MY_Model.php
Code:
class MY_Model extends Model {
    __construct(){
        parent::Model();
        //do some db queries
        //check for user login etc.
    }
}

So in the controller I would load the blog model and events model, but the MY_Model constructor code gets executed twice ... and I don't need it to. Is there a proper/better way to do this? Thanks!
#2

[eluser]whitey5759[/eluser]
The code you have included in you MY_Model (ie. checking user login, etc) should really moved and placed in a MY_Controller, which all your Controllers extend.




Theme © iAndrew 2016 - Forum software by © MyBB