我正在构建一个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]:参数号无效:绑定变量数 与令牌数量不匹配
为什么呢?怎么了?
答案 0 :(得分: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'));
更新:编辑回答