I have tables with articles and users, both have many-to-many mapping to third table - reads.
What I am trying to do here is to get all unread articles for particular user ( user_id not present in table reads ).
My query is getting all articles but those read are marked, which if fine as I can filter them out (user_id field contains id of user in question).
I have an SQL query like this:
SELECT articles.id, reads.user_id
FROM articles
LEFT JOIN
reads
ON articles.id = reads.article_id AND reads.user_id = 9
ORDER BY articles.last_update DESC LIMIT 5;
Which yields following:
articles.id | reads.user_id
-------------------+-----------------
57125839 | 9
57065456 |
56945065 |
56945066 |
56763090 |
(5 rows)
This is fine. This is what I want.
I'd like to get same result in Catalyst using my article model, but I cannot find any option to add conditions to a JOIN clause.
Do you know any way how to add AND X = Y to DBIx JOIN?
I know this can be done with custom resoult source and virtual view, but I have some other queries that could benefit from it and I'd like to avoid creating virtual view for each of them.
Thanks, Canto
AND reads.user_id = 9condition in the query if you want to get articles withreads.user_id = NULL. - krokodilkoSELECT articles.id, reads.user_id FROM articles LEFT JOIN reads ON reads.article_id = articles.id AND reads.user_id = 9 ORDER BY articles.last_update DESC LIMIT 5;id | user_id ----------+--------- 57125839 | 9 57065456 | 56945065 | 56945066 | 56763090 | Sorry I have troubles formatting this comment... - canto