CodeIgniter Forums
can i load another model inside model ? - 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: can i load another model inside model ? (/showthread.php?tid=53339)



can i load another model inside model ? - El Forum - 07-19-2012

[eluser]Mario "St.Peter" Valentino[/eluser]
hello all,

i wanna ask something about model. i have 2 model : level_model and user_model

Code:
// This is Level Model
Class Level_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function level_data()
    {
        //code goes here
    }
}

// This is User Model
class USer_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct()
    }

    public function user_data()
    {
       //code goes here
    }
}

i wanna make sure. can i load user model inside level model or vice versa ?
how to do it ?

please help me. Thanks




can i load another model inside model ? - El Forum - 07-19-2012

[eluser]solid9[/eluser]
The proper way is to load them both in the controller.
This way you can access them both.


can i load another model inside model ? - El Forum - 07-19-2012

[eluser]Aken[/eluser]
[quote author="solid9" date="1342752501"]The proper way is to load them both in the controller.
This way you can access them both.[/quote]
Nonsense. You can load a model inside a model all you want. You should only load them both in the controller if you think you'll need both in the controller. And you use the exact same syntax as you would in a controller.


can i load another model inside model ? - El Forum - 07-19-2012

[eluser]Mario "St.Peter" Valentino[/eluser]
so i can do like this :
Code:
class User_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(array('level_model'));
    }
    
    public function User_data()
    {
       //code goes here
    }
}

can i do like this include level_model inside user_model or vice versa ?




can i load another model inside model ? - El Forum - 07-19-2012

[eluser]Aken[/eluser]
Yes that's perfectly fine.


can i load another model inside model ? - El Forum - 07-19-2012

[eluser]Mario "St.Peter" Valentino[/eluser]
ok thanks my friend for helping me


can i load another model inside model ? - El Forum - 07-20-2012

[eluser]PhilTem[/eluser]
You don't have to use

Code:
$this->load->model(array('level_model'));

It's (slightly) faster to just use

[code]
$this->load->model('level_model');
[code]

since it won't loop over every element in the passed array Wink

Just as a side remark, but you can of course load one model in another model.