Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Issue on selecting an ORM with relations
  • Hi.
    I have the ORM with many relations which can be described like this
    $model = \Model\Orm\Product::query()
    ->related('category') // has one relation
    ->related('category.products') // has many relation
    ->where('id', '=', 1)
    ->get_one();

    echo count($model->category->products); // The answer is always "1"
    I get this error on both package versions 1.8 and 1.9/develop, but in 1.5 version it worked correct. How can I resolve it?


  • You're sure the is no rows_limit() included in the query code? The example you give should return 1 product, 0 or 1 category, and 0 or more category products. This is the default query behaviour.

    Have you checked with the profiler what SQL is generated? And what could be wrong with it? 
  • " and 0 or more category products. "
    I wrote it wrong, in my case the answer is always 1 if the number of category products is 1 or more.

    I understood what the problem is. 
    It's due to the previous post Issue. The problem occurs with the relations deeper than level 2, under condition that cach is disabled in the class fuel/packages/orm/classes/query.php:1408
    $obj = $this->from_cache ? Model::cached_object($pk, $model) : false;
    The same can be obtained by running the following code
    $model = \Model\Orm\Product::query()
    ->related('category') // has one relation
    ->related('category.products') // has many relation
    ->where('id', '=', 1)
    ->from_cache(false)
       ->get_one();

  • You have to be very careful with fetching partial results, due to the caching mechanism. If you bump into this issue, it suggests that your code somewhere else does that, so perhaps you need to review that, as disabling the cache will have a performance hit.

Howdy, Stranger!

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

In this Discussion