Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues when association is null #23

Open
nachitox opened this issue Jan 11, 2020 · 3 comments
Open

Issues when association is null #23

nachitox opened this issue Jan 11, 2020 · 3 comments

Comments

@nachitox
Copy link

nachitox commented Jan 11, 2020

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
Copy link
Owner

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
Copy link
Contributor

@nachitox I think #27 will fix your problem.

@jeremyharris
Copy link
Owner

ping @nachitox

Checking in to see if #27 fixed your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants