多对多主义:用户喜欢这篇文章吗? [英] Doctrine many to many: has user liked the article?

查看:70
本文介绍了多对多主义:用户喜欢这篇文章吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个实体

用户文章以及两者之间的likedByUsers"多对多关系.

User Article and a "likedByUsers" Many To Many relationship between both.

当我展示一篇文章时,我想知道用户是否喜欢它,所以会显示一个心形图标.

When I show an article, I want to know if the user has liked it so a heart icon is shown.

我在 ArticleRepository 中有这个:

I've got this in the ArticleRepository:

public function findOneBySlug($slug,$userId): ?Pack
{
    return $this->createQueryBuilder('p')
        ->andWhere('p.slug = :val')
        ->setParameter('val', $slug)
        ->addSelect('COUNT(u) AS userLike', 'p')
        ->leftJoin("p.users", 'u', 'WITH', 'u.id = :userId')
        ->setParameter('userId', $userId)
        ->getQuery()
        ->getOneOrNullResult()
    ;
}

但它抛出一个错误:

App\Repository\ArticleRepository::findOneBySlug() 的返回值必须是App\Entity\Article 的一个实例或 null,返回数组

Return value of App\Repository\ArticleRepository::findOneBySlug() must be an instance of App\Entity\Article or null, array returned

我想将userLike"(bool)添加到文章返回的实体中.谁能帮帮我?

I want to add "userLike" (bool) to the Article returned entity. Anyone can help me out?

推荐答案

在查询构建器上调用 addSelect(...) 可能会更改返回类型/格式.

calling addSelect(...) on a query builder might change the return type / format.

在您的特定情况下,前一个数据库结果类似于 [... all the article properties ...] 水化和 getOneOrNullResult 变成一个 Article 或 null.

in your particular case, the former db result was something like [... all the article properties ...] which hydration and the getOneOrNullResult turns into one Article or null.

新格式看起来像

[...所有文章属性..., userlike],水合变成了[Article, userlike],不可能变成一篇文章或空结果,因为它是一个更复杂"的数组.

[... all the article properties ..., userlike], which hydration turns into [Article, userlike] which can't possibly turned into one Article or a null result, because it's a "more complex" array.

所以你必须使用不同的结果获取器.根据你的函数的调用者期望作为返回值的内容(我期待一篇文章^^)你可能应该重命名函数或添加文章上的一个虚拟属性来隐藏用户喜欢的东西,所以你可以只返回文章或空.

So you have to use a different result fetcher. Depending on what the caller of your function expects as a return value (I would expect an article ^^) you maybe should rename the function or add a virtual property on article to hide the userlike or something, so you can return just the Article or null.

所以我会选择的解决方案:

So the solution that I would choose:

$result = $this->createQueryBuilder(...)
   //...
   ->getSingleResult();
if(!$result) { 
    // empty result, obviously
    return $result; 
}
// $result[0] is usually the object.
$result[0]->userLike = $result['userLike']; 
// or $result[0]->setUserLike($result['userLike'])

return $result[0];

btw: $this->createQueryBuilder($alias) 在存储库中自动调用 ->select($alias),所以你不必addSelect('... userLike', 'p') 只需执行 addSelect('... userLike')

btw: $this->createQueryBuilder($alias) in a repository automatically calls ->select($alias), so you don't have to addSelect('... userLike', 'p') and just do addSelect('... userLike')

这篇关于多对多主义:用户喜欢这篇文章吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