0

我有与 manyToMany 关系的“发布”实体

 /**
 * @ORM\ManyToMany(targetEntity="Core\CoreBundle\Entity\User", cascade={"persist", "remove", "merge"})
 * @ORM\JoinTable(name="blogregion_post_views")
 */
protected $views;

 /**
 * @ORM\ManyToMany(targetEntity="Application\BlogRegionBundle\Entity\PostTag", cascade={"persist", "remove", "merge"})
 * @ORM\JoinTable(name="blogregion_tags_match")
 */
protected $tags;

我想获得一个新帖子

    $query = $this->getEm()->createQuery("
        SELECT p FROM BlogRegionBundle:Post p LEFT JOIN p.tags t LEFT JOIN p.views v
        WHERE t.id = :blogId AND p.state = :state AND p.deleted = 0 AND (v <> :user OR v IS NULL )

    ")
        ->setParameter('blogId', $blogId)
        ->setParameter('state', 1)
        ->setParameter('user', $this->getUser())
    ;

但如果我的用户存在于“views”关系中,它会返回“Post”

我该如何解决这个问题?

4

1 回答 1

1

您可以尝试使用MEMBER OF条件。

SELECT p FROM BlogRegionBundle:Post p LEFT JOIN p.tags t WHERE t.id = :blogId AND p.state = :state AND p.deleted = 0 AND :user NOT MEMBER OF p.views

来自http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples

于 2013-06-18T01:40:17.687 回答