Simon
Simon

Reputation: 1733

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();

}

Upvotes: 4

Views: 14442

Answers (2)

AlterPHP
AlterPHP

Reputation: 12727

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();
}

Upvotes: 2

Fracsi
Fracsi

Reputation: 2314

You have to pass the exact number of parameters.

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

EDITED The join does not take any object params. Documentation

Upvotes: 1

Related Questions