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

Added joins functionality #9

Merged
merged 2 commits into from Sep 4, 2018
Merged

Added joins functionality #9

merged 2 commits into from Sep 4, 2018

Conversation

nmc9
Copy link

@nmc9 nmc9 commented Aug 10, 2018

Added methods to the Filter.php file to allow filters to join from other tables.
Also included testCases and stubs for further testing of the join functions

Nick Caruso added 2 commits August 10, 2018 09:28
- split the setJoin into two functions
- setJoin returns the builder
- addJoin returns the filter
- -This allows joins to be stringed together and finished with setJoin()
@nmc9 nmc9 closed this Aug 10, 2018
@nmc9 nmc9 reopened this Aug 10, 2018
@Kyslik
Copy link
Owner

Kyslik commented Aug 13, 2018

Could you explain what is the rationale behind the PR, in the filter itself you have Builder instance available so stock methods ->join*() work.

@nmc9
Copy link
Author

nmc9 commented Aug 13, 2018

Sorry i should have more clear about what I actually did. Yes you can use the join methods in the builder but it can cause some problems.
The main reason for having the join in the filter class is for situations like this:
If you have multiple fields on a joined table (like created_at, updated_at) if you try to set them up as separate filters the join duplicates. When it comes time to build the query you end up with multiple joins to the same table.
For Example a setup like this:

public function usercreatedat($created_at){
    return $this->getBuilder()->join("user","user.id","role.user_id")
    ->where("user.created_at",$created_at);
}

public function userupdatedat($updated_at){
    return $this->getBuilder()->join("user","user.id","role.user_id")
    ->where("user.updated_at",$updated_at);
}

Would produce this:

select * inner join "user" on "user"."id" = "role"."user_id" inner join "user" on "user"."id" = "role"."user_id" where "user"."created_at" = '2018-08-13 12:33:05' and "user"."updated_at" = '2018-08-13 12:33:05'

So i added a check in the filter table that only adds the join if it's not already in the builder.

Also another option if you are worried about muddying up the Filter class would be to extend it with a JoinableFilter class. Just a thought

@Kyslik
Copy link
Owner

Kyslik commented Aug 13, 2018

Makes sense, I will have to dig in to this :) and figure how best it would be, thank you for contribution.

ETA: when Laravel 5.7 comes out.

@Kyslik Kyslik merged commit c5648e3 into Kyslik:master Sep 4, 2018
Kyslik added a commit that referenced this pull request Sep 4, 2018
@Kyslik
Copy link
Owner

Kyslik commented Sep 4, 2018

@nmc9
I added trait JoinSupport that adds the methods you've created, but they all accept signatures like Builder::join(...)|leftJoin(...)|rightJoin(..,), even closures etc. Tests are unchanged (the queries themselves) so I hope I did not cripple any functionality.

I will have to work on readme.

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

2 participants