Skip to content

Commit 0e2edca

Browse files
committed
Address review feedback: simplify setHint calls, format loadProxies
1 parent d59ab62 commit 0e2edca

File tree

4 files changed

+16
-47
lines changed

4 files changed

+16
-47
lines changed

src/EntityPreloader.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,21 @@ public function preload(
6565
}
6666

6767
$maxFetchJoinSameFieldCount ??= 1;
68-
$sourceEntities = $this->loadProxies($sourceClassMetadata, $sourceEntities, $batchSize ?? self::PRELOAD_ENTITY_DEFAULT_BATCH_SIZE, $maxFetchJoinSameFieldCount, $readOnly);
68+
$sourceEntities = $this->loadProxies(
69+
$sourceClassMetadata,
70+
$sourceEntities,
71+
$batchSize ?? self::PRELOAD_ENTITY_DEFAULT_BATCH_SIZE,
72+
$maxFetchJoinSameFieldCount,
73+
$readOnly,
74+
);
6975

7076
$preloader = match ($associationMapping['type']) {
7177
ClassMetadata::ONE_TO_ONE, ClassMetadata::MANY_TO_ONE => $this->preloadToOne(...),
7278
ClassMetadata::ONE_TO_MANY, ClassMetadata::MANY_TO_MANY => $this->preloadToMany(...),
7379
default => throw new LogicException("Unsupported association mapping type {$associationMapping['type']}"),
7480
};
7581

76-
$result = $preloader($sourceEntities, $sourceClassMetadata, $sourcePropertyName, $targetClassMetadata, $batchSize, $maxFetchJoinSameFieldCount, $readOnly);
77-
78-
if ($readOnly) {
79-
$unitOfWork = $this->entityManager->getUnitOfWork();
80-
81-
foreach ($result as $entity) {
82-
if (!$unitOfWork->isReadOnly($entity)) {
83-
$unitOfWork->markReadOnly($entity);
84-
}
85-
}
86-
}
87-
88-
return $result;
82+
return $preloader($sourceEntities, $sourceClassMetadata, $sourcePropertyName, $targetClassMetadata, $batchSize, $maxFetchJoinSameFieldCount, $readOnly);
8983
}
9084

9185
/**
@@ -328,7 +322,7 @@ private function preloadManyToManyInner(
328322

329323
$sourceIdentifierType = $this->getIdentifierFieldType($sourceClassMetadata);
330324

331-
$manyToManyQuery = $this->entityManager->createQueryBuilder()
325+
$manyToManyRows = $this->entityManager->createQueryBuilder()
332326
->select("source.{$sourceIdentifierName} AS sourceId", "target.{$targetIdentifierName} AS targetId")
333327
->from($sourceClassMetadata->getName(), 'source')
334328
->join("source.{$sourcePropertyName}", 'target')
@@ -338,13 +332,9 @@ private function preloadManyToManyInner(
338332
$this->convertFieldValuesToDatabaseValues($sourceIdentifierType, $uninitializedSourceEntityIdsChunk),
339333
$this->deduceArrayParameterType($sourceIdentifierType),
340334
)
341-
->getQuery();
342-
343-
if ($readOnly) {
344-
$manyToManyQuery->setHint(Query::HINT_READ_ONLY, true);
345-
}
346-
347-
$manyToManyRows = $manyToManyQuery->getResult();
335+
->getQuery()
336+
->setHint(Query::HINT_READ_ONLY, $readOnly)
337+
->getResult();
348338

349339
$targetEntities = [];
350340
$uninitializedTargetEntityIds = [];
@@ -459,13 +449,10 @@ private function loadEntitiesBy(
459449
$queryBuilder->addOrderBy("{$rootLevelAlias}.{$field}", $direction);
460450
}
461451

462-
$query = $queryBuilder->getQuery();
463-
464-
if ($readOnly) {
465-
$query->setHint(Query::HINT_READ_ONLY, true);
466-
}
467-
468-
return $query->getResult();
452+
return $queryBuilder
453+
->getQuery()
454+
->setHint(Query::HINT_READ_ONLY, $readOnly)
455+
->getResult();
469456
}
470457

471458
private function deduceArrayParameterType(Type $dbalType): ArrayParameterType|int|null // @phpstan-ignore return.unusedType (old dbal compat)

tests/EntityPreloadBlogManyHasManyTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ public function testManyHasManyWithPreloadReadOnly(DbalType $primaryKey): void
125125

126126
self::assertCount(25, $tags);
127127

128-
// Verify that preloaded tags are marked as read-only
129-
$unitOfWork = $this->getEntityManager()->getUnitOfWork();
130-
foreach ($tags as $tag) {
131-
self::assertTrue($unitOfWork->isReadOnly($tag), 'Tag should be marked as read-only');
132-
}
133-
134128
self::assertAggregatedQueries([
135129
['count' => 1, 'query' => 'SELECT * FROM article t0'],
136130
['count' => 1, 'query' => 'SELECT * FROM article a0_ INNER JOIN article_tag a2_ ON a0_.id = a2_.article_id INNER JOIN tag t1_ ON t1_.id = a2_.tag_id WHERE a0_.id IN (?, ?, ?, ?, ?)'],

tests/EntityPreloadBlogManyHasOneTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@ public function testManyHasOneWithPreloadReadOnly(DbalType $primaryKey): void
128128

129129
self::assertCount(5, $categories);
130130

131-
// Verify that preloaded categories are marked as read-only
132-
$unitOfWork = $this->getEntityManager()->getUnitOfWork();
133-
foreach ($categories as $category) {
134-
self::assertTrue($unitOfWork->isReadOnly($category), 'Category should be marked as read-only');
135-
}
136-
137131
self::assertAggregatedQueries([
138132
['count' => 1, 'query' => 'SELECT * FROM article t0'],
139133
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.id IN (?, ?, ?, ?, ?)'],

tests/EntityPreloadBlogOneHasManyTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,6 @@ public function testOneHasManyWithPreloadReadOnly(DbalType $primaryKey): void
149149

150150
self::assertCount(25, $articles);
151151

152-
// Verify that preloaded articles are marked as read-only
153-
$unitOfWork = $this->getEntityManager()->getUnitOfWork();
154-
foreach ($articles as $article) {
155-
self::assertTrue($unitOfWork->isReadOnly($article), 'Article should be marked as read-only');
156-
}
157-
158152
self::assertAggregatedQueries([
159153
['count' => 1, 'query' => 'SELECT * FROM category t0'],
160154
['count' => 1, 'query' => 'SELECT * FROM article a0_ WHERE a0_.category_id IN (?, ?, ?, ?, ?)'],

0 commit comments

Comments
 (0)