cakephp-lazyload icon indicating copy to clipboard operation
cakephp-lazyload copied to clipboard

Issues when association is null

Open nachitox opened this issue 4 years ago • 3 comments

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 ?

nachitox avatar Jan 11 '20 14:01 nachitox

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.

jeremyharris avatar Jul 12 '21 00:07 jeremyharris

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

fabiofdsantos avatar Jul 12 '21 17:07 fabiofdsantos

ping @nachitox

Checking in to see if #27 fixed your issue.

jeremyharris avatar Apr 18 '22 16:04 jeremyharris