-1

I am using cakephp v3.x and mysql.

I would like to see the SQL query of a controller. There is no error with this controller and it runs successfully.

public function appGetResult()
{
    $this->autoRender = false;
    $start_date = $this->request->query['start_date'];
    $end_date = $this->request->query['end_date']; 

    $results = $this->MonthlyReports->getResult($start_date, $end_date);

    echo json_encode($results);
} 

How to modify the controller code to display the SQL query used in generating the results? This is not for actual production use. More for examination.

1
  • May I ask why the negative vote? Please explain so that I can improve on my future questions. THanks. Commented Nov 10, 2015 at 14:14

3 Answers 3

2

You can turn on query logging on CakePHP3: http://book.cakephp.org/3.0/en/orm/database-basics.html#query-logging

If you're interested in doing more debugging and such, DebugKit does an amazing job.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. I am still somewhat at a loss. How does one get the $conn in $conn->logQueries(true);? Where do I run this code?
This should do it: $conn = ConnectionManager::get('connection_name');. Insert your connection_name from the config.
In which file should I run this line $conn = ConnectionManager::get('connection_name');? In the controller?
Yes. I would run it just above the query you want to log, before the logQueries(true).
Don't forget to add use Cake\Datasource\ConnectionManager; at the top of your controller.
|
1

I figured out an indirect way for an answer to my own question. This is an indirect way to see the MySQL query statements.

public function appGetResult()
{
    //$this->autoRender = false;
    $start_date = $this->request->query['start_date'];
    $end_date = $this->request->query['end_date']; 

    $results = $this->MonthlyReports->getResult($start_date, $end_date);

    echo json_encode($results);
} 

By commenting away $this->autoRender = false; and not having a view .ctp, the php page stops with an error. The DebugKit panel will be shown on the erroneous webpage. From the DebugKit panel, one will be able to see the SQL statements.

1 Comment

Cool. how silly way :D. cakephp 3.x it self has it's on debugging tool (called DebigKit). I fyo have it enabled then you would have the debugKit toolbar at the footer of the page. Click "Sql Log" button it shows you all the queries that runs on that time.
0

You can use getLog() or showLog() function to do it

var_dump($this->MonthlyReports->getDataSource()->getLog(false, false));
var_dump($this->MonthlyReports->getDataSource()->showLog());

1 Comment

I think this answer is about cakephp 2.x and the quiestion is about cakephp 3.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.