dsyak22488 2011-09-28 16:33
浏览 16
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • dongwu9170 2011-09-30 21:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题