2013-05-01 144 views
0

我正在使用symfony2和doctrine2開發一個Web應用程序。Symfony2 + Doctrine2:QueryBuilder中的錯誤

我在應用程序中有一個窗體,它有一個從實體填充的下拉列表。我已經配置了一個query_builder來過濾下拉字段中的值。

public function buildForm(FormBuilderInterface $builder, array $options) { 
    $centerId = $this->centerId; 

    $builder->add("glCode", "entity", array(
     "class" => "MyBundle:GlCode", 
     "query_builder" => function(EntityRepository $er) use($centerId) { 
      return $er->createQueryBuilder("g") 
       ->join("g.account", "a") 
       ->where("g.id NOT IN (SELECT g2.id FROM MyBundle:OtherFixedCost c JOIN MyBundle:GlCode g2)") 
     } 
    )); 

此代碼產生錯誤:預期學說\ ORM \查詢\詞法:: T_WITH,得到了「)」

我試圖做的查詢生成器我想同樣的事情做以下DQL

SELECT g FROM MyBundle:GlCode g 
JOIN g.account a 
WHERE g.id NOT IN (
    SELECT g2.id FROM MyBundle:OtherFixedCost c INNER JOIN MyBundle:GlCode g2 
) 

我不知道如果我失去了一些東西,但顯然不存在的方式,直接使用DQL在窗體類中。所以,我被迫使用QueryBuilder,但我得到了上述錯誤。

回答

0

的問題是在你的其中條款,你應該使用功能notIn(),而不是僅僅對DQL寫的一切。看看在documentation

也許thisthis可以幫助,第二個實際上是相當類似的查詢,我引用了一些代碼有:

$qb->select('l') 
    ->from('Entity\Location', 'l') 
    ->where('l.state = :state') 
    ->setParameter('state', 'UT') 
    ->andWhere($qb->expr()->notIn('u.id', 
     $qb->select('l2.id') 
      ->from('Entity\Location', 'l2') 
      ->where(l2.location_type = ?1 AND l2.population < ?2) 
      ->setParameters(array(1=> 1, 2 => 1000)) 
)); 

有子查詢如何構建看看。

0

子查詢應該通過別名加入實體,而不是直接加入實體。 IE瀏覽器。

(SELECT g2.id FROM MyBundle:OtherFixedCost c JOIN MyBundle:GlCode g2) 

嘗試

(SELECT g2.id FROM MyBundle:OtherFixedCost c JOIN c.GlCodes g2) 

或任何OtherFixedCost和GlCode之間的關係的名字。也許這會有所幫助,我一直在寫這樣的子查詢並且沒有問題。