Search code examples
doctrine-ormsymfony1doctrinesymfony-1.4

Doctrine error "Invalid parameter number: number of bound variables does not match number of tokens"


I'm building a Symfony 1.4.20 application and I wrote this code:

public function getListForAdmin() {
    $user = sfContext::getInstance()->getUser();
    $q = $this->createQuery('g');

    if ($user->hasCredential('can_admin_full')) {
        $q->addWhere('g.name IN (?)', array('Administradores Monitor', 'Monitor'));
    } else if ($user->hasCredential('can_admin')) {
        $q->addWhere('g.name IN (?)', array('Monitor'));
    }

    return $q;
}

Seeing in Symfony logs the result query is as follow:

SELECT s.id AS s__id, s.name AS s__name, s.description AS s__description, s.created_at AS s__created_at, s.updated_at AS s__updated_at 
      FROM sf_guard_group s 
      WHERE (s.name IN ('Administradores Monitor'))

I run the query in phpMyAdmin and all is fine meaning query doesn't have any problem but in Symfony with Doctrine I get this error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

Why? What's wrong?


Solution

  • I think that problem in this rows:

     $q->addWhere('g.name IN (?)', array('Administradores Monitor', 'Monitor'));
    

    and

     $q->addWhere('g.name IN (?)', array('Monitor'));
    

    It should be:

    $q->whereIn('g.name', array('Administradores Monitor', 'Monitor'));
    

    and

    $q->whereIn('g.name', array('Monitor'));
    

    UPDATE: edit answer