Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Relationships Question
  • I have two questions related to ORM relationships. #1. When I write something like this...
    $query = Model_Part::find('part#')->related('inventory');
    

    I get an error: BadMethodCallException [ Error ]: Call to undefined method Model_Edp::related() Is this not possible? Running this seems to work fine:
    $query = Model_Part::find('part#', array('related' => array('inventory')));
    

    In the documentation it seems to imply that it could be done either way. Is there something I"m missing here?
    Also is there a way to chain multiple relationships together? Say I want Model_Series (related to) Model_Part (related_to) Model_Inventory; Something like:
    $query = Model_Series::find('Series#')->related('part')->related('inventory')
    

    This obviously giving me the series i'm searching for, all of the related parts in that series, and all the inventory information related to those parts in one swoop.
  • find() always returns a single result object, unless you don't pass an argument, pass null, or pass 'all'. In those cases it returns a query object you can chain on. So either use array notation (like in your second example), or chain ->where('partnr', '=', 'part#')-> to your query instead of using it as a find() parameter. Basically find(), find(null) and find('all') are aliases for Model::query(). The first version of the ORM only had array notation, and didn't expose the query object. Because a lot of people prefer chaining, we decided to expose it later. But the find() static method stayed for backward compatibility reasons. Ideally, when you want to chain use query() instead of find().

Howdy, Stranger!

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

In this Discussion