CodeIgniter Forums
Chicken and Egg Models Dont Load -- Begack! - 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: Chicken and Egg Models Dont Load -- Begack! (/showthread.php?tid=16445)



Chicken and Egg Models Dont Load -- Begack! - El Forum - 03-06-2009

[eluser]smatakajr[/eluser]
Hey

This is driving me NUTzO

Basically I have a and a Mail_model and an Affiliate_model
They all have different controller names they all are checked for parse
and db errors. SO Basically:

The affiliate_model loads the Mail_model
The Mail_model loads the Affiliate_model

For some friggin reason the Affiliate_model wont load the
Mail_model but the Mail_model will load the Affiliate_model

I get a blank screen no errors just confusion.

Please let me know is anybody else has this chicken and egg issue
with a workaround. I tried doing a regular PHP include but
that doent work out niether

Thanks
Rick


Chicken and Egg Models Dont Load -- Begack! - El Forum - 03-06-2009

[eluser]jedd[/eluser]
I suspect the general workaround is to not do it Wink

Can you rip the common / reliant code out and stick it in a library? I reckon that'd be your best bet, and that way both models can reference the functions from the library.

(I don't think two models should ever need to rely on each - if they do it probably means you've designed the scope of your models sub-optimally.)


Chicken and Egg Models Dont Load -- Begack! - El Forum - 03-06-2009

[eluser]TheFuzzy0ne[/eluser]
Are you using PHP 4? If so, that's probably the problem, in which case you'll need to call upon the loader within each function that requires the other model, as you will have problems doing it from within the model constructors.


Chicken and Egg Models Dont Load -- Begack! - El Forum - 03-06-2009

[eluser]Colin Williams[/eluser]
You are using a reference to the controller object, right? E.g.
Code:
$CI =& get_instance();
$CI->load->model('affiliate_model');



Chicken and Egg Models Dont Load -- Begack! - El Forum - 03-09-2009

[eluser]TheFuzzy0ne[/eluser]
I think I know what your problem is.

When you load the first model, the _assign_libraries() is called, which assigns the variables from the CI Super Object. When you load a model from within within that model, the model is loaded, and _assign_libraries creates a variable in the CI Super Object which is imported into the second model (via _assign_libraries), so you can access the first model. However, the first model doesn't have a reference to the second model, that's just been loaded. You'd have to call _assign_libraries a second time in the first model to have access to the second model. Hope this makes sense.

So in a nutshell, you have to call the second model via the CI Super Object if you don't call _assign_libraries a second time.

EDIT: Nuts! I just realise that this was Colin's point... Sad