xfrocks
Well-known member
- Affected version
- 2.0.0
I stumbled upon this piece of code in \XF\Mvc\Entity\Manager:
	
	
	
		
Not quite sure about the business logic here but this is causing additional database queries for relations. Sample code 1:
	
	
	
		
One would expect $singleTest2 to include the Forum data but it doesn't. Checking the log shows that $singleTest2 did not trigger a new query so I guess that's fair. We are just doing some caching here and $singleTest2.Forum will be fetched on demand anyway.
Sample code 2:
	
	
	
		
Again, items in $multipleTest2 don't have the Forum data but the system did execute 2 queries for these two lines of code. I have also checked and can confirm that the _uniqueEntityId of items in $multipleTest2 match those in $multipleTest1. Apparently getEntityCacheLookupString is buggy?
				
			
		PHP:
	
			/** @var Entity $entity */
		$entity = new $className($this, $structure, $values, $relations);
		if ($values)
		{
			...
			$primary = $this->getEntityCacheLookupString($keys);
			if (isset($this->entities[$class][$primary]))
			{
				$entity = $this->entities[$class][$primary];
				// TODO: how to handle relationships, at least if the existing entity has pending changes?
			}
			else
			{
				$this->entities[$class][$primary] = $entity;
			}
		}Not quite sure about the business logic here but this is causing additional database queries for relations. Sample code 1:
		PHP:
	
	$singleTest1 = $app->em()->find('XF:Thread', 1);
$singleTest2 = $app->em()->find('XF:Thread', 1, ['Forum']);One would expect $singleTest2 to include the Forum data but it doesn't. Checking the log shows that $singleTest2 did not trigger a new query so I guess that's fair. We are just doing some caching here and $singleTest2.Forum will be fetched on demand anyway.
Sample code 2:
		PHP:
	
	$multipleTest1 = $app->em()->findByIds('XF:Thread', [1, 2]);
$multipleTest2 = $app->em()->findByIds('XF:Thread', [1, 2], ['Forum']);Again, items in $multipleTest2 don't have the Forum data but the system did execute 2 queries for these two lines of code. I have also checked and can confirm that the _uniqueEntityId of items in $multipleTest2 match those in $multipleTest1. Apparently getEntityCacheLookupString is buggy?
 
 
		 
 
		 
 
		 
 
		 
 
		