Doctrine DQL参数号无效:绑定变量数与令牌数不匹配

时间:2013-06-27 08:25:35

标签: php symfony doctrine-orm prepared-statement dql

我在此查询中收到错误Invalid parameter number: number of bound variables does not match number of tokens

我真的没有看到问题,有什么想法吗?

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

}

2 个答案:

答案 0 :(得分:2)

公司不能成为参数,您只需指定别名,例如:

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

答案 1 :(得分:1)

您必须传递确切数量的参数。

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 连接不接受任何对象参数。 Documentation