【问题标题】:Error: Invalid PathExpression. Must be a StateFieldPathExpression错误:无效的路径表达式。必须是 StateFieldPathExpression
【发布时间】:2023-03-25 22:38:01
【问题描述】:

我是 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');

我得到错误:无效的路径表达式。必须是 StateFieldPathExpression。

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

【问题讨论】:

    标签: php forms symfony doctrine


    【解决方案1】:
    '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!)。 所以你需要手动加入实体并按其字段排序!

    【讨论】:

    • 是的,但是在这种情况下按 fk 排序可以,但显然我不能。只是觉得很奇怪,它会让我在 where 子句中使用它而不用 join,但不能在 sort 中使用。不过谢谢,会试试的。
    猜你喜欢
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    相关资源
    最近更新 更多