Skip to content

Commit 4010c0c

Browse files
phansysfranmomu
authored andcommitted
Use iterators instead of arrays for query results where possible
1 parent 25c63bb commit 4010c0c

File tree

10 files changed

+50
-48
lines changed

10 files changed

+50
-48
lines changed

src/Loggable/Entity/Repository/LogEntryRepository.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ class LogEntryRepository extends EntityRepository
5353
*/
5454
public function getLogEntries($entity)
5555
{
56-
$q = $this->getLogEntriesQuery($entity);
57-
58-
return $q->getResult();
56+
return $this->getLogEntriesQuery($entity)->getResult();
5957
}
6058

6159
/**
@@ -112,7 +110,7 @@ public function revert($entity, $version = 1)
112110
$dql .= ' WHERE log.objectId = :objectId';
113111
$dql .= ' AND log.objectClass = :objectClass';
114112
$dql .= ' AND log.version <= :version';
115-
$dql .= ' ORDER BY log.version ASC';
113+
$dql .= ' ORDER BY log.version DESC';
116114

117115
$objectId = (string) $wrapped->getIdentifier(false, true);
118116
$q = $this->_em->createQuery($dql);
@@ -121,16 +119,18 @@ public function revert($entity, $version = 1)
121119
'objectClass' => $objectClass,
122120
'version' => $version,
123121
]);
124-
$logs = $q->getResult();
125-
126-
if ([] === $logs) {
127-
throw new UnexpectedValueException('Could not find any log entries under version: '.$version);
128-
}
129122

130123
$config = $this->getLoggableListener()->getConfiguration($this->_em, $objectMeta->getName());
131124
$fields = $config['versioned'];
132125
$filled = false;
133-
while (($log = array_pop($logs)) && !$filled) {
126+
$logsFound = false;
127+
128+
$logs = $q->toIterable();
129+
assert($logs instanceof \Generator);
130+
131+
while ((null !== $log = $logs->current()) && !$filled) {
132+
$logsFound = true;
133+
$logs->next();
134134
if ($data = $log->getData()) {
135135
foreach ($data as $field => $value) {
136136
if (in_array($field, $fields, true)) {
@@ -142,6 +142,11 @@ public function revert($entity, $version = 1)
142142
}
143143
$filled = [] === $fields;
144144
}
145+
146+
if (!$logsFound) {
147+
throw new UnexpectedValueException('Could not find any log entries under version: '.$version);
148+
}
149+
145150
/*if (count($fields)) {
146151
throw new \Gedmo\Exception\UnexpectedValueException('Could not fully revert the entity to version: '.$version);
147152
}*/

src/Sortable/Mapping/Event/Adapter/ORM.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function getMaxPosition(array $config, $meta, $groups)
4343
$query = $qb->getQuery();
4444
$query->useQueryCache(false);
4545
$query->disableResultCache();
46-
$res = $query->getResult();
46+
$query->setMaxResults(1);
4747

48-
return $res[0][1];
48+
return $query->getSingleScalarResult();
4949
}
5050

5151
/**

src/Translatable/Document/Repository/TranslationRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ public function findObjectByTranslatedField($field, $value, $class)
183183
->getQuery();
184184

185185
$q->setHydrate(false);
186-
$result = $q->getIterator()->toArray();
186+
$result = $q->getSingleResult();
187187

188-
$id = $result[0]['foreign_key'] ?? null;
188+
$id = $result['foreign_key'] ?? null;
189189

190190
if (null === $id) {
191191
return null;

src/Translatable/Entity/Repository/TranslationRepository.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,8 @@ public function findTranslations($entity)
151151
'entityId' => $entityId,
152152
'entityClass' => $entityClass,
153153
]);
154-
$q = $qb->getQuery();
155154

156-
foreach ($q->getArrayResult() as $row) {
155+
foreach ($qb->getQuery()->toIterable([], Query::HYDRATE_ARRAY) as $row) {
157156
$result[$row['locale']][$row['field']] = $row['content'];
158157
}
159158
}
@@ -191,8 +190,7 @@ public function findObjectByTranslatedField($field, $value, $class)
191190
'value' => $value,
192191
]);
193192
$q->setMaxResults(1);
194-
$result = $q->getArrayResult();
195-
$id = $result[0]['foreignKey'] ?? null;
193+
$id = $q->getSingleScalarResult();
196194

197195
if (null !== $id) {
198196
$entity = $this->_em->find($class, $id);
@@ -223,7 +221,7 @@ public function findTranslationsByObjectId($id)
223221
->setParameter('entityId', $id);
224222
$q = $qb->getQuery();
225223

226-
foreach ($q->getArrayResult() as $row) {
224+
foreach ($q->toIterable([], Query::HYDRATE_ARRAY) as $row) {
227225
$result[$row['locale']][$row['field']] = $row['content'];
228226
}
229227
}

src/Translatable/Mapping/Event/Adapter/ORM.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,8 @@ public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $tran
162162
}
163163
$q = $qb->getQuery();
164164
$q->setMaxResults(1);
165-
$result = $q->getResult();
166165

167-
if ($result) {
168-
return array_shift($result);
169-
}
170-
171-
return null;
166+
return $q->getOneOrNullResult();
172167
}
173168

174169
public function removeAssociatedTranslations(AbstractWrapper $wrapped, $transClass, $objectClass)

src/Tree/Entity/Repository/ClosureTreeRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function removeFromTree($node)
272272
$dql .= " WHERE node.{$config['parent']} = :node";
273273
$q = $this->_em->createQuery($dql);
274274
$q->setParameter('node', $node);
275-
$nodesToReparent = $q->getResult();
275+
$nodesToReparent = $q->toIterable();
276276
// process updates in transaction
277277
$this->_em->getConnection()->beginTransaction();
278278

src/Tree/Entity/Repository/MaterializedPathRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function getNodesHierarchy($node = null, $direct = false, array $options
250250
$nodes = $this->getNodesHierarchyQuery($node, $direct, $options, $includeNode)->getArrayResult();
251251
usort(
252252
$nodes,
253-
static function ($a, $b) use ($path) {
253+
static function (array $a, array $b) use ($path): int {
254254
return strcmp($a[$path], $b[$path]);
255255
}
256256
);

src/Tree/Entity/Repository/NestedTreeRepository.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ public function childrenQuery($node = null, $direct = false, $sortByField = null
409409
*/
410410
public function children($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
411411
{
412-
$q = $this->childrenQuery($node, $direct, $sortByField, $direction, $includeNode);
413-
414-
return $q->getResult();
412+
return $this->childrenQuery($node, $direct, $sortByField, $direction, $includeNode)->getResult();
415413
}
416414

417415
public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false)
@@ -802,7 +800,7 @@ public function removeFromTree($node)
802800

