Lazy Load not working?

edited September 2015 in Modules
Hi,
I am trying to use the lazy loading due to performance optimizations...

In my fahrzeuge_model I have an belongs_to relation to missions_model
public $belongs_to = array('missions' => 'missions_model');

Now in the Fahrzeug_model I have
public function get_missions() { $miss = $this->lazy_load(array('id' => $this->mission_id), "missions_model", true, array('order_by' => 'datum_beginn desc, uhrzeit_beginn desc', 'limit' => 10)); return $miss; }
If I debug the returned data it displays null... I call $object->get_missions();

What is wrong?

Comments

  • edited 10:57AM
    What if you run $this->debug_query(); right before return $miss? What does that SQL look like and does it work if you try that SQL statement manually?
  • edited 10:57AM
    In my model I have
    public function get_missions() { $miss = $this->lazy_load(array('id' => $this->mission_id), "missions_model", true, array('order_by' => 'datum_beginn desc, uhrzeit_beginn desc', 'limit' => 5)); $this->debug_query(); return $miss; }
    In the view I have
    <? $a = $data->get_missions(); die(); ?>

    The SQL produced is:
    SELECT `fw_fahrzeuge`.* FROM (`fw_fahrzeuge`) WHERE `fw_fahrzeuge`.`id` = '3' AND `fw_fahrzeuge`.`published` = 'yes' LIMIT 1

    That is very strange because I lazy load the fw_missions table via the missions_model...
  • edited 10:57AM
    Sorry try running:
    CI()->missions_model->debug_query();
  • edited 10:57AM
    Is it possible to lazy load objects from a has_many relationship?
  • edited 10:57AM
    I'm not quite sure I understand the question completely. Do you have an example?

    One thing to note that if you have a has_many relationship like say a "tags" has_many relationship on an articles model, you can do the following to return the model instance back with the "where_in" active record already assigned to it to do further active query manipulation:
    $tags_model = $article->get_tags(TRUE); $tags = $tags->find_all(array('context' => 'article'), 'name asc');
  • edited 10:57AM
    So I have a vehicle model which has a has_many relationship to missions.
    In the frontend I want to show only the last 5 missions of the vehicle.
    Because of this I want to lazyload the relationship.

    At the moment I overloaded the get_missions with a new sql to achieve this.
  • edited 10:57AM
    Relationships are currently always lazy loaded.
  • edited 10:57AM
    Ok... but for my requirement to just load the last 5 entries I think the best way is to overload the get_missions() with my custom sql, isn't it?
  • edited 10:57AM
    You could do the following:
    $missions = $rec->get_missions(TRUE)->find_all(array(), 5);
    The TRUE, returns the model with the wherein already applied instead of the IDs which allows for further active record querying (or limiting in your case)
  • edited 10:57AM
    Ok, I will try this. Thanks!
Sign In or Register to comment.