2017-02-09 45 views
0

我想在if條件中添加一個連接到我的查詢中,但它返回一個錯誤。 我們如何在教條中做到這一點? 有沒有像這樣的: $ qb-> andWhere('p。$ gender =:g') - > setParameter('g',$ gender); 加入主義symfony?如何在教條中通過qb連接連接?

$qb->select("p")->from("PfmSanadBundle:Person", "p"); 

    if ((isset($province) && trim($province) !== '') && (isset($City) && trim($City) !== '')){ 
     $qb->join("p" ,"students", "ps") 
      ->join("ps" ,"organization", "po") 
      ->join("po" ,"cityProvince", "pc") 
      ->join("pc" ,"province", "pp"); 
    } 
    if ((isset($province) && trim($province) !== '') && !isset($City)) { 
     $qb->join("p.students", "ps") 
      ->join("ps.organization", "po"); 
    } 
    $res = $qb->getQuery()->getArrayResult(); 

誤差(郵遞員):

"error": { "code": 500, "message": "Internal Server Error", "exception": [ { "message": "[Semantical Error] line 0, col 49 near 'p students INNER': Error: Class 'p' is not defined.", "class": "Doctrine\ORM\Query\QueryException", "trace": [ {

回答

0

加入方法使用被以下

// Example - $qb->join('u.Group', 'g', Expr\Join::WITH, $qb->expr()->eq('u.status_id', '?1')) 
// Example - $qb->join('u.Group', 'g', 'WITH', 'u.status = ?1') 
// Example - $qb->join('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id') 
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null); 

因此以下行

$qb->join("p" ,"students", "ps") 
    ->join("ps" ,"organization", "po") 
    ->join("po" ,"cityProvince", "pc") 
    ->join("pc" ,"province", "pp"); 

變化

$qb->join("p.students", "ps") 
    ->join("ps.organization", "po") 
    ->join("po.cityProvince", "pc") 
    ->join("pc.province", "pp"); 
+0

謝謝,現在一切都好了! – vahid