Skip to content

Commit 5ce1eb7

Browse files
author
roadiz-ci
committed
feat: Optimize dynamic nodes-sources path resolver (#35)
1 parent bf9bd66 commit 5ce1eb7

File tree

3 files changed

+135
-267
lines changed

3 files changed

+135
-267
lines changed

src/Repository/NodeRepository.php

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\Common\Collections\Criteria;
88
use Doctrine\ORM\NonUniqueResultException;
99
use Doctrine\ORM\NoResultException;
10-
use Doctrine\ORM\Query;
1110
use Doctrine\ORM\Query\Expr;
1211
use Doctrine\ORM\QueryBuilder;
1312
use Doctrine\ORM\Tools\Pagination\Paginator;
@@ -617,131 +616,6 @@ public function findByNodeNameWithTranslation(
617616
return $qb->getQuery()->getOneOrNullResult();
618617
}
619618

620-
/**
621-
* Find one node using its nodeName and a translation, or a unique URL alias.
622-
*
623-
* @return array|null Array with node-type "name" and node-source "id"
624-
*
625-
* @throws NonUniqueResultException
626-
*/
627-
public function findNodeTypeNameAndSourceIdByIdentifier(
628-
string $identifier,
629-
?TranslationInterface $translation,
630-
bool $availableTranslation = false,
631-
bool $allowNonReachableNodes = true,
632-
): ?array {
633-
$qb = $this->createQueryBuilder(self::NODE_ALIAS);
634-
$qb->select('nt.name, ns.id')
635-
->innerJoin('n.nodeSources', self::NODESSOURCES_ALIAS)
636-
->innerJoin('n.nodeType', self::NODETYPE_ALIAS)
637-
->innerJoin('ns.translation', self::TRANSLATION_ALIAS)
638-
->leftJoin('ns.urlAliases', 'uas')
639-
->andWhere($qb->expr()->orX(
640-
$qb->expr()->eq('uas.alias', ':identifier'),
641-
$qb->expr()->andX(
642-
$qb->expr()->eq('n.nodeName', ':identifier'),
643-
$qb->expr()->eq('t.id', ':translation')
644-
)
645-
))
646-
->setParameter('identifier', $identifier)
647-
->setParameter('translation', $translation)
648-
->setMaxResults(1)
649-
->setCacheable(true);
650-
651-
if (!$allowNonReachableNodes) {
652-
$qb->andWhere($qb->expr()->eq('nt.reachable', ':reachable'))
653-
->setParameter('reachable', true);
654-
}
655-
656-
if ($availableTranslation) {
657-
$qb->andWhere($qb->expr()->eq('t.available', ':available'))
658-
->setParameter('available', true);
659-
}
660-
661-
$this->alterQueryBuilderWithAuthorizationChecker($qb);
662-
$query = $qb->getQuery();
663-
$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
664-
$query->setHydrationMode(Query::HYDRATE_ARRAY);
665-
666-
return $query->getOneOrNullResult();
667-
}
668-
669-
/**
670-
* Find one Node with its nodeName and the default translation.
671-
*
672-
* @throws NonUniqueResultException
673-
*
674-
* @deprecated Use findNodeTypeNameAndSourceIdByIdentifier
675-
*/
676-
public function findByNodeNameWithDefaultTranslation(
677-
string $nodeName,
678-
): ?Node {
679-
$qb = $this->createQueryBuilder(self::NODE_ALIAS);
680-
$qb->select('n, ns')
681-
->innerJoin('n.nodeSources', self::NODESSOURCES_ALIAS)
682-
->innerJoin('ns.translation', self::TRANSLATION_ALIAS)
683-
->andWhere($qb->expr()->eq('n.nodeName', ':nodeName'))
684-
->andWhere($qb->expr()->eq('t.defaultTranslation', ':defaultTranslation'))
685-
->setMaxResults(1)
686-
->setParameter('nodeName', $nodeName)
687-
->setParameter('defaultTranslation', true)
688-
->setCacheable(true);
689-
690-
$this->alterQueryBuilderWithAuthorizationChecker($qb);
691-
692-
return $qb->getQuery()->getOneOrNullResult();
693-
}
694-
695-
/**
696-
* Find the Home node with a given translation.
697-
*
698-
* @throws NonUniqueResultException
699-
*/
700-
public function findHomeWithTranslation(
701-
?TranslationInterface $translation = null,
702-
): ?Node {
703-
if (null === $translation) {
704-
return $this->findHomeWithDefaultTranslation();
705-
}
706-
707-
$qb = $this->createQueryBuilder(self::NODE_ALIAS);
708-
$qb->select('n, ns')
709-
->innerJoin('n.nodeSources', self::NODESSOURCES_ALIAS)
710-
->andWhere($qb->expr()->eq('n.home', ':home'))
711-
->andWhere($qb->expr()->eq('ns.translation', ':translation'))
712-
->setMaxResults(1)
713-
->setParameter('home', true)
714-
->setParameter('translation', $translation)
715-
->setCacheable(true);
716-
717-
$this->alterQueryBuilderWithAuthorizationChecker($qb);
718-
719-
return $qb->getQuery()->getOneOrNullResult();
720-
}
721-
722-
/**
723-
* Find the Home node with the default translation.
724-
*
725-
* @throws NonUniqueResultException
726-
*/
727-
public function findHomeWithDefaultTranslation(): ?Node
728-
{
729-
$qb = $this->createQueryBuilder(self::NODE_ALIAS);
730-
$qb->select('n, ns')
731-
->innerJoin('n.nodeSources', self::NODESSOURCES_ALIAS)
732-
->innerJoin('ns.translation', self::TRANSLATION_ALIAS)
733-
->andWhere($qb->expr()->eq('n.home', ':home'))
734-
->andWhere($qb->expr()->eq('t.defaultTranslation', ':defaultTranslation'))
735-
->setMaxResults(1)
736-
->setParameter('home', true)
737-
->setParameter('defaultTranslation', true)
738-
->setCacheable(true);
739-
740-
$this->alterQueryBuilderWithAuthorizationChecker($qb);
741-
742-
return $qb->getQuery()->getOneOrNullResult();
743-
}
744-
745619
public function findByParentWithTranslation(
746620
TranslationInterface $translation,
747621
?Node $parent = null,

src/Repository/NodesSourcesRepository.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Doctrine\ORM\QueryBuilder;
1010
use Doctrine\ORM\Tools\Pagination\Paginator;
1111
use Doctrine\Persistence\ManagerRegistry;
12-
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
1312
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
1413
use RZ\Roadiz\CoreBundle\Doctrine\Event\QueryBuilder\QueryBuilderNodesSourcesApplyEvent;
1514
use RZ\Roadiz\CoreBundle\Doctrine\Event\QueryBuilder\QueryBuilderNodesSourcesBuildEvent;
@@ -179,6 +178,40 @@ protected function applyFilterByCriteria(array &$criteria, QueryBuilder $qb): vo
179178
}
180179
}
181180

