0

我有以下代码给出以下异常,我不知道为什么,任何帮助将不胜感激。

无效的参数号:绑定变量的数量与标记的数量不匹配

if (!empty($ids)) {
            $queryIds = implode(",", $ids);
            $query = $em->createQueryBuilder()
                        ->from('MainClientBundle:Posts','p')
                        ->select('p')
                        ->where('p.id >= :rand')
                        ->where('p.id NOT IN (:ids)')
                        ->orderBy('p.id','ASC')
                        ->setParameter('rand', rand(1, $max))
                        ->setParameter('ids', $queryIds)
                        ->setMaxResults(1);
        } else {
            $query = $em->createQueryBuilder()
                ->from('MainClientBundle:Posts','p')
                ->select('p')
                ->where('p.id >= :rand')
                ->orderBy('p.id','ASC')
                ->setParameter('rand', rand(1, $max))
                ->setMaxResults(1);
        }
        try {
            if($options['videos'] == "off"){
                $query->where("p.type <> :type")->setParameter("type",1);
            }
            if($options['sfw'] == "on"){
                $query->where("p.safeForWork <> :sfw")->setParameter("sfw",0);
            }
            $post = $query->getQuery()->getSingleResult();
        } catch (\Doctrine\Orm\NoResultException $e) {
            $post = null;
        }
4

2 回答 2

1

Your first query is the problem. Your query should look like this

if (!empty($ids)) {
    $queryIds = implode(",", $ids);
    $query = $em->createQueryBuilder()
                ->from('MainClientBundle:Posts','p')
                ->select('p')
                ->where('p.id >= :rand')
                ->andWhere('p.id NOT IN (:ids)')
                ->orderBy('p.id','ASC')
                ->setParameter('rand', rand(1, $max))
                ->setParameter('ids', $queryIds)
                ->setMaxResults(1);
} else {
    $query = $em->createQueryBuilder()
        ->from('MainClientBundle:Posts','p')
        ->select('p')
        ->where('p.id >= :rand')
        ->orderBy('p.id','ASC')
        ->setParameter('rand', rand(1, $max))
        ->setMaxResults(1);
}
try {
    if($options['videos'] == "off"){
        $query->where("p.type <> :type")->setParameter("type",1);
    }
    if($options['sfw'] == "on"){
        $query->where("p.safeForWork <> :sfw")->setParameter("sfw",0);
    }
    $post = $query->getQuery()->getSingleResult();
} catch (\Doctrine\Orm\NoResultException $e) {
    $post = null;
}

Note that I changed your second where to andWhere. When you use two where() in the same query builder, the first gets overwritten by the second.

于 2013-01-27T21:22:07.340 回答
0

如评论中所述,问题在于->where您的查询构建器中的双重使用。如果要组合多个 where 子句,则需要使用->andWhereor/and ->orWhere

此外,我将您的结果更改为->getOneOrNullResult(),这样您就不需要使用您用来捕获教义异常的整个 try/catch 块。

if (!empty($ids)) {
    $queryIds = implode(",", $ids);
    $query = $em->createQueryBuilder()
        ->from('MainClientBundle:Posts','p')
        ->select('p')
        ->where('p.id >= :rand')
        ->where('p.id NOT IN (:ids)')
        ->orderBy('p.id','ASC')
        ->setParameter('rand', rand(1, $max))
        ->setParameter('ids', $queryIds)
        ->setMaxResults(1);
} else {
    $query = $em->createQueryBuilder()
        ->from('MainClientBundle:Posts','p')
        ->select('p')
        ->where('p.id >= :rand')
        ->orderBy('p.id','ASC')
        ->setParameter('rand', rand(1, $max))
        ->setMaxResults(1);
}

if($options['videos'] == "off"){
    $query->where("p.type <> :type")->setParameter("type",1);
}
if($options['sfw'] == "on"){
    $query->where("p.safeForWork <> :sfw")->setParameter("sfw",0);
}

$post = $query->getQuery()->getOneOrNullResult();
于 2013-01-28T14:24:52.680 回答