99use Doctrine \ORM \QueryBuilder ;
1010use Doctrine \ORM \Tools \Pagination \Paginator ;
1111use Doctrine \Persistence \ManagerRegistry ;
12- use RZ \Roadiz \Contracts \NodeType \NodeTypeFieldInterface ;
1312use RZ \Roadiz \Core \AbstractEntities \TranslationInterface ;
1413use RZ \Roadiz \CoreBundle \Doctrine \Event \QueryBuilder \QueryBuilderNodesSourcesApplyEvent ;
1514use 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