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

发布于 2025-01-04 07:03:36 字数 667 浏览 0 评论 0原文

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

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

getText() 方法返回以下 SQL:

select name from package where id=:id

而不是:

select name from package where id=5

这对于简单查询来说很好,但对于具有大量参数的更复杂查询,将每个参数复制/粘贴到 SQL 代码中来测试它是相当痛苦的。

有没有办法使用 getText() 或 Yii 中的其他方法直接在 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

风柔一江水 2025-01-11 07:03:36
$sql = Yii::app()->db->createCommand()
  ->select("name")
  ->from('package')
  ->where('id=:id', array(':id'=>5))
  ->queryRow();

$query=str_replace(
   array_keys($sql->params),
   array_values($sql->params),
   $sql->getText()
);
$sql = Yii::app()->db->createCommand()
  ->select("name")
  ->from('package')
  ->where('id=:id', array(':id'=>5))
  ->queryRow();

$query=str_replace(
   array_keys($sql->params),
   array_values($sql->params),
   $sql->getText()
);
所谓喜欢 2025-01-11 07:03:36

您可以使用 CDbConnetion::enableParamLogging 属性。例如,在 config/main.php: 中

'db' => array (
        'enableParamLogging' => true,

,显示和记录的错误将包含绑定值。

You can use the CDbConnetion::enableParamLogging propery. For instance, in config/main.php:

'db' => array (
        'enableParamLogging' => true,

and the shown and logged error will contain the bound values.

又爬满兰若 2025-01-11 07:03:36

为什么你不尝试如下。我不是专家,只是根据我的知识发布,如果它不适合你的问题,请原谅我......

               $connection=Yii::app()->db;
            $id=5; // you can able to change by "GET" or "POST" methods
    $sql="SELECT name FROM package WHERE id = :id ";
    $command = $connection->createCommand($sql);
    $command->bindParam(":id",$id,PDO::PARAM_STR);
    $dataReader=$command->query();          
    $rows=$dataReader->readAll();
    $namevalue=array();
    foreach($rows as $max)
    {
    $namevalue = $max['name'];
    }   
    echo $namevalue; // which is the value u need

谢谢......

why don you try as below.i am not an expert just posting to my knowledge if its no suitable for your prob pardon me...

               $connection=Yii::app()->db;
            $id=5; // you can able to change by "GET" or "POST" methods
    $sql="SELECT name FROM package WHERE id = :id ";
    $command = $connection->createCommand($sql);
    $command->bindParam(":id",$id,PDO::PARAM_STR);
    $dataReader=$command->query();          
    $rows=$dataReader->readAll();
    $namevalue=array();
    foreach($rows as $max)
    {
    $namevalue = $max['name'];
    }   
    echo $namevalue; // which is the value u need

thank you...

尹雨沫 2025-01-11 07:03:36

看起来您正在寻找 Yii 正在准备的 PDOStatement:

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

这对您有用吗?看起来应该如此(代码未经测试)。

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

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