gpt4 book ai didi

php - 多对多 Doctrine : count left join collection

转载 作者:行者123 更新时间:2023-11-29 15:40:34 24 4
gpt4 key购买 nike

注意:这是 Doctrine many to many: has user liked the article? 的后续内容

拥有三个实体:用户、文章、评论以及用户和评论之间的“commentLikedByUser”多对多关系,我需要从文章中获取评论集合,包括要使用的虚拟属性“userLiked”(bool)在 View 上。

//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);
}

转储以下内容:

enter image description here

如您所见,“userFavorite”的位置不正确,而是位于 Comment 对象之外。我该如何解决这个问题?

最佳答案

也许试试这个代码:

//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')
->andWhere('u.id = :userId')
->setParameters(['articleId' => $articleId, 'userId' => $userId])
->orderBy('c.id', 'ASC')
->getQuery()
->getResult();

dd($result);

}

关于php - 多对多 Doctrine : count left join collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57707422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com