0

我正在构建一个 Symfony 1.4.20 应用程序,并编写了以下代码:

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

在 Symfony 日志中查看结果查询如下:

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'))

我在 phpMyAdmin 中运行查询,一切都很好,这意味着查询没有任何问题,但在 Symfony with Doctrine 中我收到此错误:

SQLSTATE[HY093]:无效的参数号:绑定变量的数量与标记的数量不匹配

为什么?怎么了?

4

1 回答 1

3

我认为这行中的问题是:

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

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

它应该是:

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

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

更新:编辑答案

于 2013-06-08T18:12:52.967 回答