@@ -57,6 +57,7 @@ abstract class AbstractEntityRepository implements EntityRepositoryInterface
57
57
58
58
private array $ tableAliases = [];
59
59
private array $ additionalFieldsToSelect = [];
60
+ private array $ joins = [];
60
61
61
62
public function __construct (
62
63
private string $ entityClassName ,
@@ -138,6 +139,8 @@ public function updateSingleField(int $id, string $field, mixed $newValue): bool
138
139
139
140
public function createFindByQueryBuilder (array $ criteria , ?array $ orderBy ): QueryBuilder
140
141
{
142
+ $ this ->resetProperties ();
143
+
141
144
$ normalizedCriteria = $ this ->normalizeCriteria ($ criteria );
142
145
$ this ->tableAliases ['p ' ] = $ this ->getEntityBaseFields ();
143
146
@@ -531,6 +534,10 @@ private function createNestedCriteria(QueryBuilder $queryBuilder, NestedConditio
531
534
private function joinSelfMetaTable (QueryBuilder $ queryBuilder , bool $ incrementIfNecessary = false ): string
532
535
{
533
536
$ applyJoin = function (QueryBuilder $ queryBuilder , string $ alias ): void {
537
+ if (in_array ($ alias , $ this ->joins , true )) {
538
+ return ;
539
+ }
540
+
534
541
if (!array_key_exists ($ alias , $ this ->tableAliases )) {
535
542
$ this ->tableAliases [$ alias ] = [];
536
543
}
@@ -541,6 +548,8 @@ private function joinSelfMetaTable(QueryBuilder $queryBuilder, bool $incrementIf
541
548
$ alias ,
542
549
sprintf ('p.%s = %s.%s ' , static ::TABLE_IDENTIFIER , $ alias , static ::TABLE_META_IDENTIFIER ),
543
550
);
551
+
552
+ $ this ->registerJoin ($ alias );
544
553
};
545
554
546
555
static $ aliasNumber = 1 ;
@@ -569,7 +578,7 @@ private function hasJoin(QueryBuilder $queryBuilder, string $joinAlias): bool
569
578
return count (array_filter ($ matches , static fn (array $ match ) => $ match [2 ] === $ joinAlias )) > 0 ;
570
579
}
571
580
} catch (QueryException ) {
572
- return false ;
581
+ return array_key_exists ( $ joinAlias , $ this -> joins ) ;
573
582
}
574
583
575
584
return false ;
@@ -622,6 +631,8 @@ private function createRelationshipCriteria(
622
631
->setParameter (sprintf ('%s_field ' , $ alias ), $ condition ->getRelationshipFieldName ())
623
632
;
624
633
634
+ $ this ->registerJoin ($ alias );
635
+
625
636
if (!empty ($ condition ->getAlias ())) {
626
637
$ trimmedAlias = trim ($ condition ->getAlias (), '_ ' );
627
638
@@ -685,6 +696,8 @@ private function createTermRelationshipCriteria(
685
696
)
686
697
;
687
698
699
+ $ this ->registerJoin (["tr_ {$ aliasNumber }" , "tt_ {$ aliasNumber }" , $ termTableAlias ]);
700
+
688
701
$ prefixedCriteria = $ this ->getPrefixedCriteriaForTermRelationshipCondition (
689
702
$ condition ->getCriteria (),
690
703
$ termTableAlias ,
@@ -732,6 +745,8 @@ private function createPostRelationshipCriteria(
732
745
)
733
746
;
734
747
748
+ $ this ->registerJoin (["tr_ {$ aliasNumber }" , "p_ {$ aliasNumber }" ]);
749
+
735
750
$ this ->additionalFieldsToSelect [] = 'tt.term_taxonomy_id ' ;
736
751
$ this ->addPostMetaJoinForPostRelationshipCondition ($ queryBuilder , $ condition , $ aliasNumber );
737
752
$ prefixedCriteria = $ this ->getPrefixedCriteriaForPostRelationshipCondition ($ condition , $ aliasNumber );
@@ -769,6 +784,8 @@ private function addPostMetaJoinForPostRelationshipCondition(
769
784
sprintf ('p_%d.id = pm_%d.%s ' , $ aliasNumber , $ aliasNumber , self ::TABLE_META_IDENTIFIER ),
770
785
);
771
786
787
+ $ this ->registerJoin ($ alias );
788
+
772
789
foreach ($ extraFields as $ extraField ) {
773
790
$ this ->tableAliases [$ alias ][] = property_to_field ($ extraField );
774
791
}
@@ -825,6 +842,7 @@ private function selectColumns(QueryBuilder $queryBuilder, array $extraFields, S
825
842
if (in_array ($ column , $ extraFields , true )) {
826
843
$ mappedMetaKey = $ this ->getMappedMetaKey ($ column );
827
844
$ selects [] = select_from_eav ($ column , $ mappedMetaKey );
845
+ $ this ->joinSelfMetaTable ($ queryBuilder );
828
846
} elseif (str_starts_with ($ column , 'MAX( ' )) {
829
847
$ selects [] = $ column ;
830
848
$ this ->joinSelfMetaTable ($ queryBuilder );
@@ -843,4 +861,22 @@ private function selectColumns(QueryBuilder $queryBuilder, array $extraFields, S
843
861
$ queryBuilder ->select (...$ selects , ...$ this ->additionalFieldsToSelect );
844
862
$ this ->additionalFieldsToSelect = [];
845
863
}
864
+
865
+ private function registerJoin (string |array $ alias ): void
866
+ {
867
+ if (is_array ($ alias )) {
868
+ $ this ->joins = array_merge ($ this ->joins , $ alias );
869
+ } else {
870
+ $ this ->joins [] = $ alias ;
871
+ }
872
+
873
+ $ this ->joins = array_unique ($ this ->joins );
874
+ }
875
+
876
+ private function resetProperties (): void
877
+ {
878
+ $ this ->tableAliases = [];
879
+ $ this ->additionalFieldsToSelect = [];
880
+ $ this ->joins = [];
881
+ }
846
882
}
0 commit comments