2012-08-11 52 views
5

我是新來的Symfony2的查詢生成器,這裏是我做的:錯誤:PathExpression無效。必須是StateFieldPathExpression

$builder 
     ->add('access', 'entity', array(
      'label' => 'Behörigheter', 
      'multiple' => true, // Multiple selection allowed 
      'expanded' => true, // Render as checkboxes 
      'property' => 'name', // Assuming that the entity has a "name" property 
      'class' => 'BizTV\ContainerManagementBundle\Entity\Container', 
      'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) { 
       $qb = $er->createQueryBuilder('a'); 
       $qb->where('a.containerType IN (:containers)', 'a.company = :company'); 
       $qb->setParameters(array('containers' => array(1,2,3,4), 'company' => $company)); 

       return $qb; 
      } 
     ));  

除了我想containerType訂購我的實體(這是一個關係領域,FK),它工作正常。

當我加入這一行:

$qb->orderBy('a.containerType', 'ASC'); 

我得到錯誤:無效PathExpression。必須是StateFieldPathExpression。

那麼,什麼是這個 - 我可以使用關係領域containerType在我的where子句,但不是我的那種條款?還是我錯過了別的?

回答

6
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) { 
     $qb = $er->createQueryBuilder('a'); 
     $qb->innerJoin('a.containerType', 'ct'); 
     $qb->where('a.containerType IN (:containers)', 'a.company = :company'); 
     $qb->orderBy('ct.id', 'ASC'); // Or the field you want from containerType 
     $qb->setParameters(array('containers' => array(1,2,3,4), 'company' => $company)); 

     return $qb; 
} 

你不能使用容器類型與排序子句,因爲這是一個實體與當前! 查詢生成器不知道要使用的字段(即使containerType表示實體的ID!)。 因此,您需要加入實體並手動對其字段進行排序!

+0

是的,而是由FK排序會做在這種情況下,但顯然我不能。只是發現奇怪的是,它會讓我在沒有連接的情況下在where子句中使用它,但不是這樣。謝謝,但會嘗試。 – 2012-08-14 00:02:26

相關問題