Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Ordering of multiple relations
  • I have 3 models:

    # Model_Food (id, name, kcal, ... )
    # Model_Recipe (id, name, kcal, ...)
    # Model_Bookmark (id, date, food_id, recipe_id)

    The bookmark model has a "belongs to" relationship to food and to recipe (but one bookmarkrow has only either a food_id or a recipe_id).

    Now I want to get a list of all bookmarks and related food/recipes ordered by the name of the food/recipe.

    I tried:

    $items = \Model_Bookmark::find( 'all', array(
       'related' => array(
      'food' => array(
    ???
      ),
      'recipe' => array(
    ???
      ),
       ),
    ???
    ));
  • The array notition of find() will quickly become very complex for queries like this, I personally never use it, I prefer method chaining, keeps things easier to read.

    What exactly is the problem. You said you tried, and I assume you post it because it failed, but what exactly failed? In case like this, which more complex questions, I usually try to write out the SQL myself, to understand the structure I need, then see if it can be converted to an ORM query (not all queries can), or if a DB query is needed.

    If you start with

    $result = \Model_Bookmark::query()->related('food')->related('recipe')->get();

    what is wrong with the result (apart from the ordering which isn't in there yet)?

Howdy, Stranger!

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

In this Discussion