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

Mat*_*der 5 php forms doctrine symfony

我是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;
            }
        ));     
Run Code Online (Sandbox Code Playgroud)

它工作正常,除了我想通过containerType(这是一个关系字段,FK)命令我的实体.

当我添加这一行时:

$qb->orderBy('a.containerType', 'ASC');
Run Code Online (Sandbox Code Playgroud)

我收到错误:无效的PathExpression.必须是StateFieldPathExpression.

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

Syb*_*bio 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;
}
Run Code Online (Sandbox Code Playgroud)

你不能将containerType与sort子句一起使用,因为这是一个与当前有关的实体!查询构建器不知道要使用的字段(甚至containerType表示实体的id!).因此,您需要加入实体并手动按字段排序!