假设我有“新闻”实体,它有ManyToMany“标记”关系
class News
{
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Entity\Vocabulary\Tag")
*/
private Collection $tags;
}我有这样的疑问:
public function getList(
array $tags = null,
): Query {
if (null !== $tags) {
$qb->andWhere('nt.id IN (:tags)');
$qb->setParameter('tags', $tags);
}
}问题是,当我传递"Tag1“、"Tag2”时,它会选择具有第一个标签或第二个标签的新闻,但不是同时包含两个标签的新闻。如何重写查询以选择同时具有两个标记的新闻?
发布于 2021-05-30 17:02:45
有些事情首先要注意:
对于理论注释,可以使用::class-constant:
use App\Domain\Entity\Vocabulary\Tag;
class News
{
/**
* @ORM\ManyToMany(targetEntity=Tag::class)
*/
private Collection $tags;
}如果$tags数组为空,则将抛出一个异常,因为空值集无效,至少在mysql中是这样的:
nt.id IN () # invalid!现在谈一谈问题:
使用SQL-聚合函数COUNT和GROUP BY,我们可以计算所有新闻的标记数。加上允许标记的条件,每个新闻的标记数必须等于标记数组中的标记数:
/**
* @var EntityManagerInterface
*/
private $manager;
...
/**
* @param list<Tag> $tags - Optional tag filter // "list" is a vimeo psalm annotation.
*
* @return list<News>
*/
public function getNews(array $tags = []): array
{
$qb = $this->manager
->createQueryBuilder()
->from(News::class, 'news')
->select('news')
;
if(!empty($tags)) {
$tagIds = array_unique(
array_map(static function(Tag $tag): int {
return $tag->getId();
}) // For performance reasons, give doctrine ids instead of objects.
); // Make sure duplicate tags are handled.
$qb
->join('news.tags', 'tag')
->where('tag IN (:tags)')
->setParameter('tags', $tagIds)
->addSelect('COUNT(tag) AS HIDDEN numberOfTags')
->groupBy('news')
->having('numberOfTags = :numberOfTags')
->setParameter('numberOfTags', count($tags))
;
}
return $qb
->getQuery()
->getResult()
;
}https://stackoverflow.com/questions/67499992
复制相似问题