Welcome Guest, Not a member yet? Register   Sign In
Calling one model from another model, how?
#1

[eluser]Gewa[/eluser]
Hi, How can I load one model from another model? Or one in one function of model call another function of model?
#2

[eluser]The Wizard[/eluser]
yeah surely
load it and access it as normal Smile
#3

[eluser]TheFuzzy0ne[/eluser]
You will need to call $this->_assign_libraries() a second time on the first model after it's loaded the second model, otherwise the first second model will not be available to the first.
#4

[eluser]Gewa[/eluser]
The FuzzyOne can you give a detailed example?


lets say in Foto_model.php I have function foto_data()


Code:
function foto_data($foto_id){




$this->load->model('ratings_model');

$this->_assign_libraries() ;
$foto_rating=$this->ratings_model->foto_rating($foto_id);



}

Like this???
#5

[eluser]TheFuzzy0ne[/eluser]
I sit corrected. Your best bet would be to create a class property which references the $CI Super Object when the model is instantiated:

Code:
function __construct()
{
    $this->CI =& get_instance();
}

and then you can access other models that way:

Code:
$this->CI->some_model->some_method();
#6

[eluser]srisa[/eluser]
I load the model in the constructor and use it normally everywhere else. In your example, $this->load->model('ratings_model') should go in the constructor of foto_model.php. You can use the functions of ratings_model in other functions as you are doing now.
#7

[eluser]Gewa[/eluser]
Thank YOU ALLL!!!!
#8

[eluser]Zac G.[/eluser]
Why is it that you still have to load the model manually even if it's loaded in the auto loader?
#9

[eluser]TheFuzzy0ne[/eluser]
You don't. What makes you say that?
#10

[eluser]Zac G.[/eluser]
For some reason, it wasn't working for me... I did that and then it worked? I must have made some other change as well...hmmm




Theme © iAndrew 2016 - Forum software by © MyBB