Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:38:15+00:00 2026-06-15T08:38:15+00:00

This is a hypothetical question, because I’m trying to get my head around Doctrine

  • 0

This is a hypothetical question, because I’m trying to get my head around Doctrine ORM and having a hard time replicating what I’d do in plain SQL.

Suppose I have a simple ManytoMany relationship between tags and posts. They would map so that Post::tags is the owning side and Tag::posts is the reverse mapping.

I understand that with Doctrine’s DQL I can select posts containing their tags, or tags referencing their posts with the following 2 queries

(1) SELECT p, t FROM MyBundle:Post p JOIN p.tags t WHERE p.id = :id

(2) SELECT t, p FROM MyBundle:Tag t JOIN t.posts p WHERE t.id = :id

But when I want to fetch posts by a number of tags, I have to choose between these:

(3) SELECT p, t FROM MyBundle:Post p JOIN p.tags t WHERE t.value IN ('foo','bar')

(4) SELECT t, p FROM MyBundle:Tag t JOIN t.posts p WHERE t.value IN ('foo','bar')

Both of these seem wrong.

With (3) I imagine the DB would scan the whole posts table before reducing the set to those tagged

With (4) I get a collection of tag objects back which is the inverse of what I’m after.

I tried the following, because logically it seemed to mirror what I’d do in SQL:

SELECT p, pt FROM MyBundle:Tag t JOIN t.posts p JOIN p.tags pt
                 WHERE t.value IN ('foo','bar') GROUP BY p.id

It doesn’t work because Doctrine insists I select the root entity

What’s the best way to select on tags but get back unique posts as full objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:38:17+00:00Added an answer on June 15, 2026 at 8:38 am

    If I understood well the question, what you want is select all posts that have at least one tag whose value is in a defined set (“foo”, “bar”, …) and ignore all other posts.

    Let start from (3):

    SELECT p, t FROM MyBundle:Post p JOIN p.tags t WHERE t.value IN ('foo','bar')
    

    In MySQL this means approximately: “Select all posts having a tag whose value is either foo or bar”.

    However in DQL this means “Select all Posts and attach only tags whose values is either foo or or bar”. Meaning that the query will return the set of all posts, but with tags filtered. This difference of behavior is due to the fact that doctrine has to create objects with the array returned by the Database Layer. When you make a MySQL query with JOINs, the results will be an array with duplicates. However, in the object world, duplicate posts have to be merged in a single Post object…

    I believe the solution to your query is to filter when the join is made:

    SELECT p,t FROM MyBundle:Post p JOIN p.tags t WITH t.value IN ('foo', 'bar')
    

    you can understand the WITH DQL clause as if it added a condition in the ON MySQL clause. I.e. the above query is similar to MySQL query below:

    SELECT p.*, t.* FROM posts AS p JOIN posts_tags AS pt ON pt.post_id = p.id JOIN tags AS t ON pt.tag_id = t.id AND t.value IN ("foo", "bar")
    

    Remark that there is no more WHERE clause. The condition moved in the JOIN condition clause…

    Hope this helps 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble wrapping my head around how to write this query. A hypothetical
This is more of a hypothetical question. I am wondering if anyone knows if/how
This is more of a hypothetical question as I am using .NET 3.5 more
This question is quite hypothetical but I'll ask anyway. Let's say you start a
(This is a hypothetical question for discussion, I have no actual problem). Say that
(This was meant to be a general hypothetical question, me whining that .NET was
This is just a hypothetical question, if you wouldn't have the Array and the
This is probably going to be more of a discussion or hypothetical question, but
this is more of a fundamental question at how apache/threading works. in this hypothetical
This is a hypothetical question: I want to build a Chrome extension that'll track

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.