181+
public function findOneByIdentifierAndTranslation(
182+
string $identifier,
183+
?TranslationInterface $translation,
184+
bool $availableTranslation = false,
185+
): ?NodesSources {
186+
$qb = $this->createQueryBuilder(self::NODESSOURCES_ALIAS);
187+
$qb->select([self::NODESSOURCES_ALIAS, static::NODE_ALIAS, 'ua'])
188+
->innerJoin(self::NODESSOURCES_ALIAS.'.node', self::NODE_ALIAS)
189+
->innerJoin(self::NODESSOURCES_ALIAS.'.translation', self::TRANSLATION_ALIAS)
190+
->leftJoin(self::NODESSOURCES_ALIAS.'.urlAliases', 'ua')
191+
->andWhere($qb->expr()->orX(
192+
$qb->expr()->eq('ua.alias', ':identifier'),
193+
$qb->expr()->andX(
194+
$qb->expr()->eq(self::NODE_ALIAS.'.nodeName', ':identifier'),
195+
$qb->expr()->eq(self::TRANSLATION_ALIAS.'.id', ':translation')
196+
)
197+
))
198+
->setParameter('identifier', $identifier)
199+
->setParameter('translation', $translation)
200+
->setMaxResults(1)
201+
->setCacheable(true);
202+
203+
if ($availableTranslation) {
204+
$qb->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS.'.available', ':available'))
205+
->setParameter('available', true);
206+
}
207+
208+
$this->alterQueryBuilderWithAuthorizationChecker($qb);
209+
$query = $qb->getQuery();
210+
$query->setCacheable(true);
211+
212+
return $query->getOneOrNullResult();
213+
}
214+
182215
public function alterQueryBuilderWithAuthorizationChecker(
183216
QueryBuilder $qb,
184217
string $prefix = EntityRepository::NODESSOURCES_ALIAS,
@@ -476,8 +509,7 @@ public function findByTextQuery(
476509
array $additionalCriteria = [],
477510
) {
478511
$qb = $this->createQueryBuilder(static::NODESSOURCES_ALIAS);
479-
$qb->addSelect(static::NODE_ALIAS)
480-
->addSelect('ua')
512+
$qb->select([static::NODESSOURCES_ALIAS, static::NODE_ALIAS, 'ua'])
481513
->leftJoin(static::NODESSOURCES_ALIAS.'.urlAliases', 'ua')
482514
->andWhere($qb->expr()->orX(
483515
$qb->expr()->like(static::NODESSOURCES_ALIAS.'.title', ':query'),
@@ -651,39 +683,12 @@ public function searchBy(
651683
return parent::searchBy($pattern, $criteria, $orders, $limit, $offset, static::NODESSOURCES_ALIAS);
652684
}
653685

654-
/**
655-
* @deprecated Use findByNodesSourcesAndFieldNameAndTranslation instead
656-
*/
657-
public function findByNodesSourcesAndFieldAndTranslation(
658-
NodesSources $nodesSources,
659-
NodeTypeFieldInterface $field,
660-
): ?array {
661-
$qb = $this->createQueryBuilder(static::NODESSOURCES_ALIAS);
662-
$qb->select('ns, n, ua')
663-
->innerJoin('ns.node', static::NODE_ALIAS)
664-
->leftJoin('ns.urlAliases', 'ua')
665-
->innerJoin('n.aNodes', 'ntn')
666-
->andWhere($qb->expr()->eq('ntn.fieldName', ':fieldName'))
667-
->andWhere($qb->expr()->eq('ntn.nodeA', ':nodeA'))
668-
->andWhere($qb->expr()->eq('ns.translation', ':translation'))
669-
->addOrderBy('ntn.position', 'ASC')
670-
->setCacheable(true);
671-
672-
$this->alterQueryBuilderWithAuthorizationChecker($qb);
673-
674-
$qb->setParameter('fieldName', $field->getName())
675-
->setParameter('nodeA', $nodesSources->getNode())
676-
->setParameter('translation', $nodesSources->getTranslation());
677-
678-
return $qb->getQuery()->getResult();
679-
}
680-
681686
public function findByNodesSourcesAndFieldNameAndTranslation(
682687
NodesSources $nodesSources,
683688
string $fieldName,
684689
): ?array {
685690
$qb = $this->createQueryBuilder(static::NODESSOURCES_ALIAS);
686-
$qb->select('ns, n, ua')
691+
$qb->select([static::NODESSOURCES_ALIAS, static::NODE_ALIAS, 'ua'])
687692
->innerJoin('ns.node', static::NODE_ALIAS)
688693
->leftJoin('ns.urlAliases', 'ua')
689694
->innerJoin('n.aNodes', 'ntn')
@@ -705,7 +710,7 @@ public function findByNodesSourcesAndFieldNameAndTranslation(
705710
public function findByNode(Node $node): array
706711
{
707712
$qb = $this->createQueryBuilder(static::NODESSOURCES_ALIAS);
708-
$qb->select('ns, n, ua')
713+
$qb->select([static::NODESSOURCES_ALIAS, static::NODE_ALIAS, 'ua'])
709714
->innerJoin('ns.node', static::NODE_ALIAS)
710715
->innerJoin('ns.translation', static::TRANSLATION_ALIAS)
711716
->leftJoin('ns.urlAliases', 'ua')

0 commit comments

Comments
 (0)