symfony表单字段上的子查询

xuo3flqw  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(167)

我尝试在symfony中生成子查询以进行填充和表单选择,但收到以下错误消息
[Semantical Error] line 0, col 167 near 'd2 WHERE d2.distributor': Error: 'd2' is already defined.
这是我的密码

-`>add('doctor', EntityType::class, [
                'class' => Doctor::class,
                'query_builder' => function (EntityRepository $er) use ($options) {

                $subquery =  $er->createQueryBuilder('d2')
                    ->select('')
                    ->from('AppBundle\Entity\Distributor', 'd2')
                    ->andWhere('d2.distributor = :distributor_subquery')
                    ->setParameter('distributor_subquery', $options['user']);

                $query =  $er->createQueryBuilder('d');
                $query->select('d')
                    ->where('d.distributor = :distributor')
                    ->setParameter('distributor', $options['user'])
                    ->orWhere($query->expr()->in('d.distributor', $subquery->getDQL()))
                    ->orderBy('d.name', 'ASC')
                    ->addOrderBy('d.surname', 'ASC');

                return $query;
            },`

d2别名定义了两次$er->createQueryBuilder('d2')
以及
->from('AppBundle\Entity\Distributor', 'd2')
但在这两个函数中,该参数都是必需的
我试过
$subquery = $er->createQueryBuilder() ->select('') ->from('AppBundle\Entity\Distributor', 'd2') ->andWhere('d2.distributor = :distributor_subquery')
但它不起作用

rryofs0p

rryofs0p1#

请参见 EntityRepository::createQueryBuilder() 方法的实现:它已经包含了 selectfrom 部分,它从它的实体类中获取这些信息,所以你可以省略调用这些方法,只需要添加一个 where 子句和绑定参数:

$subquery =  $er->createQueryBuilder('d2')
   ->andWhere('d2.distributor = :distributor_subquery')
   ->setParameter('distributor_subquery', $options['user']);

相关问题