Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get articles by tag (many to many)

Hi! I have made model connections (many to many) and I can easily get the tags by article

Tables:

articles (id, name)

tags (id, name)

articlestags (articleid, tagid)

categories (id, name)

articlescategories (articleid, categoryid)

  1. How to get articles by tagid and categoryid (the orm way)? for example get articles with tagid = 1 and categoryid = 2
  2. How to get articles with multiple tags and categories? for example get articles that have tagid = 1, tagid = 2, categoryid = 3, categoryid = 4


106.8k
Accepted
answer
edited Oct '15
  1. You have to use modelsManager or static ::query method on articletags and join articles where tagid = 1 and join articlescategories and add where categoryid = 2.

  2. Same, just use modelsManager or static ::query method, WHERE tagid =1 OR tagid =2 OR categoryid = 3 OR categoryid = 4.

In the "orm way" its gonna be hard and you will make so many select operations, with modelsManager or static ::query method you will do only one.