4

我收到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();

}
4

2 回答 2

2

company 不能是参数,您只需指定一个别名,例如:

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();
}
于 2013-06-27T08:45:51.473 回答
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();
}

已编辑 连接不采用任何对象参数。文档

于 2013-06-27T08:34:23.107 回答