Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add support to use subqueries as a where condition on a join clause #21008

Merged
merged 1 commit into from
Sep 11, 2017
Merged

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Sep 5, 2017

As reported on issue #19695 when a user tries to use a ->whereIn() or a ->whereExists() with a sub-query in a join clause, the query builder does not build the inner query where condition as expected.

Example:

<?php

\Route::get( '/test', function () {
    $builder = \DB::table( 'users' )
        ->join( 'password_resets', function ( JoinClause $join ) {
            $join->on( 'users.email', '=', 'password_resets.email' );
            $join->whereIn( 'email', function ( $query ) {
                // dummy example just to demonstrate issue
                $query->select( 'email' )
                    ->from( 'users' )
                    ->where( 'id', '>', 1 );
            } );
        } );

    return $builder->toSql();
} );

Generates this SQL statment:

select * 
from 
    "users" 
    inner join "password_resets" 
        on "users"."email" = "password_resets"."email" 
        and "email" in (select "email" from "users" on "id" > ?)

instead of the expected one:

select * 
from 
    "users" 
    inner join "password_resets" 
        on "users"."email" = "password_resets"."email" 
        and "email" in (select "email" from "users" WHERE "id" > ?)

The is caused due to JoinClause@newQuery() method override returns a new JoinClause instance instead of a Query Builder instance. This makes Grammar@concatenateWhereClauses() to compile the nested query where clause not as expected.

I tried to change JoinClause@newQuery() method override to return a Query Builder instance. But doing so fails the nested where tests within a join clause.

This PR adds a protected method on the Query Builder that returns a new Builder instance for sub-queries so that the JoinClause class can override it properly.

Tests were added to cover these additional cases.

Copy link
Member

@themsaid themsaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants