CodeIgniter Forums
Calling Models and loopback? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Calling Models and loopback? (/showthread.php?tid=26648)



Calling Models and loopback? - El Forum - 01-19-2010

[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?


Calling Models and loopback? - El Forum - 01-19-2010

[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();



Calling Models and loopback? - El Forum - 01-20-2010

[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');
}



Calling Models and loopback? - El Forum - 01-21-2010

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