参数无效:查询中未定义标记1

时间:2017-05-09 19:45:59

标签: symfony doctrine dql

这是我根据JSON ajax查询过滤宠物的代码 我正在接收图像错误

enter image description here

public function filter($object, $active=true){

        $query = $this->createQueryBuilder('p');
        $query->innerjoin('TadpetProfessionalBundle:ProPet', 'pp', 'WITH', 'pp.professionalId = p.id');
        $query->innerjoin('TadpetManagerBundle:Pet', 'ppp', 'WITH', 'ppp.id = pp.petId');

        $query->where('p.isActive = :active')
        ->setParameter('active', $active);

        if(!empty($object->pets)){
            $qString = "";
            for($i=1; $i<=sizeof($object->pets); $i++){
                if($i == 1){
                    $qString .= "ppp.name = :petname".$i;
                }else{
                    $qString .= " OR ppp.name = :petname".$i;
                }
            }
            $query->andWhere($qString);

            $query->setParameter('petname'+1,$object->pets[0]);
            $query->setParameter('petname'+2,$object->pets[1]);
            $query->setParameter('petname'+3,$object->pets[2]);
        }
        return $query->getQuery()->getResult();

}

请帮帮我

1 个答案:

答案 0 :(得分:3)

在这些方面:

$query->setParameter('petname'+1,$object->pets[0]);
$query->setParameter('petname'+2,$object->pets[1]);
$query->setParameter('petname'+3,$object->pets[2]);

您正在为数字添加'petname',但您应该将它们连接起来:

$query->setParameter('petname'.1,$object->pets[0]);
$query->setParameter('petname'.2,$object->pets[1]);
$query->setParameter('petname'.3,$object->pets[2]);

此外,您可以使用循环:

for($i=1; $i<=sizeof($object->pets); $i++){
    $query->setParameter('petname'.$i,$object->pets[$i-1]);
}