2015-07-21 65 views
4

我有一個存儲庫這樣的代碼:學說不存在子查詢

public function getNotAssignedBy($speciality, $diploma) 
{ 
    $qb = $this->createQueryBuilder('j') 
     ->select('DISTINCT(j.id) id', 'j.firstName', 'j.lastName', 'j.dateBirth', 'j.sex') 
     ->leftJoin('j.qualifications', 'q') 
    ; 


    if ($speciality) { 
     $qb->andWhere('q.speciality = :speciality_id')->setParameter('speciality_id', $speciality); 
    } 
    if ($diploma) { 
     $qb->andWhere('q.diploma = :diploma_id')->setParameter('diploma_id', $diploma); 
    } 

    $result = $qb->getQuery()->getResult(); 

    return $result; 
} 

我怎麼只能得到行其中id不是在另一個實體存在?

任何幫助。由於

回答

13

你可以像這樣實現它:

.... 
    // construct a subselect joined with an entity that have a relation with the first table as example user 
    $sub = $this->createQueryBuilder(); 
    $sub->select("t"); 
    $sub->from("AnotherEntity","t"); 
    $sub->andWhere('t.user = j.id'); 

    // Your query builder: 
    $qb->andWhere($qb->expr()->not($qb->expr()->exists($sub->getDQL()))); 

希望這有助於

+1

謝謝利瑪竇。有用。 –