shuba.ivan
shuba.ivan

Reputation: 4061

Symfony Invalid parameter format, : given

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.

Upvotes: 2

Views: 3695

Answers (1)

lxg
lxg

Reputation: 13107

In your foreach loop, you’re looping over the result of an explode operation which yields a numeric array, i.e. $key will always have a numeric value.

Hence, your parameter placeholder is colon + number + string, i.e. :1dateFrom. This is not allowed. Either you reference a string value with a colon + string placeholder (:foo), or you reference a numeric value with a question mark + number value (?1).

Your problem is easy to solve: Simply add any letter between the colon and the number, and you’re good:

->add($qb->expr()->between(
    'q.createdAt',
    ':x'.$key.'dateFrom',
    ':x'.$key.'dateTo'
));

Upvotes: 6

Related Questions