Doctrine andWhere 或Where 似乎没有绑定多个参数

发布于 2024-12-07 04:39:24 字数 489 浏览 0 评论 0原文

我有一个这样的查询:

 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-12-14 04:39:24

哇!我找到了答案!

无论出于何种原因,学说不使用 PDO 的 SQL Server 准备好的语句功能。 Connection/Mssql.php 替换查询字符串中的任何参数并将空数组传递给 Connection::execute。这很好:

  1. 如果准备好的语句不起作用(也许在第一次编写连接驱动程序时它们在 sql 2000 中不起作用?):/
  2. 如果替换实际上成功替换了多个值! :o

修复很简单:

在 Connection/Mssql.php 的第 314 行中,将下面的执行函数更改

 public function execute($query, array $params = array())
 {
      if(! empty($params)) {
           $query = $this->replaceBoundParamsWithInlineValuesInQuery($query, $params);
      }

      return parent::execute($query, array());
 }

为:

 public function execute($query, array $params = array())
 {          
      return parent::execute($query, $params);
 }

您可以轻松地删除被覆盖的函数。

不要忘记对同一代码文件中的执行函数下方出现的 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:

  1. If the prepared statements didn't work (maybe they didn't work in sql 2000 when the connection driver was first written?) :/
  2. If the substitution actually successfully substituted more than one value! :o

The fix is easy:

in Connection/Mssql.php on line 314 change the execute function below:

 public function execute($query, array $params = array())
 {
      if(! empty($params)) {
           $query = $this->replaceBoundParamsWithInlineValuesInQuery($query, $params);
      }

      return parent::execute($query, array());
 }

to:

 public function execute($query, array $params = array())
 {          
      return parent::execute($query, $params);
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文