Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading and using a codeigniter model from another model

Fellow coders, using codeigniter 1.7.3 can I load a model from the code of another model? I have read many posts theoretical and practical but none gave a final answer.

I have a model that has a function in which i would like to perform an operation on another model. the code is like this:

1: $this->load->model('decision_model');
2: $this->decision_model->hello_decision();  

line 1 works. line 2 fails as follows:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Account_model::$decision_model
Filename: models/account_model.php

I have tried creating simple dumb models, changed function names, giving the model an alias when loading it, etc... no luck

So, theory aside, is this doable?

thanks in advance.

like image 986
djeetee Avatar asked Jan 07 '11 19:01

djeetee


People also ask

Can we call a model from another model?

Yes, you can call a method from another model in a model in Code Igniter. You need the model you are calling a method on to be loaded. If you autoload all your models, it will always work.

Can the CodeIgniter view be load from the model?

You don't access the model from the view. You access it from the controller and provides the output to the view.


2 Answers

Try this:

$this->load->model('decision_model');
$CI =& get_instance();
$CI->decision_model->hello_decision(); 
like image 186
user516322 Avatar answered Oct 06 '22 06:10

user516322


In CI 2.0 you can just call one model directly from another.

like image 27
phirschybar Avatar answered Oct 06 '22 06:10

phirschybar