ItGo.me Focus on IT Recommend

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

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

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

up vote 60 down vote favorite 35 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. sql cakephp
  |
  this question asked Sep 5 '10 at 17:47 Justin 304 1 4 3



 |  8 Answers

up vote 104 down vote Try this: $log = $this->Model->getDataSource()->getLog(false, false);

debug($log);

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.
  |
  this answer edited Feb 24 '14 at 12:10 Sadikhasan 12k 8 35 75 answered Sep 6 '10 at 2:04 deceze ♦ 321k 50 416 564 5   I recently had to do this for a legacy 1.2 code base and the method is similar, but not the same: $log = $this->Model->getDataSource()->showLog( false ); debug( $log ). Just in case anyone else runs across this answer via a search. –  Rob Wilkerson Sep 26 '11 at 12:26 5   Still works in 2.0 - thanks! –  jocull Nov 5 '11 at 16:04 6   And you will need to set Configure::write('debug', 2); to get MySQL logging enabled. –  Wirone Sep 26 '12 at 8:25



 |  up vote 28 down vote If you're using CakePHP 1.3, you can put this in your views to output the 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: $this->render('sql');

(Also remember to set your debug level to at least 2 in app/config/core.php) Source
  |
  this answer edited Jul 4 '13 at 10:27 Roney Michael 3,330 4 19 40 answered Sep 5 '10 at 20:28 bjudson 2,708 2 18 43



 |  up vote 25 down vote There are four ways to show queries: This will show the last query executed of user model: debug($this->User->lastQuery());

This will show all executed query of user model: $log = $this->User->getDataSource()->getLog(false, false);

debug($log);

This will show a log of all queries: $db =& ConnectionManager::getDataSource('default');

$db->showLog();

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'); ?>


  |
  this answer edited Jul 24 '15 at 7:18 answered Dec 16 '13 at 17:48 urdesh kumar 647 14 20



 |  up vote 7 down vote for cakephp 2.0 Write this function in AppModel.php function getLastQuery()

{

$dbo = $this->getDatasource();

$logs = $dbo->getL

Recommend:sql - How can I read a .dump file with R

l source dumpname.dump;

og();

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

return $lastLog['query'];

}

To use this in Controller Write : echo $this->YourModelName->getLastQuery();
  |
  this answer answered May 17 '13 at 5:51 Govind Totla 895 9 15



 |  up vote 5 down vote It is greatly frustrating that CakePHP does not have a $this->Model->lastQuery();. Here are two solutions including a modified version of Handsofaten's: 1. Create a Last Query Function To print the last query run, in your /app_model.php file add: 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) 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?)
  |
  this answer answered Oct 12 '11 at 17:57 Joe Purcell 126 2 3      loved this approach –  zanlok Jul 13 '12 at 18:23



 |  up vote 4 down vote In CakePHP 1.2 .. $db =& ConnectionManager::getDataSource('default');

$db->showLog();


  |
  this answer edited Mar 11 '13 at 18:36 answered Jan 3 '13 at 21:39 rtconner 825 7 13



 |  up vote 2 down vote What worked finally for me and also compatible with 2.0 is to add in my layout (or in model) <?php echo $this->element('sql_dump');?>

It is also depending on debug variable setted into Config/core.php
  |
  this answer answered Feb 28 '13 at 8:25 Philippe Gachoud 816 9 14



 |  up vote 0 down vote Plugin DebugKit for cake will do the job as well. https://github.com/cakephp/debug_kit
  |
  this answer answered Nov 25 '15 at 8:20 Rito 3,068 2 25 34

 | 

Recommend:cakephp 3.0 - How can I make multiple controller on single view in CakePHP3

-3.0 share | improve this question edited Nov 17 '15 at 2:28 Unheilig 10k 15 43 84 asked Nov 14 '15 at 16:26 Ko Thet 1 1 1 To me this sounds as if you need to learn abot the basics of associations. book.cakephp.org/3.0/en/orm/associations

up vote 60 down vote favorite 35 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 SQ

------splitte line----------------------------