• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you load models within models?

#1
[eluser]jleequeen[/eluser]
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.

Code:
function total_available_supply($pwsid, $year) {
        $this->load->model('purchases_model');
        $this->load->model('ground_model');
        $this->load->model('surface_model');
        
        $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.

#2
[eluser]jleequeen[/eluser]
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?

#3
[eluser]Seppo[/eluser]
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 =)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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