Skip to content

Commit b2eb07e

Browse files
committed
Fix locationCount: use repository count query instead of lazy collection
getComponentPositions() returns a Doctrine PersistentCollection that can be stale after a within-scenario clear(). A direct COUNT query via the repository is always accurate regardless of identity map state.
1 parent 9098652 commit b2eb07e

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

features/bootstrap/DoctrineContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,5 +1585,6 @@ public function thereIsAPublishedDummyPublishableComponentInPositions(int $count
15851585
}
15861586

15871587
$this->manager->flush();
1588+
$this->manager->clear();
15881589
}
15891590
}

src/Serializer/Normalizer/PublishableNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Doctrine\ORM\Mapping\ClassMetadata;
1919
use Doctrine\Persistence\ManagerRegistry;
2020
use Doctrine\Persistence\ObjectManager;
21+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
2122
use Silverback\ApiComponentsBundle\Annotation\Publishable;
2223
use Silverback\ApiComponentsBundle\Event\ResourceChangedEvent;
2324
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
@@ -76,7 +77,9 @@ public function normalize($object, $format = null, array $context = []): float|a
7677
}
7778

7879
$isPublished = $this->publishableStatusChecker->isActivePublishedAt($object);
79-
$locationCount = method_exists($object, 'getComponentPositions') ? count($object->getComponentPositions()) : null;
80+
$locationCount = method_exists($object, 'getComponentPositions')
81+
? $this->registry->getManagerForClass(ComponentPosition::class)?->getRepository(ComponentPosition::class)->count(['component' => $object])
82+
: null;
8083

8184
$resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object);
8285
$resourceMetadata->setPublishable($isPublished, null, $locationCount);

0 commit comments

Comments
 (0)