fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
152 stars 96 forks source link

Eager-loaded relations are not cached when null #451

Open romosan opened 1 year ago

romosan commented 1 year ago

I hope this is a bug and not a feature, because the latest version of the ORM doesn't cache the result of eager-loading has_many relations (and probably many_many as well) when there are no related records found. This happens at least since the commit 776c245027deacce0b7f2b5dd47c1bf8d49297f4 (speedup of the hydration process) and it causes the relation to be lazy-loaded redundantly when it is accessed for the second time. In 1.8.2, an empty array was set from the first load and it prevented the second one from happening.

In the current \Orm\Query::process_row function, this code seems to me to be the problem:

            // skip the rest if we don't have a pk (= no result)
            if (is_null($pk))
            {
                continue;
            }

Perhaps before this continue, an empty array should be added to $pointers if ($model['singular'] === false).

Edit: A null value doesn't seem to be set in the case of singular relations which haven't been found either.