dongzhuxun5136 2012-02-10 10:53
浏览 23
已采纳

Yii CDBCommand getText显示SQL中的所有变量

I am using Yii's Yii::app()->db->createCommand() to build an SQL query. In order to view the SQL code that Yii generates, I am using the getText() method of CDBCommand. Problem is, when I use the getText() method on SQL code that contain parameters, for example:

Yii::app()->db->createCommand()
           ->select("name")
           ->from('package')
           ->where('id=:id', array(':id'=>5))
           ->queryRow();

the getText() method returns the following SQL:

select name from package where id=:id

instead of:

select name from package where id=5

This is fine for simple queries, but for more complex queries with lots of parameters, it is quite a pain to copy/paste each parameter into the SQL code to test it.

Is there any way to display the parameters directly inside the SQL using getText() or some other method in Yii?

Cheers!

  • 写回答

4条回答 默认 最新

  • douhuantui6259 2012-02-20 20:03
    关注

    Looks like you're after the PDOStatement that Yii is preparing:

    $cmd = Yii::app()->createCommand();
    
    $cmd->select("name")
           ->from('package')
           ->where('id=:id', array(':id'=>5))
           ->queryRow();
    
    // returns a PDO Statement - http://php.net/manual/en/class.pdostatement.php
    Yii::log($cmd->getPdoStatement()->queryString);
    

    Does that work for you? Seems like it should (code untested).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决