Extending CI_Model more than once in applications/core |
[eluser]glevine[/eluser]
I'd like to have multiple core models extending from CI_Model. I don't know why it wouldn't work, but I can't seem to get it right. Here's what I'm hoping to do (in a very basic way): Code: // applications/core/MY_Object.php Code: // applications/core/MY_Peer.php Code: // applications/models/carobject.php Code: // applications/models/carpeer.php I get the following error: Quote:Fatal error: Class 'MY_Peer' not found in /var/www/application/models/carpeer.php on line 3 Any thoughts as to what I could be doing wrong? My custom controller extends from CI_Controller just fine. Can there be only one custom core class that extends a CI core class? Thanks
[eluser]aquary[/eluser]
EDIT: Just saw your last lines, You are extending something not declared yet. It works when you extend a model from CI_Model or MY_Model, not something else. You might have to include the MY_Peer file manually also. In the application/core folder, there can be only classes extend from CI core library only. Else they won't be used/included autometically. Also, I think in your case, you have to include the carobject.php file before loading it, maybe? I don't think CI could handle the structure which is not their native's... Something like this ? not tested though... looks weird in my sense. Code: include_once(APPPATH.'core/MY_Peer.php'); Or, to follow CI structure, You could merge some class into MY_Model, and move some part of CarObject into CarPeer instead...? Edit: move content to make comments more... followable. Continue from above... Code: // applications/core/MY_Model.php Code: // applications/models/carpeer.php Not tested nor optimization.... but around this concept?
[eluser]glevine[/eluser]
The following line in the CarPeer constructor should take care of loading the CarObject class for me, as load->model() looks in the models dir. Code: $this->load->model('CarObject'); I'd hate to be forced to add my own includes if CI has already built a construct to do that for me. I'm not sure what you mean by the structure not being native to CI. I followed exactly the instructions for extending a core class (http://ellislab.com/codeigniter/user-gui...asses.html), in this case CI_Model. The only difference is that I'm extending it twice.
[eluser]aquary[/eluser]
The $this->load->model('CarObject') do load CarObject into CI, yes. But your CarPeer is extending from MY_Peer, which is unknown by CI...
[eluser]glevine[/eluser]
Well the point of this exercise is that I don't want my model to talk to the database. I want my peer to do that. Essentially the peer would be a singleton that would deal with generating the objects that I need and the model would simply contain the state of the object. So creating a method named CarObject() kind of defeats the purpose. But thank you for your help. I just may not be able to do what I was hoping.
[eluser]aquary[/eluser]
You could, you just have to include the Peer object (not MY_Peer) in MY_Model. Then you can use your Peer anywhere in any model. It'd work if the Peer is a standalone class, but instead, it relies on CI_model which is... hmm, like I said, not in a common CI's structure. Instead of MVC, yours are... Hidden-MVC? I think my brain is spinning now @_@
[eluser]glevine[/eluser]
How is CI_Model not in a common CI's structure? CI_Model is the base model class for CI, located in system/core. As long as you don't replace it, it is loaded for every request. All that said, I looked at how CI loads models. Every time you call $this->load->model('foo'); you get the same instance of Foo. So if you need multiple instances of Foo in a single request, then loading the model isn't really what you want. It's like CI models are inherently singletons, just written in a weird way. When the model is loaded the first time, the name of the model is added to Loader::_ci_models and the CI super object ($CI) gets a property that references the instantiated object. After that, anytime you load the model again during the same request, you are referencing the same object. So really, my Peer class should inherit from CI_Model and my models should become a new class hierarchy that simply stores the state of an object. The Peer classes can directly include the object class that they support.
[eluser]InsiteFX[/eluser]
When you extend multiple class in core you need to use Phil's Autoloader Code: /*
|
Welcome Guest, Not a member yet? Register Sign In |