2012-12-19 29 views
0

我有一個與實體相關的實體Person(p)與實體NotificationType相關的通知。如何用多個連接編寫這個原則查詢?

  • 有些人已經收到類型1的通知(notification_type_id = 1)。
  • 有些人已收到通知但不是類型1
  • 有些人根本沒有收到任何消息。

在全球範圍內,我想找回那些誰沒有收到1

我寫了這個查詢類型的消息:

$qb = $this->createQueryBuilder('p') 
     ->leftJoin('p.notifications', 'n') 
     ->leftJoin('p.notification_type', 'nt') 
     ->addSelect(array('p','n','nt')) 
     ->where('nt.id NOT IN (1)') 
     ->orderBy('p.name', 'ASC'); 
return $qb->getQuery()->getResult(); 

但與此查詢我只得到那些誰已收到通知但不是第1類,我沒有收到那些沒有收到通知的人。

我該如何糾正我的查詢有那些?如您所願

非常感謝您的幫助

回答

0

沒有與空值(您加入其中沒有消息)將無法解析。對於eq或ne同樣如此。

嘗試:

$qb = $this->createQueryBuilder('p'); 
$results = $qb->leftJoin('p.notifications', 'n') 
       ->leftJoin('p.notification_type', 'nt') 
       ->addSelect(array('p','n','nt')) 
       ->where('nt.id !=1') 
       ->orWhere('nt.id IS NULL') 
       ->orderBy('p.name', 'ASC'); 
return $qb->getQuery()->getResult(); 

你也可以把這些作爲你的加入條件,請參閱學說代碼

作爲的13.2.5. The Expr class不談,你也許並不需要,因爲你可以選擇通過實體中的獲取者以編程方式訪問這些值。