CodeIgniter Forums

Full Version: Can you load models within models?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I'm getting the following error:

Quote:Fatal error: Call to a member function total_purchases_amount() on a non-object in C:\Apache2.2\htdocs\Local_Water_Supply_Plan\application\models\calculations_model.php on line 311

Here is the code the error references from my calculations_model.

function total_available_supply($pwsid, $year) {
        $purchases = $this->purchases_model->total_purchases_amount($pwsid, $year);
        $surface = $this->surface_model->total_surface_amount($pwsid, $year);
        $ground = $this->ground_model->total_ground_amount($pwsid, $year);
        return $purchases + $surface + $ground;

As you can see I'm trying to load some other models within my calculation model. Can you do this? Is that why it is throwing the error? Any help would be awesome.

El Forum

I've read a couple posts related to loading models within models. I understand that the only way to do it is to use the CI super object. I do understand why you probably should find a way to keep from having to load models inside of models. What I'm trying to do, and the reason I'm even having this problem is the following.

I have a table called "a" and a table called "b" that each have a model. Obviously in each of those models I have functions that access the data respectively. But I also need to do some calculations that involve data from both tables, so I've created a new model called calculations that basically just needs access to some functions in each of the other models to complete calculations.

How do I manage this type of thing? Is this where I would need to create a library instead of a model? In a way that would make sense. Am I on the right track?

El Forum

I don't usually like relating a model with a database table... I prefer using models as table groups... there is no table completely isolated. If you prefer to continue as you were going, put on the model you want... probably where it looks simplier =)