Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Eager loading
  • I would like to eager load automatically some relations on some models. Is it implemented, or may I overload the query method so that I add some "->related('i_want_it')" before returning the query ?
  • HarroHarro
    Accepted Answer
    If you specify the relation on the query, either in the find() array, or using ->related(), it will eager load the related objects.

    There is currently no option to include a relation in the result by default, and not easy to fix since the ORM Query class is not extendable.

    What you could do is to overload the query() method in your model. It is defined as

    public static function query($options = array())

    and in the options array you can have a 'related' => array() entry, which defines the relations to eager load, same syntax as on the find() call. You could inject your default relations in that array, and then call the parent:

    // make sure the entry exists
    isset($options['related']) or $options['related'] = array();

    // add the required relations
    in_array('i_want_it', $options['related']) or $options['related'][] = 'i_want_it';

    // create the query object and return it
    return parent::query($options);
  • Thanks !

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion