@@ -3551,7 +3551,20 @@ private function remapDottedQueryAttributes(array $queries, array $joinTablePref
35513551 private function remapDottedQuery (BaseQuery $ query , array $ aliasSet , array $ mainAttributes ): void
35523552 {
35533553 $ method = $ query ->getMethod ();
3554- if ($ method ->isJoin () || $ method === Method::Select) {
3554+ if ($ method === Method::Select) {
3555+ return ;
3556+ }
3557+
3558+ if ($ method ->isJoin ()) {
3559+ if ($ query ->isNestedJoin ()) {
3560+ foreach ($ query ->getJoinOnQueries () as $ onQuery ) {
3561+ if ($ onQuery ->getMethod () === Method::On) {
3562+ continue ;
3563+ }
3564+ $ this ->remapDottedQuery ($ onQuery , $ aliasSet , $ mainAttributes );
3565+ }
3566+ }
3567+
35553568 return ;
35563569 }
35573570
@@ -3815,30 +3828,30 @@ private function remapJoinQueries(array &$queries): array
38153828 $ resolvedTable = $ this ->getSQLTableRaw ($ this ->filter ($ joinTable ));
38163829 $ query ->setAttribute ($ resolvedTable );
38173830
3818- $ values = $ query ->getValues ();
38193831 $ method = $ query ->getMethod ();
3820- $ aliasIndex = ($ method === Method::CrossJoin || $ method === Method::NaturalJoin) ? 0 : 3 ;
3821- $ joinAlias = $ this ->sanitizeJoinAlias (
3822- \is_string ($ values [$ aliasIndex ] ?? null ) ? $ values [$ aliasIndex ] : ''
3823- );
3832+ $ joinAlias = $ this ->sanitizeJoinAlias ($ query ->getJoinAlias ());
38243833 if ($ joinAlias === '' ) {
38253834 $ joinAlias = 'j ' .$ joinIndex ;
38263835 }
38273836 $ joinIndex ++;
38283837
3829- if ($ aliasIndex === 3 && \count ($ values ) >= 3 ) {
3830- $ left = $ values [0 ] ?? null ;
3831- $ right = $ values [2 ] ?? null ;
3832- if (! \is_string ($ left ) || ! \is_string ($ right )) {
3833- throw new QueryException ('Join columns must be strings ' );
3838+ if ($ method === Method::CrossJoin || $ method === Method::NaturalJoin) {
3839+ $ query ->setValues ([$ joinAlias ]);
3840+ } elseif ($ query ->isNestedJoin ()) {
3841+ $ query ->setValues ($ this ->remapNestedJoinValues ($ query , $ alias , $ joinAlias ));
3842+ } else {
3843+ $ values = $ query ->getValues ();
3844+ if (\count ($ values ) >= 3 ) {
3845+ $ left = $ values [0 ] ?? null ;
3846+ $ right = $ values [2 ] ?? null ;
3847+ if (! \is_string ($ left ) || ! \is_string ($ right )) {
3848+ throw new QueryException ('Join columns must be strings ' );
3849+ }
3850+ $ values [0 ] = $ this ->qualifyJoinColumn ($ left , $ alias );
3851+ $ values [2 ] = $ this ->qualifyJoinColumn ($ right , $ joinAlias );
3852+ $ values [3 ] = $ joinAlias ;
3853+ $ query ->setValues ($ values );
38343854 }
3835- $ values [0 ] = $ this ->qualifyJoinColumn ($ left , $ alias );
3836- $ values [2 ] = $ this ->qualifyJoinColumn ($ right , $ joinAlias );
3837- $ values [3 ] = $ joinAlias ;
3838- $ query ->setValues ($ values );
3839- } elseif ($ aliasIndex === 0 ) {
3840- $ values [0 ] = $ joinAlias ;
3841- $ query ->setValues ($ values );
38423855 }
38433856
38443857 $ joinTablePrefixes [] = ['table ' => $ joinTable , 'alias ' => $ joinAlias ];
@@ -3847,6 +3860,39 @@ private function remapJoinQueries(array &$queries): array
38473860 return $ joinTablePrefixes ;
38483861 }
38493862
3863+ /**
3864+ * @return list<mixed>
3865+ */
3866+ private function remapNestedJoinValues (BaseQuery $ query , string $ mainAlias , string $ joinAlias ): array
3867+ {
3868+ $ values = [$ joinAlias ];
3869+ foreach ($ query ->getJoinOnQueries () as $ onQuery ) {
3870+ $ values [] = $ this ->remapNestedJoinOnQuery ($ onQuery , $ mainAlias , $ joinAlias );
3871+ }
3872+
3873+ return $ values ;
3874+ }
3875+
3876+ private function remapNestedJoinOnQuery (BaseQuery $ onQuery , string $ mainAlias , string $ joinAlias ): BaseQuery
3877+ {
3878+ if ($ onQuery ->getMethod () !== Method::On) {
3879+ return $ onQuery ;
3880+ }
3881+
3882+ $ values = $ onQuery ->getValues ();
3883+ $ left = $ values [0 ] ?? null ;
3884+ $ right = $ values [2 ] ?? null ;
3885+ if (! \is_string ($ left ) || $ left === '' || ! \is_string ($ right ) || $ right === '' ) {
3886+ throw new QueryException ('Join ON requires left and right columns ' );
3887+ }
3888+
3889+ $ values [0 ] = $ this ->qualifyJoinColumn ($ left , $ mainAlias );
3890+ $ values [2 ] = $ this ->qualifyJoinColumn ($ right , $ joinAlias );
3891+ $ onQuery ->setValues ($ values );
3892+
3893+ return $ onQuery ;
3894+ }
3895+
38503896 /**
38513897 * @param array<BaseQuery> $queries
38523898 */
0 commit comments