Yii CDBCommand getText 显示 SQL 中的所有变量 [英] Yii CDBCommand getText to show all variables in the SQL

查看:18
本文介绍了Yii CDBCommand getText 显示 SQL 中的所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yii 的 Yii::app()->db->createCommand() 来构建 SQL 查询.为了查看 Yii 生成的 SQL 代码,我使用了 CDBCommand 的 getText() 方法.问题是,当我对包含参数的 SQL 代码使用 getText() 方法时,例如:

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();

getText() 方法返回以下 SQL:

the getText() method returns the following SQL:

select name from package where id=:id

代替:

select name from package where id=5

这对于简单的查询来说很好,但是对于具有大量参数的更复杂的查询,将每个参数复制/粘贴到 SQL 代码中进行测试是一件很痛苦的事情.

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.

有没有什么方法可以使用 getText() 或 Yii 中的其他方法直接在 SQL 中显示参数?

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

干杯!

推荐答案

看起来你在关注 Yii 正在准备的 PDOStatement:

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).

这篇关于Yii CDBCommand getText 显示 SQL 中的所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