【发布时间】:2023-04-09 18:07:02
【问题描述】:
注意:这是Doctrine many to many: has user liked the article?的后续跟进
拥有三个实体:用户、文章、评论以及用户和评论之间的“commentLikedByUser”多对多关系,我需要从文章中获取 cmets 集合,包括要使用的虚拟属性“userLiked”(布尔)在视图中。
//CommentRepository.php
public function findCommentsFromArticle($articleId,$userId)
{
$result = $this->createQueryBuilder('c')
->where('c.article = :articleId')
->addSelect('COUNT(u) AS userLiked', 'c')
->leftJoin("c.users", 'u', 'WITH', 'u.id = :userId')
->setParameters(['articleId' => $articleId, 'userId' => $userId])
->orderBy('c.id', 'ASC')
->getQuery()
->getResult();
dd($result);
}
这会转储以下内容:
如您所见,“userFavorite”不在正确的位置,而是在 Comment 对象之外。 我该如何解决这个问题?
【问题讨论】:
-
你能分享不同的实体声明吗?
标签: php mysql symfony doctrine many-to-many