• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can i load another model inside model ?

#1
[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


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

#3
[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.

#4
[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 ?


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

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

#7
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.