Home » Php » Advance Php Interview Question » How to debug SQL queries from a controller in cakePHP

How to debug SQL queries from a controller in cakePHP

How to debug SQL queries from a controller in cakePHP

If you want to see the result of last compiled SQL Query before execution. then first you have to required some configuration change. to change the configuration file follow these steps.

Step 1: Set the debug variable to 2 in app/config/core.php

Step 2: After enable debug mode add below line at the end of the layout file where action is render on view file. Now you are able to see all queries that go to the database.

Note : But if you are using an ajax call then page is not reload and sql dump not get refreshed.So we can not debug the sql from this method.

Second Approach : This method is work for both simple and ajax call.to implement this follow these steps.

Step 1: Open your AppModel file and add below method on AppModel class \app\Model\AppModel.php

Step 1: After adding getLastQuery method in AppModel add below line of code in your controller where to debug sql query(which action controller is called).

, , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">