我有以下查询:

...
$now = new \DateTime('now');
$now->setTime($now->format('H'), 0, 0);

$shopsQueryBuilder = $this->createQueryBuilder('shops')
  ->select('shops.id, shops.fullname, shops.username')
  ->leftJoin('shops.owner', 'owner')
  ->leftJoin('owner.location', 'location')
  ->leftJoin('location.city', 'city')
  ->leftJoin('city.state', 'state')
  ->leftJoin('state.province', 'province')
  ->where('shops.expirydate >= :expiryDate')
  ->setParameter('expiryDate', $now);

if ($provinceId)
{
  $shopsQueryBuilder->where('province = :province')
    ->setParameter('province', $provinceId);
}

if ($stateId)
{
  $shopsQueryBuilder->andWhere('state = :state')
    ->setParameter('state', $stateId);
}

if ($cityId)
{
  $shopsQueryBuilder->andWhere('city = :city')
    ->setParameter('city', $cityId);
}
...


我不知道为什么我总是得到错误:


  绑定变量的数量与查询生成器的令牌数量不匹配


任何想法?

最佳答案

您应该在此处使用andWhere

      if ($provinceId)
      {
          $shopsQueryBuilder->andWhere('province = :province')
                  ->setParameter('province', $provinceId);
      }


否则,您是将where子句替换为shops.expiryDate,而不是添加到其中。并且它仍然具有该字段的参数。

10-06 04:08