Weird behaviour in $this->model->load() - 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: Weird behaviour in $this->model->load() (/showthread.php?tid=4731) |
Weird behaviour in $this->model->load() - El Forum - 12-12-2007 [eluser]Unknown[/eluser] Hi guys, I was wondering if you could shed a little light on my current dilemma. Basically, I'm using $this->load->model(X) and $this->load->model(Y) Both files X and Y exist, are identically located with identically formatted names ('posts' and 'pages' in minuscule). Both models X and Y are at the moment identical apart from their name (and the appropriate function name changes). However, I can call $this->X but not $this->Y - see the error message Code: A PHP Error was encountered Best explained through code, the relevant files (various comments removed) are below: Model: Sidebars (application/models/sidebars.php) Code: <?php Model: Pages (application/models/pages.php) Code: <?php Model: Posts (application/models/posts.php) Code: <?php Note: The class SpecialActiveRecord is just an extension to the standard Model class. It actually provides me with quick shortcuts to the combination of ActiveRecord functions I use most (another layer of abstraction! yipee). This shouldn't be causing the error as it is identically loaded by both X and Y classes! --- So basically the first call to $this->pageList() is working fine but not $this->popularPosts(); Many thanks for any help you can give me! Weird behaviour in $this->model->load() - El Forum - 12-12-2007 [eluser]gtech[/eluser] You cant load a model within a model like that outside the constructor... has the pages model been loaded outside the model and thats why it works? what you could try is to load the models in the constructor Code: <?php give it a go see if it works I may be on the wrong track its only a guess. you can also try this Code: class Sidebars extends Model Weird behaviour in $this->model->load() - El Forum - 12-12-2007 [eluser]tonanbarbarian[/eluser] Ok Firstly I am not sure if you load a model from a model that the new model will be available inside the original model, only in the controller. Not 100% sure on that so that might be the issue. More likely though is that you are doing the following... Code: $this->load->model("Posts"); Code: $this->load->model("Posts"); Weird behaviour in $this->model->load() - El Forum - 12-12-2007 [eluser]gtech[/eluser] [quote author="tonanbarbarian" date="1197520454"]Ok Firstly I am not sure if you load a model from a model that the new model will be available inside the original model, only in the controller. Not 100% sure on that so that might be the issue. [/quote] [edit] you may of have a point tonanbarbarian, as you often give good advice so I tested your theory, however the results suggested the following: Code: <?php Result: hellohello Which means even though I load Mod1 outside Mod2 in the controller, I can still reference Mod1 inside Mod2. This means in theory at least Adrien could of loaded the pages model before you load the sidebar model and maybee thats why one works and one does not. Adrien is loading a model within a model and I know in CI you cant do this by using the normal $this->load->model outside the constructor as I have been stung by this before. You either need to load the model/s in the constructor of the model (you will be able to reference the methods outside the constructor) OR use the CI instance as described in reply 1. Weird behaviour in $this->model->load() - El Forum - 12-13-2007 [eluser]Unknown[/eluser] Thanks guys. I'd tried using the CI instance and it worked, I forgot to mention this in my first message. I was wondering if there was a way of doing it without using the CI instance (which is a rather ugly solution) but gTech's suggestion works: load the models within the constructor. I guess it'll be a tradeoff decision: load all my models within the constructor or using the CI instance to limit the number of modules being loaded to the minimum necessary (the sidebar model is likely to have the ability to loadover a dozen modules). Cheers though! Weird behaviour in $this->model->load() - El Forum - 12-13-2007 [eluser]gtech[/eluser] [quote author="MPress" date="1193887618"]The problem is in the 'model' method of Loader.php. You just have to add Code: $this->_ci_assign_to_models(); I'll reference this thread in a bug report and hopefully this will be remedied in the next release.[/quote] Mpress found the above solution so you can load the models in the 'normal' way, and it does work however I do not know what the trade offs are (if any). |