803801
$qb->andWhere($qb->expr()->eq('node.'.$config['parent'], ':pid'));
804802
$qb->setParameter('pid', $nodeId);
805-
$nodes = $qb->getQuery()->getArrayResult();
803+
$nodes = $qb->getQuery()->toIterable([], Query::HYDRATE_ARRAY);
806804

807805
// go through each of the node's children
808806
foreach ($nodes as $newRoot) {
@@ -1235,13 +1233,17 @@ private function verifyTree(array &$errors, ?object $root = null): void
12351233
$qb->andWhere($qb->expr()->eq('node.'.$config['root'], ':rid'));
12361234
$qb->setParameter('rid', $rootId);
12371235
}
1238-
$nodes = $qb->getQuery()->getArrayResult();
1239-
if ([] !== $nodes) {
1240-
foreach ($nodes as $node) {
1241-
$errors[] = "node [{$node[$identifier]}] has missing parent".($root ? ' on tree root: '.$rootId : '');
1242-
}
12431236

1244-
return; // loading broken relation can cause infinite loop
1237+
$areMissingParents = false;
1238+
1239+
foreach ($qb->getQuery()->toIterable([], Query::HYDRATE_ARRAY) as $node) {
1240+
$areMissingParents = true;
1241+
$errors[] = "node [{$node[$identifier]}] has missing parent".($root ? ' on tree root: '.$rootId : '');
1242+
}
1243+
1244+
// loading broken relation can cause infinite loop
1245+
if ($areMissingParents) {
1246+
return;
12451247
}
12461248

12471249
// check for nodes that have a right value lower than the left
@@ -1272,9 +1274,8 @@ private function verifyTree(array &$errors, ?object $root = null): void
12721274
$qb->andWhere($qb->expr()->eq('node.'.$config['root'], ':rid'));
12731275
$qb->setParameter('rid', $rootId);
12741276
}
1275-
$nodes = $qb->getQuery()->getResult(Query::HYDRATE_OBJECT);
12761277

1277-
foreach ($nodes as $node) {
1278+
foreach ($qb->getQuery()->toIterable() as $node) {
12781279
$right = $meta->getReflectionProperty($config['right'])->getValue($node);
12791280
$left = $meta->getReflectionProperty($config['left'])->getValue($node);
12801281
$id = $meta->getReflectionProperty($identifier)->getValue($node);

src/Tree/Strategy/ORM/Closure.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Doctrine\ORM\EntityManagerInterface;
1414
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
1515
use Doctrine\ORM\Mapping\ClassMetadataInfo;
16+
use Doctrine\ORM\Query;
1617
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
1718
use Doctrine\Persistence\Mapping\ClassMetadata;
1819
use Doctrine\Persistence\ObjectManager;
@@ -302,23 +303,26 @@ public function processPostPersist($em, $entity, AdapterInterface $ea)
302303
$dql .= ' WHERE c.descendant = :parent';
303304
$q = $em->createQuery($dql);
304305
$q->setParameter('parent', $parent);
305-
$ancestors = $q->getArrayResult();
306306

307-
if ([] === $ancestors) {
308-
// The parent has been persisted after the child, postpone the evaluation
309-
$this->pendingChildNodeInserts[$emHash][] = $node;
307+
$mustPostpone = true;
310308

311-
continue;
312-
}
309+
foreach ($q->toIterable([], Query::HYDRATE_ARRAY) as $ancestor) {
310+
$mustPostpone = false;
313311

314-
foreach ($ancestors as $ancestor) {
315312
$entries[] = [
316313
$ancestorColumnName => $ancestor['ancestor'][$identifier],
317314
$descendantColumnName => $nodeId,
318315
$depthColumnName => $ancestor['depth'] + 1,
319316
];
320317
}
321318

319+
if ($mustPostpone) {
320+
// The parent has been persisted after the child, postpone the evaluation
321+
$this->pendingChildNodeInserts[$emHash][] = $node;
322+
323+
continue;
324+
}
325+
322326
if (isset($config['level'])) {
323327
$this->pendingNodesLevelProcess[$nodeId] = $node;
324328
}

src/Tree/Strategy/ORM/Nested.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ public function processScheduledDelete($em, $node)
223223
}
224224
$q = $qb->getQuery();
225225
// get nodes for deletion
226-
$nodes = $q->getResult();
227-
foreach ((array) $nodes as $removalNode) {
226+
foreach ($q->toIterable() as $removalNode) {
228227
$uow->scheduleForDelete($removalNode);
229228
}
230229
}

0 commit comments

Comments
 (0)