如何在控制器中看到 CakePHP 的 SQL 转储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3647065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:25:40  来源:igfitidea点击:

How can I see CakePHP's SQL dump in the controller?

sqlcakephp

提问by Justin

Is there a way that one can cause CakePHP to dump its SQL log on demand? I'd like to execute code up until a point in my controller and see what SQL has been run.

有没有一种方法可以让 CakePHP 按需转储其 SQL 日志?我想执行代码直到我的控制器中的某个点并查看已运行的 SQL。

回答by deceze

Try this:

尝试这个:

$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);

http://api.cakephp.org/2.3/class-Model.html#_getDataSource

http://api.cakephp.org/2.3/class-Model.html#_getDataSource

You will have to do this for each datasource if you have more than one though.

如果您有多个数据源,则必须为每个数据源执行此操作。

回答by Urdesh Kumar

There are four ways to show queries:

有四种显示查询的方式:

  1. This will show the last query executed of user model:

    debug($this->User->lastQuery());  
    
  2. This will show all executed query of user model:

    $log = $this->User->getDataSource()->getLog(false, false);       
    debug($log);
    
  3. This will show a log of all queries:

    $db =& ConnectionManager::getDataSource('default');
    $db->showLog();
    
  4. If you want to show all queries log all over the application you can use in view/element/filename.ctp.

    <?php echo $this->element('sql_dump'); ?>
    
  1. 这将显示用户模型执行的最后一个查询:

    debug($this->User->lastQuery());  
    
  2. 这将显示用户模型的所有已执行查询:

    $log = $this->User->getDataSource()->getLog(false, false);       
    debug($log);
    
  3. 这将显示所有查询的日志:

    $db =& ConnectionManager::getDataSource('default');
    $db->showLog();
    
  4. 如果您想显示整个应用程序中的所有查询日志,您可以在 view/element/filename.ctp 中使用。

    <?php echo $this->element('sql_dump'); ?>
    

回答by bjudson

If you're using CakePHP 1.3, you can put this in your views to output the SQL:

如果您使用的是 CakePHP 1.3,您可以将它放在您的视图中以输出 SQL:

<?php echo $this->element('sql_dump'); ?>

So you could create a view called 'sql', containing only the line above, and then call this in your controller whenever you want to see it:

因此,您可以创建一个名为“sql”的视图,仅包含上面的行,然后在您想查看它时在控制器中调用它:

$this->render('sql');

(Also remember to set your debug level to at least 2 in app/config/core.php)

(还要记住将调试级别设置为至少 2 in app/config/core.php

Source

来源

回答by Govind Totla

for cakephp 2.0 Write this function in AppModel.php

对于 cakephp 2.0 在 AppModel.php 中写这个函数

function getLastQuery()
{
    $dbo = $this->getDatasource();
    $logs = $dbo->getLog();
    $lastLog = end($logs['log']);
    return $lastLog['query'];
}

To use this in Controller Write : echo $this->YourModelName->getLastQuery();

要在控制器写入中使用它: echo $this->YourModelName->getLastQuery();

回答by Joe Purcell

It is greatly frustrating that CakePHP does not have a $this->Model->lastQuery();. Here are two solutions including a modified version of Handsofaten's:

CakePHP 没有 $this->Model->lastQuery(); 非常令人沮丧。以下是两种解决方案,包括 Handsofaten 的修改版本:

1. Create a Last Query Function

1. 创建最后一个查询函数

To print the last query run, in your /app_model.php file add:

要打印上次运行的查询,请在您的 /app_model.php 文件中添加:

function lastQuery(){
    $dbo = $this->getDatasource();
    $logs = $dbo->_queriesLog;
    // return the first element of the last array (i.e. the last query)
    return current(end($logs));
}

Then to print output you can run:

然后打印输出,您可以运行:

debug($this->lastQuery()); // in model

OR

或者

debug($this->Model->lastQuery()); // in controller

2. Render the SQL View (Not avail within model)

2. 渲染 SQL 视图(在模型内不可用)

To print out all queries run in a given page request, in your controller (or component, etc) run:

要打印给定页面请求中运行的所有查询,请在您的控制器(或组件等)中运行:

$this->render('sql');

It will likely throw a missing view error, but this is better than no access to recent queries!

它可能会抛出一个缺少视图的错误,但这总比不能访问最近的查询要好!

(As Handsofaten said, there is the /elements/sql_dump.ctp in cake/libs/view/elements/, but I was able to do the above without creating the sql.ctp view. Can anyone explain that?)

(正如 Handsofaten 所说,cake/libs/view/elements/ 中有 /elements/sql_dump.ctp,但我能够在不创建 sql.ctp 视图的情况下执行上述操作。谁能解释一下?)

回答by rtconner

In CakePHP 1.2 ..

在 CakePHP 1.2 中...

$db =& ConnectionManager::getDataSource('default');
$db->showLog();

回答by Pipo

What worked finally for me and also compatible with 2.0 is to add in my layout (or in model)

最终对我有用并且与 2.0 兼容的是添加到我的布局(或模型中)

<?php echo $this->element('sql_dump');?>

It is also depending on debug variable setted into Config/core.php

它也取决于设置到 Config/core.php 中的调试变量

回答by Rito

Plugin DebugKit for cake will do the job as well. https://github.com/cakephp/debug_kit

用于蛋糕的插件 DebugKit 也可以完成这项工作。https://github.com/cakephp/debug_kit