Search code examples
phpsymfonydoctrine-ormtokendql

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


I am failing to debug an error with Doctrine. I have the following code which aims at fetching all accounts for a specific owner (there is a one-to-many mapping relationship between the tables "owner" and "account"):

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT a FROM AcmeUserBundle:Account a WHERE a.owner=11');
$accounts = $query->getResult();

This code works perfectly and fetch all accounts for the owner whose id is 11. Now as part of my debugging I just changed the determination of the owner's id as follows:

$id = 11;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT a FROM AcmeUserBundle:Account a WHERE a.owner=:id');
$accounts = $query->getResult();

I get the following error message "Invalid parameter number: number of bound variables does not match number of tokens". Could anyone give me some high level explanations on those concepts of "bound variable" and "tokens" along with letting me know what code I should use If I want to define "id" elsewhere in my php controller? Thanks.


Solution

  • You need to set the parameter $id to the query otherwise there is no way the builder will know your $id :

    $query->setParameter('id', $id);
    

    As the error says the numbers of bound variables which are the parameters you set doesnt match the number of tokens which are the ':id' etc..