Doctrine(参数编号无效:绑定变量的数量与标记的数量不匹配)
Doctrine (Invalid parameter number: number of bound variables does not match number of tokens)
我无法使用 Doctrine 调试错误。我有以下代码,旨在获取特定所有者的所有帐户(表 "owner" 和 "account" 之间存在一对多映射关系):
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT a FROM AcmeUserBundle:Account a WHERE a.owner=11');
$accounts = $query->getResult();
此代码完美运行并获取 ID 为 11 的所有者的所有帐户。现在,作为我调试的一部分,我刚刚更改了所有者 ID 的确定,如下所示:
$id = 11;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT a FROM AcmeUserBundle:Account a WHERE a.owner=:id');
$accounts = $query->getResult();
我收到以下错误消息 "Invalid parameter number: number of bound variables does not match number of tokens"。任何人都可以给我一些关于 "bound variable" 和 "tokens" 的概念的高级解释以及让我知道我应该使用什么代码如果我想在我的 [=24= 的其他地方定义 "id" ] 控制器?谢谢
您需要将参数 $id 设置为查询,否则构建器无法知道您的 $id :
$query->setParameter('id', $id);
正如错误所说,您设置的参数绑定变量的数量与':id'等标记的数量不匹配。
我无法使用 Doctrine 调试错误。我有以下代码,旨在获取特定所有者的所有帐户(表 "owner" 和 "account" 之间存在一对多映射关系):
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT a FROM AcmeUserBundle:Account a WHERE a.owner=11');
$accounts = $query->getResult();
此代码完美运行并获取 ID 为 11 的所有者的所有帐户。现在,作为我调试的一部分,我刚刚更改了所有者 ID 的确定,如下所示:
$id = 11;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT a FROM AcmeUserBundle:Account a WHERE a.owner=:id');
$accounts = $query->getResult();
我收到以下错误消息 "Invalid parameter number: number of bound variables does not match number of tokens"。任何人都可以给我一些关于 "bound variable" 和 "tokens" 的概念的高级解释以及让我知道我应该使用什么代码如果我想在我的 [=24= 的其他地方定义 "id" ] 控制器?谢谢
您需要将参数 $id 设置为查询,否则构建器无法知道您的 $id :
$query->setParameter('id', $id);
正如错误所说,您设置的参数绑定变量的数量与':id'等标记的数量不匹配。