Loading a model without instantiating (OOP support in CI) |
[eluser]beardedlinuxgeek[/eluser]
I think this a pretty common situation. You have a model class and some other classes that extend that class. In this case we have a few different types of vehicles, each with an ID in the database. Code: class Vehicle extends CI_Model { I want to create my car I can just do Code: $car5 = new Car(5); But when I do Code: $this->load->model('vehicle'); I could do something like Code: function __construct ( $vehicle_id = null ) { Code: $this->vehicle->schedule_maintenance('Friday'); One thing I was thinking of doing is creating a new folder called "objects" and not having the Vehicle class extend CI_Model at all. But then I'd need to create a new function like Code: function set_db( $db ) { and then create my car from the controller like Code: $car5 = new Car(5); which really isn't a bad idea from an Inversion of Control perspective. But I also only have one database and only plan on having one, so the benefit from this IoC is really minimal. And let say I decide I need to send an email to the guy doing the maintenance, now I need to load the email class and do Code: $this->load->library('email'); Advice?
[eluser]CroNiX[/eluser]
You might try to manually include the model, instead of using CI's loader which automatically instantiates it. I haven't tried it, though. Code: include_once(APPPATH . 'models/your_model.php');
[eluser]beardedlinuxgeek[/eluser]
[quote author="CroNiX" date="1348099577"]You might try to manually include the model, instead of using CI's loader which automatically instantiates it. I haven't tried it, though.[/quote] When you call a model from a controller using this method it doesn't end up extending CI_Model. I'm not sure why, I didn't look into it. I could also do Code: class DoNothingModel extends CI_Model { But that also seems hackish. I know you can extend core classes, but I've only tried it with CI_Controller. I wonder what results you get if you try it with CI_Model. I'll have to try when I'm at my desktop tomorrow. I figured that creating a normal object class outside CI_Controller and CI_Model but letting it use the core classes/libraries would be pretty standard. I also had one more idea. I could do Code: class Vehicle { And at that point you might as well create a static "CreateObject" class which handles the dependency injection so you get all the benefits. Code: class CreateObject { Code: // Single line object creation This looks like a decent solution to me. Anyone see problems with it?
[eluser]alexwenzel[/eluser]
You can override the Loader. Check out this Code. I do exactly the thing you want there: https://bitbucket.org/alexwenzel/codeign...Loader.php
[eluser]beardedlinuxgeek[/eluser]
[quote author="alexwenzel" date="1348136028"]You can override the Loader. Check out this Code. I do exactly the thing you want there: https://bitbucket.org/alexwenzel/codeign...Loader.php[/quote] Works perfectly. I have to wonder why this isn't the default behavior. It makes more sense to treat models as objects and if you need the current behavior where you use a model statically, why not just declare a static function? Can someone explain why it's the way it is? The engineers at EllisLabs are pretty smart guys, so I'm sure I'm just misunderstanding something.
[eluser]alexwenzel[/eluser]
[quote author="beardedlinuxgeek" date="1348153614"][quote author="alexwenzel" date="1348136028"]You can override the Loader. Check out this Code. I do exactly the thing you want there: https://bitbucket.org/alexwenzel/codeign...Loader.php[/quote] Works perfectly. I have to wonder why this isn't the default behavior. It makes more sense to treat models as objects and if you need the current behavior where you use a model statically, why not just declare a static function? Can someone explain why it's the way it is? The engineers at EllisLabs are pretty smart guys, so I'm sure I'm just misunderstanding something.[/quote] I guess its because they attach every model per default to the codeigniter class, so you can call them via: Code: $this->model->something();
[eluser]beardedlinuxgeek[/eluser]
[quote author="alexwenzel" date="1348155244"] I guess its because they attach every model per default to the codeigniter class, so you can call them via: Code: $this->model->something(); Yeah, but why do it like that? Your way gives us more control over our models. The only reason not to use your way is when you want to use the functions in your model statically. In which case writing Code: Model::something(); Code: /** Code: $this->user->get_userlogin(); Sorry if this sounds like complaining, it's not meant to be. I just see a ton of reasons to load models like this and not a single reason not to.
[eluser]alexwenzel[/eluser]
If you like you can checkout my whole model override. Its currently in version 1.0 but i will release the next day next version which brings a lot more comfort with. Simple Model for CodeIgniter: forum: http://ellislab.com/forums/viewthread/223845/ code: https://bitbucket.org/alexwenzel/codeigniter-model/ |
Welcome Guest, Not a member yet? Register Sign In |