Loading a model within a model. |
[eluser]MPress[/eluser]
This doesn't seem to be working for me. $this->load->model('whatever'); will work but no $this->whatever reference is created.
[eluser]MPress[/eluser]
Got it. You need something like this in a base model: Code: function load_model($model){
[eluser]gtech[/eluser]
Not quite sure what your getting at, but I have managed to load a model within a model e.g. Code: class Usersdb extends Model
[eluser]MPress[/eluser]
That doesn't seem to work for me. I believe what's going on is that loading a model using $this->load->model is actually creating the original reference in the CI instance and these references are only copied over (localized) to the model upon construction. Therefore, any other model loaded post-construction of the subject model will only have a reference in the CI instance (controller) and not in the model itself. What could be happening in your case is that these models were already previously loaded and so the references have already been localized within the subject model (on construction). My workaround fixes this problem, but if something else is going on here, perhaps it is unnecessary.
[eluser]CodeOfficer[/eluser]
this works great for me: Code: class MyModel extends Model
[eluser]gtech[/eluser]
Ok I get you, I only load my models within the model constructor and thats why it works in my case, if I moved the load outside the constructor it probably wont work.. Thanks for the info.
[eluser]MPress[/eluser]
[quote author="gtech" date="1193848097"]Ok I get you, I only load my models within the model constructor and thats why it works in my case, if I moved the load outside the constructor it probably wont work.. Thanks for the info.[/quote] No, I believe it works in your case because those models were already previously loaded in your CI instance (somewhere in your controller), before the construction of the subject model. At least this has been my experience.
[eluser]MPress[/eluser]
[quote author="CodeOfficer" date="1193827453"]this works great for me: Code: class MyModel extends Model So you manually code wrapper methods anytime you want to utilize a method of a secondary model within your subject model? Not sure if I'm a fan. With the above load_model function that I whipped up, you can simply do the following: Code: $this->load_model('model2');
[eluser]gtech[/eluser]
Quote:Quote: Ok I get you, I only load my models within the model constructor and thats why it works in my case, if I moved the load outside the constructor it probably wont work.. Thanks for the info.No, I believe it works in your case because those models were already previously loaded in your CI instance (somewhere in your controller), before the construction of the subject model. At least this has been my experience. Hello, I wrote a little test that loads a model within a model, and works: The controller mod.php Code: <?php then two models testmod.php Code: <?php testmod2.php Code: <?php when I call http://localhost/..../index.php/mod/modtest it prints out: Code: hello1..hello2 Is this behavior you would expect?
[eluser]mdg5w[/eluser]
That is the behavior I would like, however it is not working. And I'd like to avoid rewriting code. |
Welcome Guest, Not a member yet? Register Sign In |