Search code examples
phpsymfonydoctrine-ormprepared-statementdql

Doctrine DQL Invalid parameter number: number of bound variables does not match number of tokens


I'm getting the error Invalid parameter number: number of bound variables does not match number of tokens on this query.

I really don't see the problem, any ideas?

public function getByPartial($q, Company $company)
{

    $query = $this->createQueryBuilder('u')
        ->join('u.company',':company')
        ->where('u.firstName LIKE :q')
        ->orWhere('u.lastName LIKE :q')
        ->setParameters(array('company' => $company, 'q' => '%'.$q.'%'))
        ->getQuery();
    return $query->getResult();

}

Solution

  • company can't be a parameter, you just have to specify an alias such as :

    public function getByPartial($q, Company $company)
    {
        $query = $this->createQueryBuilder('u')
            ->addSelect('c')
            ->join('u.company','c')
            ->where('u.firstName LIKE :q OR u.lastName LIKE :q')
            ->andWhere('c.id = :companyId')
            ->setParameters(array('companyId' => $company->getId(), 'q' => '%'.$q.'%'))
            ->getQuery();
        return $query->getResult();
    }