@@ -403,6 +403,7 @@ private function initColumnArrays()
403403 $ this ->addSearchColumn ($ column , null , $ searchDql );
404404 } elseif (true === $ this ->accessor ->getValue ($ column , 'selectColumn ' )) {
405405 $ parts = explode ('. ' , $ dql );
406+ $ withExpr = $ this ->accessor ->getValue ($ column , 'joinConditions ' );
406407
407408 while (\count ($ parts ) > 1 ) {
408409 $ previousPart = $ currentPart ;
@@ -412,8 +413,14 @@ private function initColumnArrays()
412413 $ currentAlias = ($ previousPart === $ this ->entityShortName ? '' : $ previousPart .'_ ' ).$ currentPart ;
413414 $ currentAlias = $ this ->getSafeName ($ currentAlias );
414415
415- if (! \array_key_exists ($ previousAlias .'. ' .$ currentPart , $ this ->joins )) {
416- $ this ->addJoin ($ previousAlias .'. ' .$ currentPart , $ currentAlias , $ this ->accessor ->getValue ($ column , 'joinType ' ));
416+ $ columnTableName = $ previousAlias .'. ' .$ currentPart ;
417+ if (!array_key_exists ($ columnTableName , $ this ->joins )) {
418+ $ this ->addJoin ($ columnTableName , $ currentAlias , $ this ->accessor ->getValue ($ column , 'joinType ' ));
419+ }
420+ // for the last dql part join using WITH, if expression is given for column
421+ if (count ($ parts ) === 1 && array_key_exists ($ columnTableName , $ this ->joins ) && null !== $ withExpr ) {
422+ $ with = str_replace ($ currentPart . '. ' , $ currentAlias . '. ' , $ withExpr );
423+ $ this ->addJoin ($ columnTableName , $ currentAlias , $ this ->accessor ->getValue ($ column , 'joinType ' ), $ with );
417424 }
418425
419426 $ metadata = $ this ->setIdentifierFromAssociation ($ currentAlias , $ currentPart , $ metadata );
@@ -486,7 +493,11 @@ private function setSelectFrom(QueryBuilder $qb)
486493 private function setJoins (QueryBuilder $ qb )
487494 {
488495 foreach ($ this ->joins as $ key => $ value ) {
489- $ qb ->{$ value ['type ' ]}($ key , $ value ['alias ' ]);
496+ if (\array_key_exists ('with ' , $ value ) && null !== $ value ['with ' ]) {
497+ $ qb ->{$ value ['type ' ]}($ key , $ value ['alias ' ], Query \Expr \Join::WITH , $ value ['with ' ]);
498+ } else {
499+ $ qb ->{$ value ['type ' ]}($ key , $ value ['alias ' ]);
500+ }
490501 }
491502
492503 return $ this ;
@@ -714,14 +725,16 @@ private function addSearchOrderColumn($column, $columnTableName, $data)
714725 * @param string $columnTableName
715726 * @param string $alias
716727 * @param string $type
728+ * @param string $with
717729 *
718730 * @return $this
719731 */
720- private function addJoin ($ columnTableName , $ alias , $ type )
732+ private function addJoin ($ columnTableName , $ alias , $ type, $ with = null )
721733 {
722734 $ this ->joins [$ columnTableName ] = [
723735 'alias ' => $ alias ,
724736 'type ' => $ type ,
737+ 'with ' => $ with ,
725738 ];
726739
727740 return $ this ;
0 commit comments