我有以下查询:
...
$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);
}
...
我想知道为什么我一直收到错误:
绑定变量数与查询构建器的标记数
不匹配
有什么想法吗?
答案 0 :(得分:2)
您应该在这里使用andWhere
:
if ($provinceId)
{
$shopsQueryBuilder->andWhere('province = :province')
->setParameter('province', $provinceId);
}
否则,您要替换where
的{{1}}子句,而不是添加它。它仍然具有该字段的参数。
答案 1 :(得分:0)
也许您需要设置属性所有者并使用andWhere方法,如下所示:
...
$shopsQueryBuilder
->andWhere('city.province = :province')
->setParameter('province', $provinceId);
...