Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Get 1 result with Orm\Model
  • First of all - Fuel is awesome. Thank you so much for such a great framework.

    Next, my question - a simple one. I want to get the name of an entity by it's ID, so I query like so:

    $regionArr = Model_Region::query()->select('name')->where('id', '=', $product->region_id)->get();

    Now I want my result:

    foreach ($regionArr as $f){
    echo $f->id;
    }

    However, I know there is only 1 result. I know Model_CRUD has find_by_one - does the ORM package have anything similar? This works perfectly well, but it's a bit messy for getting 1 column of 1 row.

    Cheers,

    Paul
  • HarroHarro
    Accepted Answer
    $region = Model_Region::find($product->region_id);

    And if you have setup your relations correctly (given the query you give you have "one region has many products"), you can also do

    echo $product->region->id;

    ( assuming you have called the relation between the two 'region').
  • This

    echo $product->region->id;

    is genius. I had the relations set up, but totally forgot that syntax

    Thank you.
  • HarroHarro
    Accepted Answer
    Note that this syntax will fire an extra query if the relation isn't loaded.

    If you know up front you need the related data, include the relation in the query, either in the find() with the options array, or with query() using related(). That will produce a more efficient join to fetch the data.

Howdy, Stranger!

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

In this Discussion