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

Adding WHERE clause to a relation #28

Open
salain opened this issue Jun 28, 2021 · 6 comments
Open

Adding WHERE clause to a relation #28

salain opened this issue Jun 28, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@salain
Copy link

salain commented Jun 28, 2021

HI,

Is there anyway to add a WHERE clause to a relation?
For example retrieve all authors details with all of their posts created in 2020.

@MGatner
Copy link
Collaborator

MGatner commented Jun 28, 2021

Yes. You can use all the usual Query Builder methods intermixed with Relations methods.

@salain
Copy link
Author

salain commented Jun 28, 2021

Sorry, but could you give an example as I can not get it to work.
If I have this and want to filter posts on year 2020:

$author = $authorModel->with('posts')->where('year', '2020')->findAll();

I get error column year not found in where clause

@MGatner
Copy link
Collaborator

MGatner commented Jun 28, 2021

Maybe try 'posts.year' instead? I cannot dig into this right now but you could try the CodeIgniter forums for help.

@salain
Copy link
Author

salain commented Jun 29, 2021

Adding the table name does not work as it adds the where clause to the main query.
I will try to find a workaround for this.

Thanks

@MGatner
Copy link
Collaborator

MGatner commented Jun 29, 2021

Ahh I see what you mean now. No, this is not currently possible, though we've had requests for handling content from join tables and this would overlap with conditional relationships. I will consider it a feature request.

@MGatner MGatner added the enhancement New feature or request label Jun 29, 2021
@salain
Copy link
Author

salain commented Jun 29, 2021

Thanks.
I have a basic workaround to suite my tests for now.

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

No branches or pull requests

2 participants