Skip to content

Commit

Permalink
Introduce configuration option for subselect batch size.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Oct 10, 2023
1 parent 47952c3 commit b9e55ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,4 +1127,14 @@ public function isRejectIdCollisionInIdentityMapEnabled(): bool
{
return $this->_attributes['rejectIdCollisionInIdentityMap'] ?? false;
}

public function setFetchModeSubselectBatchSize(int $batchSize = 100): void
{
$this->_attributes['fetchModeSubselectBatchSize'] = $batchSize;
}

public function getFetchModeSubselectBatchSize(): int
{
return $this->_attributes['fetchModeSubselectBatchSize'] ?? 100;
}
}
33 changes: 18 additions & 15 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use Throwable;
use UnexpectedValueException;

use function array_chunk;
use function array_combine;
use function array_diff_key;
use function array_filter;
Expand Down Expand Up @@ -3173,29 +3174,31 @@ private function subselectLoadCollection(array $collections, array $mapping): vo
$class = $this->em->getClassMetadata($mapping['sourceEntity']);
$mappedBy = $mapping['mappedBy'];

$entities = [];
$index = [];
$batches = array_chunk($collections, $this->em->getConfiguration()->getFetchModeSubselectBatchSize(), true);

foreach ($collections as $idHash => $collection) {
$index[$idHash] = $collection;
$entities[] = $collection->getOwner();
}
foreach ($batches as $collectionBatch) {
$entities = [];

foreach ($collectionBatch as $collection) {
$entities[] = $collection->getOwner();
}

$found = $this->em->getRepository($targetEntity)->findBy([$mappedBy => $entities]);
$found = $this->em->getRepository($targetEntity)->findBy([$mappedBy => $entities]);

$targetClass = $this->em->getClassMetadata($targetEntity);
$targetProperty = $targetClass->getReflectionProperty($mappedBy);
$targetClass = $this->em->getClassMetadata($targetEntity);
$targetProperty = $targetClass->getReflectionProperty($mappedBy);

foreach ($found as $targetValue) {
$sourceEntity = $targetProperty->getValue($targetValue);
foreach ($found as $targetValue) {
$sourceEntity = $targetProperty->getValue($targetValue);

$id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($sourceEntity));
$idHash = implode(' ', $id);
$id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($sourceEntity));
$idHash = implode(' ', $id);

$index[$idHash]->add($targetValue);
$collectionBatch[$idHash]->add($targetValue);
}
}

foreach ($index as $association) {
foreach ($collections as $association) {
$association->setInitialized(true);
$association->takeSnapshot();
}
Expand Down

0 comments on commit b9e55ba

Please sign in to comment.