问题描述
我正在尝试创建一个Doctrine DBAL querybuilder对象并在其中设置一个参数。
(使用postgres db,dbal 2.3.4,doctrine
I'm trying to create a Doctrine DBAL querybuilder object and setting a parameter in it.(using a postgres db, dbal 2.3.4, doctrine
$connection = $this->_em->getConnection();
$qb = $connection->createQueryBuilder();
$qb->select('tbl_user_contract.pkid AS pkid');
$qb->from('tbl_user_contract', 'tbl_user_contract');
$qb->join('tbl_user_contract', 'tbl_user', 'tbl_user', 'tbl_user_contract.fk_user = tbl_user.pkid');
$qb->where('tbl_user.pkid = :userid');
$qb->setParameter(':userid', 10);
当我尝试获取此querybuilder对象的结果时,我收到以下错误:
When I try to get the results of this querybuilder object I get the following error:
SQLSTATE[08P01]: <<Unknown error>>: 7 ERROR: bind message supplies 0 parameters,
but prepared statement "pdo_stmt_00000002" requires 1
当我检查postgres日志时,我看到传递的查询,我注意到它需要一个参数,但是我不会得到参数传入。
When I check the postgres logs, I see the query passing by and I notice that it expects a parameter, but I won't get a parameter passed in.
我试图在where表达式本身设置id(不使用预先准备的语句),这是有效的。但是我真的想让这个工作准备好了。
I tried to set the id in the where expression itself (without using prepared statements), that worked. But I really want to get this working with prepared statements.
任何人都知道如何解决这个问题?
Anyone knows how to solve this?
谢谢提前
推荐答案
我想你只需要从 setparameter
命令
$qb->setParameter('userid', 10);
至少它在Doctrine帮助中起作用
At least it works in Doctrine help http://docs.doctrine-project.org/en/latest/reference/query-builder.html
这篇关于doctrine dbal querybuilder作为准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!