警告:缺少QueryBuilder :: setParameter()的参数2

时间:2013-11-19 09:02:15

标签: symfony doctrine-orm

我想要获得一个月的事件,Symfony2会显示此错误:

  

警告:缺少Doctrine \ ORM \ QueryBuilder :: setParameter()的参数2,在 D:\ Works \ Development \ www \ intranet \ src \ Intranet \ IntranetBundle \ Entity中调用第22行上的\ EventRepository.php 并在 D:\ Works \ Development \ www \ intranet \ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ QueryBuilder.php 第356行中定义< / p>

的indexAction

$date_to   = $year . "-" . $m . "-01";
$date_from = $year . "-" . $m . "-" . $nDay;

$events = $this->getDoctrine()
               ->getManager()
               ->getRepository('IntranetIntranetBundle:Event')
               ->getEventsOfTheMonth($date_to, $date_from);

EventRepository

class EventRepository extends EntityRepository
{
    public function getEventsOfTheMonth($date_from, $date_to) {

        $q = $this->createQueryBuilder('e')
                  ->where('e.date BETWEEN :date_from AND :date_to')
                  ->setParameter(array(
                      'date_from' => $date_from,
                      'date_to'   => $date_to
                    ))
                  ->orderBy('e.date', 'ASC');

        return $q->getQuery()->getResult();
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:4)

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/QueryBuilder.php#L515

setParameter($key, $value, $type)setParameters($parameters)的情况下不期望数组。

@param string|integer $key

使用以下内容:

// ...
->setParameter('date_from', $date_from)
->setParameter('date_to', $date_to)

......或......

->setParameters( array(/* ... */))

......或......

->setParameters(new ArrayCollection(array(
      new Parameter('date_from', $date_from),
      new Parameter('date_to', $date_to),
 )))

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/QueryBuilder.php#L558

答案 1 :(得分:0)

您可能正在寻找需要数组的QueryBuilder::setParameters(array $parameters)方法。