Welcome Guest, Not a member yet? Register   Sign In
can i model load another model
#1

[eluser]louis w[/eluser]
i think i remember the answer being no. but i thought i would ask anyway.

trying to write a more robust model class (ala cake's) and want to add relations to it, so that it will automatically get/update an item in a related table.
#2

[eluser]beemr[/eluser]
According to Rick Jolly in another thread it is possible via include/require

Personally, I'm happy using ActiveRecord Class Mod, the development version (0.7), for relationships ala Rails.

Also, there's an example of require once in this post
#3

[eluser]Mirage[/eluser]
Like beemr said, a simple php include_once directive will work fine. But you can also load additional models via the CI reference:

Code:
class SuperModel extends Model {

   function SuperModel() {
       parent::Model();

       $this->CI =& get_instance();

       // add relations
       $this->CI->load->model('ModelA', '' , true);
       $this->CI->load->model('ModelB', '' , true);
       $this->CI->load->model('ModelC', '' , true);

       // in your relational sub routines access with
       $this->CI->{modelname}
   }
}

HTH
-m
#4

[eluser]louis w[/eluser]
Mirage what is the third param in your load->model (true) doing? I do not see mention of this in the docs.

I ended up making helper which i call which loads the model and calls the desired method. Maybe not the best approach now but I didn't want to have include calls. I may just autoload all my models in the application to have them in memory but not sure if that would be bloated.
#5

[eluser]Mirage[/eluser]
[quote author="louis w" date="1219691134"]Mirage what is the third param in your load->model (true) doing? I do not see mention of this in the docs.[/quote]

It's in the docs, just below the 'Autoloading' paragraph. Passing true in the 3rd parameter causes the model to connect to the db automatically.


Quote:I ended up making helper which i call which loads the model and calls the desired method. Maybe not the best approach now but I didn't want to have include calls. I may just autoload all my models in the application to have them in memory but not sure if that would be bloated.

Well, whatever works for you and your application is good. Personally I'm not convinced that a helper is needed. It adds the complexity of an extra file to maintain for something fairly trivial. Autoloading all models is an option, but I'd agree that it's add unnecessary weight on the performance side. All of that for the sake of relations wouldn't do it for me.

But don't let that discourage you. I've seen so many attempts at ORM implementations and I don't really like any of them, mostly because of the bloat they represent. I think that CI's ActiveRecord approach presents an excellent medium for doing database work.

Cheers,
-m




Theme © iAndrew 2016 - Forum software by © MyBB