错误:无效的PathExpression。必须是StateFieldPathExpression

时间:2012-08-11 22:39:39

标签: php forms symfony doctrine

我是Symfony2查询构建器的新手,这就是我所做的:

    $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。

那么这是什么 - 我可以在我的where子句中使用关系字段containerType但不能在我的sort子句中使用?或者我错过了其他什么?

1 个答案:

答案 0 :(得分: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与sort子句一起使用,因为这是一个与当前有关的实体! 查询构建器不知道要使用的字段(甚至containerType表示实体的id!)。 因此,您需要加入实体并手动按字段排序!