Doctrine查询构建器 - 参数太少

问题描述 投票:0回答:1

我做了一个Doctrine查询构建器查询,我有这个错误:

Too few parameters : The query defines 3 parameters but you only bound 2.

我查看了查询并尝试了一些重构,但我无法找到代码错误的地方。

查询:

$query = $this->_em->createQueryBuilder();
$query->select('m')
      ->from(Matiere::class, 'm')
      ->join('m.ues', 'ue')
      ->andWhere($query->expr()->eq('ue.semester', ':semestre'))
      ->setParameter('semestre', $semestre);

if (count($UEs) === 1) {
    $query->andWhere($query->expr()->neq('ue.id', ':ueSpe'))
          ->setParameter('ueSpe', $UEs);
}
else {
    $ueM = $UEs[0];
    $ueP = $UEs[1];
    $query->andWhere($query->expr()->neq('ue.id', ':ueM'))
          ->andWhere($query->expr()->neq('ue.id', ':ueP'))
          ->setParameters(['ueM' => $ueM, 'ueP' => $ueP]);
}
return $query->getQuery()->getResult();
symfony doctrine dql
1个回答
1
投票
$query = $this->_em->createQueryBuilder();
$query->select('m')
      ->from(Matiere::class, 'm')
      ->join('m.ues', 'ue')
      ->andWhere($query->expr()->eq('ue.semester', ':semestre'))
      ->setParameter('semestre', $semestre);

if (count($UEs) === 1) {
    $query->andWhere($query->expr()->neq('ue.id', ':ueSpe'))
          ->setParameter('ueSpe', $UEs);
}
else {
    $parameters[] = $UEs[0];
    $parameters[] = $UEs[1];
    $query->andWhere($qb->expr()->notIn('ue.id', ':parameters'))
          ->setParameter('parameters' => $parameters);
}
return $query->getQuery()->getResult();
© www.soinside.com 2019 - 2024. All rights reserved.