From 8917fcc30691804038afe6a429dcf5f00f151093 Mon Sep 17 00:00:00 2001 From: BacLuc Date: Sun, 10 Dec 2023 17:20:54 +0100 Subject: [PATCH] ContentNodeRepository: also fetch root and parent of contentNode With the ResponsiveLayout, we now have mostly ContentNodes which are nested by 2 levels: ColumnLayout -> DefaultLayout -> "Content Nodes with content" This may lead to n+1 db queries that we experienced in the EndpointQueryCountTest. --- api/src/Repository/ContentNodeRepository.php | 10 ++++++++++ ...ryCountTest__testNumberOfQueriesDidNotChange__1.yml | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/api/src/Repository/ContentNodeRepository.php b/api/src/Repository/ContentNodeRepository.php index 9af408c73f..f8267c777c 100644 --- a/api/src/Repository/ContentNodeRepository.php +++ b/api/src/Repository/ContentNodeRepository.php @@ -30,4 +30,14 @@ public function __construct(EntityManagerInterface $em, string $entityClass = Co public function filterByUser(QueryBuilder $queryBuilder, User $user): void { $this->filterByContentNode($queryBuilder, $user, $queryBuilder->getRootAliases()[0]); } + + public function createQueryBuilder($alias, $indexBy = null): QueryBuilder { + $qb = parent::createQueryBuilder($alias, $indexBy); + $qb->select("{$alias}", 'fetchRoot', 'fetchParent', 'fetchParentOfParent'); + $qb->leftJoin("{$alias}.root", 'fetchRoot'); + $qb->leftJoin("{$alias}.parent", 'fetchParent'); + $qb->leftJoin('fetchParent.parent', 'fetchParentOfParent'); + + return $qb; + } } diff --git a/api/tests/Api/SnapshotTests/__snapshots__/EndpointQueryCountTest__testNumberOfQueriesDidNotChange__1.yml b/api/tests/Api/SnapshotTests/__snapshots__/EndpointQueryCountTest__testNumberOfQueriesDidNotChange__1.yml index e159d9d559..7df55b048a 100644 --- a/api/tests/Api/SnapshotTests/__snapshots__/EndpointQueryCountTest__testNumberOfQueriesDidNotChange__1.yml +++ b/api/tests/Api/SnapshotTests/__snapshots__/EndpointQueryCountTest__testNumberOfQueriesDidNotChange__1.yml @@ -10,7 +10,7 @@ /camp_collaborations/item: 15 /categories: 11 /categories/item: 9 -/content_nodes: 9 +/content_nodes: 8 /content_node/column_layouts: 6 /content_node/column_layouts/item: 10 /content_node/material_nodes: 6