菜鸟警报;
我有Post
和Tag
这样的实体:
Post.php
/**
* Many posts can have many tags. This is the owning side.
* @ORM\ManytoMany(targetEntity="Tag", inversedBy="posts")
* @ORM\JoinTable(name="post_tag")
*/
protected $tags;
Tag.php
/**
* Many tags can belong to many different posts. This is the inverse side
* @ORM\ManytoMany(targetEntity="Post", mappedBy="tags")
*/
protected $posts;
现在,我想用其标签查询所有帖子。
为此,我在存储库中使用queryBuilder并能够成功检索结果。
$query = $this->createQueryBuilder('P')
->select('P.title, T.name')
->leftJoin('P.tags', 'T')
->where('P.id = 1')
->getQuery()
->execute();
但是,您可能会想到,此查询将获取重复项。因此,如果我为一个帖子使用两个标签,则数组内将有两个类似的帖子。
Output
array(
array(title=>'Post One', name=>'php'),
array(title=>'Post One', name=>'python'),
)
是否存在一种将这些标签转换为数组集合并将其填充回最终发布数组的方法?