tattersoftware / codeigniter4-relations

Entity relationships for CodeIgniter 4
MIT License
87 stars 20 forks source link

Adding WHERE clause to a relation #28

Open salain opened 3 years ago

salain commented 3 years ago

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 commented 3 years ago

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

salain commented 3 years ago

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 commented 3 years ago

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

salain commented 3 years ago

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 commented 3 years ago

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.

salain commented 3 years ago

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