有没有好的方法能获取增删改等操作的SQL语句?

新手提问 · wy1272086709 · 于 6年前 发布 · 3737 次阅读

如题,查询操作获取SQL语句的我已经知道了.

共收到 2 条回复
xjdata#16年前 1 个赞

debug模式下, 日志里有, 要想通过代码获取,还不知道,看看手册什么的。。。

话说是为了确认增删改时的字段操作吗?

@xjdata ,

namespace app\components\db;
use yii\db\Command;
class Connection extends \yii\db\Connection
{
    public static $instance = null;
    
    public function createCommand($sql = null,$params = [])
    {
        if (!self::$instance)
        {
            $command = new $this->commandClass([
                'db' => $this,
                'sql' => $sql,
            ]);
        }
        else
        {
            $command = self::$instance;
            $command->setSql($sql);
        }
        return self::$instance = $command->bindValues($params);
    }
    
    public function getLastSql()
    {
        $instance = self::$instance;
        return $instance->getRawSql();
    }
}

然后更改db.php配置项`php 'class' => 'app\components\db\Connection' ` 然后在具体的SQL操作后,使用\Yii::$app->getDb()->getLastSql(),貌似就可以得到最后操作的SQL了。

有没有其他的影响,就不清楚了哦 :smile:

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册