2013-11-15 66 views
1

我到目前爲止以下查詢:Doctrine2 DQL問題與COUNT = 0

$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())。我該怎麼做?

+1

。我認爲你需要一個子查詢。讓我想一想:) –

+0

@Pi Wi是對的,但是你可以用左連接代替你的內連接來匹配兩個結果。 –

回答

1

如果使用innerJoin你不會得到你想要的,因爲,如果用戶沒有跟隨你不會得到該行的結果試試這個

$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();