I have this very usual question. but I spent a lot of time cannot solve. I am using symfony2 with doctrine. any help? appreciate

let say I have 3 entities: Post, Tag, PostTag

  • Post has many Tags
  • Tag has many Posts

I need to do a Post listing filter by Tag.

SELECT p,pt FROM PostTag pt LEFT JOIN pt.post p

this result duplicated posts due to a Post has many PostTags.

any solution please advice

有帮助吗?

解决方案

SELECT p FROM Post p JOIN p.postTags pt JOIN pt.tag t WHERE t.name = :name

Don't forget to make sure Post has one-to-many with PostTag.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top