Welcome Guest, Not a member yet? Register   Sign In
Calling Models and loopback?
#1

[eluser]bunal[/eluser]
Hi Igniters,

I have 2 model which are modelA and modelB

Sometimes because of the logic i may need to access methods of ModelA from ModelB and vica versa.

So i am loading the ModelB in the contructor of ModelA and load ModelA in the contructor of ModelB that causes loopback?

How to prevent this from being happening?
#2

[eluser]danmontgomery[/eluser]
[quote author="bunal" date="1263953597"]Hi Igniters,

I have 2 model which are modelA and modelB

Sometimes because of the logic i may need to access methods of ModelA from ModelB and vica versa.

So i am loading the ModelB in the contructor of ModelA and load ModelA in the contructor of ModelB that causes loopback?

How to prevent this from being happening?[/quote]

Don't load them in the constructor, load them in the functions you need them

Code:
$ci =& get_instance();
$ci->load->model("modelA");
$ci->modelA->some_function();
#3

[eluser]attos[/eluser]
Use PHP's class_exists function:

In ModelA (Using CI's coding conventions):
Code:
if (class_exists('ModelB')) {
    $this->load->model('ModelB');
}

And in ModelB:
Code:
if (class_exists('ModelA')) {
    $this->load->model('ModelA');
}
#4

[eluser]bunal[/eluser]
Thanks much. Both worked like a charm




Theme © iAndrew 2016 - Forum software by © MyBB