cakephp mysql_mysql-CakePHP-获取上一次查询运行

mysql-CakePHP-获取上一次查询运行

我想获取最后一个运行CakePHP的查询。 我无法在core.php中打开调试功能,也无法在本地运行代码。 我需要一种方法来获取最新的sql查询并将其记录到错误日志中,而不会影响实时站点。 该查询失败,但正在运行。

像这样的东西会很棒:

$this->log($this->ModelName->lastQuery);

提前致谢。

asked 2020-07-23T02:47:04Z

11个解决方案

46 votes

对于Cake 2.0,查询日志受到保护,因此可以正常工作

function getLastQuery() {

$dbo = $this->getDatasource();

$logs = $dbo->getLog();

$lastLog = end($logs['log']);

return $lastLog['query'];

}

Matt answered 2020-07-23T02:47:57Z

30 votes

在CakePHP v2.3.2中测试

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

debug($log);

Haktan Suren answered 2020-07-23T02:48:17Z

29 votes

在CakePHP 1.x中,可以在app/app_model.php中访问所需的数据。Cake并未真正为该成员提供getter方法,但是基础语言是PHP,没有什么可以阻止您执行以下操作:

在app/app_model.php中:

function getLastQuery()

{

$dbo = $this->getDatasource();

$logs = $dbo->_queriesLog;

return end($logs);

}

Daniel Wright answered 2020-07-23T02:48:41Z

3 votes

您可以使用此内联。

$dbo = $this->Model->getDatasource();

// store old state

$oldStateFullDebug = $dbo->fullDebug;

// turn fullDebug on

$dbo->fullDebug = true;

// Your code here! eg.

$this->Model->find('all');

// write to logfile

// use print_r with second argument to return a dump of the array

Debugger::log(print_r($dbo->_queriesLog, true));

// restore fullDebug

$dbo->fullDebug = $oldStateFullDebug;

blavla answered 2020-07-23T02:49:03Z

3 votes

Matt和blavia解决方案的组合(当调试不是2时有效):

$dbo = $this->Model->getDatasource();

$oldStateFullDebug = $dbo->fullDebug;

$dbo->fullDebug = true;

// find or whatever...

$this->Model->find("all");

$logs = $dbo->getLog();

$lastLog = end($logs['log']);

CakeLog::write("DBLog", $lastLog['query']);

$dbo->fullDebug = $oldStateFullDebug;

Krunoslav Djakovic answered 2020-07-23T02:49:23Z

3 votes

很简单,您可以使用showLog()函数

var_dump($this->YourModel->getDataSource()->showLog());

Quy Le answered 2020-07-23T02:49:43Z

3 votes

我知道这是一个很晚的答案,但是对于将来需要此功能的人,您始终可以将调试设置限制为IP,例如:

Configure::write('debug', 0);

if($_SERVER["REMOTE_ADDR"] == '192.168.0.100'){

Configure::write('debug', 2); //Enables debugging only for your IP.

}

alinn answered 2020-07-23T02:50:02Z

2 votes

快速浏览一下本书,cakephp api getLog可以打开logTransaction。尽管没有使用它,但我不确定它的性能如何。

否则,您可以尝试使用FirePHP,这是它的指南,

您可能会尝试使用DebugKit,尽管我觉得最麻烦的是我仍然需要debug 2才能使其正常工作。

希望有什么可以帮助您。 :)

David Yell answered 2020-07-23T02:50:36Z

1 votes

您可以使用此:

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

pr($log);die;

ajeet answered 2020-07-23T02:50:56Z

1 votes

在CakePHP中有两种查看查询的方法。

两种方法都必须在app / Config / core.php中添加以下一行

Configure::write('debug', 2);

第一种方法:

debug($this->getLastQuery());

您要获取查询的位置,在上面的行中添加并使用以下代码在同一控制器上调用此函数getLastQuery()

public function getLastQuery() {

$dbo = $this->TModel->getDatasource(); //Here TModel is a model.what table you want to print

$logs = $dbo->getLog();

$lastLog = end($logs['log']);

return $lastLog['query'];

}

第二种方法:

将以下行添加到任何Elements文件中。

krishna ragav answered 2020-07-23T02:51:37Z

0 votes

这会帮到你。

echo '

';

$log = $this->YOUR_MODEL->getDataSource();

print_r($log);

exit;

Captain Sparrow answered 2020-07-23T02:51:57Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值