Doctrine andWhere 或Where 似乎没有绑定多个参数
我有一个这样的查询:
$name = "field1";
$name2 = "field2";
$value = "searchTerm";
$query->select('*')->
from("TableName")->
where($name . " = ?", array($value))->
andWhere($name2 . " = ?", array($value));
我很惊讶地发现,当执行该查询时,会生成 MS SQL 错误 102(语法错误),因为发送到 sql server 的查询如下所示:
SELECT * FROM TableName WHERE field1 = 'searchTerm' AND field2 =?
在添加到查询中的每个附加条件中,问号都是按字面意思理解的! :o
也许我做错了什么,有人可以在这里纠正我。
I have a query like this:
$name = "field1";
$name2 = "field2";
$value = "searchTerm";
$query->select('*')->
from("TableName")->
where($name . " = ?", array($value))->
andWhere($name2 . " = ?", array($value));
I was suprised to see that when this executes the query generates MS SQL Error 102 (syntax error) because the query sent to sql server looks like this:
SELECT * FROM TableName WHERE field1 = 'searchTerm' AND field2 = ?
The question mark was taken literally in each additional condition added to the query! :o
Perhaps I am doing something wrong and someone can set me straight here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇!我找到了答案!
无论出于何种原因,学说不使用 PDO 的 SQL Server 准备好的语句功能。 Connection/Mssql.php 替换查询字符串中的任何参数并将空数组传递给 Connection::execute。这很好:
修复很简单:
在 Connection/Mssql.php 的第 314 行中,将下面的执行函数更改
为:
您可以轻松地删除被覆盖的函数。
不要忘记对同一代码文件中的执行函数下方出现的 exec 进行相同的更改。
另外,您可能希望在将其与 2005 年之前的 SQL Server 版本一起使用之前对此进行测试,因为我尚未在该版本上测试过此解决方案。
Whew! I found the answer!
For whatever reason doctrine does not use PDO's prepared statement functionality for SQL server. Connection/Mssql.php substitutes any parameters within the query string and passes an empty array to Connection::execute. This would be fine:
The fix is easy:
in Connection/Mssql.php on line 314 change the execute function below:
to:
You could just as easily remove the overridden function.
Don't forget to make the same changes to exec which appears just below the execute function in the same code file.
Also, you may wish to test this before using it with versions of SQL server earlier than 2005 as I have not tested this solution with that version.