I need create multiply search by years. From request I get string like 2017,2018 and then I want get Questions which createdAt, between from start year and end year. I have query builder with part, and I'am not understand why I have this error
if ($paramFetcher->get('years')) {
$orXSearch = $qb->expr()->orX();
$yearData = trim($paramFetcher->get('years'));
foreach (explode(',', $yearData) as $key => $id) {
if (!$id) {
continue;
}
$orXSearch
->add($qb->expr()->between('q.createdAt', ':'.$key.'dateFrom', ':'.$key.'dateTo'));
$date = $this->additionalFunction->validateDateTime($id, 'Y');
$first = clone $date;
$first->setDate($date->format('Y'), 1, 1);
$first->setTime(0, 0, 0);
$last = clone $date;
$last->setDate($date->format('Y'), 12, 31);
$last->setTime(23, 59 , 59);
$qb
->setParameter($key.'dateFrom', $first->format('Y-m-d H:i:s'))
->setParameter($key.'dateTo', $last->format('Y-m-d H:i:s'));
}
$qb->andWhere($orXSearch);
}
error:
symfony Invalid parameter format, : given, but :<name> or ?<num> expected.
if(!$id), add$cParamA=':'.$key.'dateFrom'; $cParamB=':'.$key.'dateTo';and then use$cParamAand$cParamBin your DQL. You would most likely need two other variable for theseParameter. Also, make sure to trim$keyjust in caseSELECT q FROM AppBundle:Questions q WHERE (q.createdAt BETWEEN :0dateFrom AND :0dateTo) OR (q.createdAt BETWEEN :1dateFrom AND :1dateTo) ORDER BY q.createdAt ASCand I checkedparameters, everything is in place$orXSearch ->add($qb->expr()->between( 'q.createdAt', $qb->expr()->literal($first->format('Y-m-d H:i:s')), $qb->expr()->literal($last->format('Y-m-d H:i:s'))) );but I think this is bad way, I want use parameters