查询生成器给出异常

Query builder gives exception

我正在使用类似

的查询生成器
$querym =$querym->select('DISTINCT(a.id)') ->where ('a.doctorid =:u')
                                                              ->setParameter('u', $u);

$query =$query->select('DISTINCT(d.id),d.name,d.dob,d.mobile')

                                                    ->innerJoin('d.PatientSymptoms','ps')
                                                    ->innerJoin('ps.symptoms', 's')

                                                    ->andWhere('ps.symptoms =:name')
                                                    ->where($query->expr()->In('d.id', $querym))
                                                    ->setParameter('name', $name);

给我
Error: Method Doctrine\Common\Collections\ArrayCollection::__toString() must not throw an exception

能不能换个方式写? 请帮助我。

您可以使用 getDql() 方法(但我认为当子查询有参数时它会失败):

$querym = $querym->select('DISTINCT(a.id)') ->where ('a.doctorid =' . (int) $u);

$query = $query->select('DISTINCT(d.id),d.name,d.dob,d.mobile')->...->where($query->expr()->In('d.id', $querym->getDql()));

您还可以使用 2 个查询(如 Derick F 所说),类似的东西:

$ids = array();
$querym = $querym->select('DISTINCT(a.id)') ->where ('a.doctorid = :u')->setParameter('u',$u);
$as = $querym->getQuery()->getArrayResult();
foreach ($as as $a) $ids[] = $a[1];

$query = $query->select('DISTINCT(d.id),d.name,d.dob,d.mobile')->...->where("d.in (:ids)")->setParameter('ids',$ids);