Skip to content

Sub-query support in from() and join() #5577

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

Closed
wants to merge 1 commit into from
Closed

Sub-query support in from() and join() #5577

wants to merge 1 commit into from

Conversation

JamShady
Copy link
Contributor

Usage: $query->from(['foo' => $subquery])->join(['bar' => $subquery2], 'foo.id', '=', 'bar.id');

Taken from @jarredholman's work: 3ec55a1

A previous PR was created (#4238) but was closed due to implementation. The implementation in this PR is more correct.

@mvalim
Copy link
Contributor

mvalim commented Sep 8, 2014

that looks nice

@RSully
Copy link
Contributor

RSully commented Sep 23, 2014

I'd like to see a sample using from() without a raw query - would that fit into the use case?

@@ -274,6 +281,13 @@ public function from($table)
*/
public function join($table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
{
if (is_array($table) && count($table) === 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Braces for control statements should be on a new line please.

@JamShady
Copy link
Contributor Author

@GrahamCampbell: Braces fixed & squashed, cheers

@RSully: not sure what you mean?

@RSully
Copy link
Contributor

RSully commented Sep 24, 2014

@JamShady I was thinking it might be over complicating the test by using a Raw - I don't think it's necessary? Just feels like it is adding an unneeded variable to an otherwise simple test case.

@JamShady
Copy link
Contributor Author

@RSully, I see. I didn't write it so can't take the credit, I took it from @jarredholman

@jarredholman
Copy link

It was to test the specific use case I wanted this feature for. Being able to define expressions as columns in the subquery and then using that column in a select or where in the outer query.

It makes writing reusable table/grid ajax code much easier.

Does raw really make it more complicated? Its pretty much required for anything but the most basic operations.

@RSully
Copy link
Contributor

RSully commented Oct 1, 2014

Right but this test isn't about raw, it's just about from/join. My goal would be to remove anything that is unnecessary to keep the test as pure as possible.

@jonspalmer
Copy link

using and array of size one to trigger this behavior seems a bit strange. It would be a lot simpler and clearer to add fromSub and joinSub methods you might also want to support closures as well as builder much like selectSub.

@@ -256,8 +257,15 @@ public function distinct()
*/
public function from($table)
{
$this->from = $table;
if (is_array($table) && count($table) === 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also indicate array type in @param of this parameter in DocBlock.

@jonspalmer
Copy link

Are sub selects like this supported in a sql dialects?

@JamShady
Copy link
Contributor Author

JamShady commented Oct 7, 2014

@bostanio Good suggestion, I don't see why we can't have both. Would like to see what @taylorotwell has to say before implementing it though.

Also, yes they work in MySQL at least. My application uses them quite extensively, mainly to generate a dynamic date range.

@aik099 Done

@JamShady JamShady deleted the pull-request/supply-subqueries-as-tables branch March 2, 2015 10:29
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.

8 participants