【问题标题】:Doctrine2 DQL issue with COUNT = 0COUNT = 0 的 Doctrine2 DQL 问题
【发布时间】:2013-11-28 10:37:07
【问题描述】:

到目前为止,我有以下查询:

   $shopQuery = $qb->select('DISTINCT u')
                    ->from("BlahUserBundle:User", 'u')
                    ->innerJoin('u.followers', 'followers')
                    ->andWhere('followers.id != :userId')
                    ->setParameter('userId', $user->getId())
                    ->orWhere('') //or where those user who doesn't have a follower yet
                    //->setMaxResults(5)
                    ;

我正在尝试找到一种方法来查询所有没有追随者且追随者不是我自己的用户(在这种情况下,我的自己是$user->getId())。我该怎么做?

【问题讨论】:

  • 如果您使用innerJoin,您将无法获得您想要的结果,因为如果用户没有关注者,您将无法获得该行。我认为您需要一个子查询。让我考虑一下:)
  • @Pi Wi 是对的,但您可以用左连接替换内连接以匹配两个结果。

标签: php mysql symfony orm doctrine-orm


【解决方案1】:

试试这个

$shopQuery = $qb->from("BlahUserBundle:User", 'u')
            ->leftJoin(
                'u.followers',
                'followers',
                'on',
                'followers.id != :userId'
            )
            ->where('followers.id IS NULL')
            ->setParameter('userId', $user->getId());
$shopQuery->getQuery()->getResults();

【讨论】:

    猜你喜欢
    • 2015-03-02
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多