Doctrine Query Builder not registering parameter?
-
25-02-2021 - |
Question
I have the following code which is throwing the exception Invalid parameter number: number of bound variables does not match number of tokens
. Yet when I print the registered parameters, my parameter is showing up.
public function getUnitPriceFor($entityType,$entityID,$qty,$configuration_id)
{
$this->qb = $this->getEntityManager()->createQueryBuilder();
$this->qb ->select($this->_entities[$entityType]['select'])
// for Base this would be ->select(array('t','c','w','g'))
// for the other cases below, like website, it's array('t','w')
->from('AcmeBundle:PriceTier', 't');
switch($entityType) :
case 'base' :
$this->qb ->leftJoin('t.customers','c')
->leftJoin('t.customergroups','g')
->leftJoin('t.websites','w');
break;
case 'website' :
$this->qb ->join('t.websites','w','WITH','w.id = '.$entityID);
break;
case 'custgrp' :
$this->qb ->join('t.customergroups','g','WITH','g.id = '.$entityID);
break;
case 'cust' :
$this->qb ->join('t.customers','t','WITH','t.id = '.$entityID);
break;
endswitch;
$this->qb ->where('t.printconfiguration = :configuration_id');
$this->qb ->setParameter('configuration_id', $configuration_id);
print_r( $this->qb->getParameters() );
$dql = $this->qb->getDQL();
echo"<pre>";
print_r($this->getEntityManager()->createQuery($dql)->getArrayResult());
echo"</pre>";
}
Printing $this->qb->getParameters();
shows me Array ( [configuration_id] => 1 )
, and removing my where and set parameter clauses prevents the exception from occurring. Finally, (and get this), if I remove my where clause but keep the parameter set, no exception occurs. I'm rather confused.
Solution
Apparently $dql = $this->qb->getDQL();
will not pass parameters.
I needed to change
$dql = $this->qb->getDQL();
echo"<pre>";
print_r($this->getEntityManager()->createQuery($dql)->getArrayResult());
echo"</pre>";
to
$query = $this->qb->getQuery();
echo"<pre>";
print_r($query->getArrayResult());
echo"</pre>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow