0

suppose i have a query in cakephp 3

$post = $this->Posts->get($id, [
        'contain' => ['Postmeta']
    ]);

I want to print it like plain mysql query for example

SELECT * FROM posts....

can anyone please explain this how can i achieve this. Please answer it only in cakephp 3 environment. Is it possible to print it in controller as normal mysql query ? please do not mention queries.log file solution. beacause it is time taking to open file and see the query after every executed query in queries.log file in that is look in cakephp style Thanks

Aman Attari
  • 181
  • 3
  • 12
  • Possible duplicate of [How to get last run query in CakePHP 3.2?](http://stackoverflow.com/questions/35291033/how-to-get-last-run-query-in-cakephp-3-2) – bill Jan 05 '17 at 19:26
  • @bill I saw that solution but queries.log file show query in cakephp style. I want to print it like plain mysql query like SELECT * FROM posts. and also i want to see print it on my controller file. Is it possible ? – Aman Attari Jan 05 '17 at 19:38
  • There are couple of ways to print sql just before it get execution. I think you need it after execution right ? – Manohar Khadka Jan 06 '17 at 04:34
  • yes @ManoharKhadka – Aman Attari Jan 06 '17 at 19:57

3 Answers3

0

In Controller, We need to write two lines after query code as follows

$post = $this->Posts->get($id, [
   'contain' => ['Postmeta']
]);
echo "<pre>";
print_r(debug($post));die; 

It will show all result along with sql query syntax.

Here we are using debug for show result along with sql query.

-2

Query:

$post = $this->Posts->get($id, [
    'contain' => ['Postmeta']
]);

For Print/Get SQL Statement of above query you can use debug() function as follow in controller:

$post = $this->Posts->get($id, [
    'contain' => ['Postmeta']
]);
debug($post);
Haresh Kumar
  • 539
  • 6
  • 15
-2

Just write:

die(print_r($post));
nexequ
  • 321
  • 5
  • 16