jeremyharris / cakephp-lazyload

A lazy loader for CakePHP entities.
MIT License
61 stars 10 forks source link

Issues when association is null #23

Open nachitox opened 4 years ago

nachitox commented 4 years ago

Context: I have EntityA which hasOne EntityB that can be NULL.

My problem: I'm trying to cache the association to avoid querying it everytime i call $entityA->entity_b, so on the EntityA's Entity file i have:

    public function _getEntityB()
    {
        if ($this->entityB === 'UNSET')
            $this->entityB = TableRegistry::get('EntityB')->findByKeyId($this->id)->first();

        return $this->entityB;
    }

but because entityB is NULL, _parentGet returns null and tries to lazyload it everytime.

Is there any solution besides changing $entityA->entity_b for something else ?

jeremyharris commented 3 years ago

Sorry for the super late response, I'm not sure how I missed the notification!

If you're using the lazy load plugin you shouldn't need to cache anything that's lazily loaded. It won't try to reload it if the property exists. So, if it's null, it will lazy load, then on following calls it will just return what was loaded on that first request. Let me know if that makes sense.

If you're finding it's repeatedly loading for every call please provide me with a test case as I'd very much like to fix that bug.

fabiofdsantos commented 3 years ago

@nachitox I think https://github.com/jeremyharris/cakephp-lazyload/pull/27 will fix your problem.

jeremyharris commented 2 years ago

ping @nachitox

Checking in to see if #27 fixed your issue.