How to get access to the executed SQL queries in Zend Framework 3?

405 Views Asked by At

How can you get access to all the SQL queries that are being executed by Zend Framework 3?

1

There are 1 best solutions below

0
Daan On

In Zend Framework 3 you can use "profiling" to track the performance of the SQL queries. This can also be used to access the executed SQL queries.

In your database config set the profiler = true options on a certain adapter like this:

<?php
return [
    'db' => [
        'adapters' => [
            'adapter1' => [
                'dsn' => 'mysql:dbname=dbname;host=127.0.0.1',
                'username' => '',
                'password' => '',
                'profiler' => true,
            ],
        ],
    ],
];

Then fetch the adapter (it is a service with corresponding name) and get the driver, get the profiler and the profiles. Profiles is an array containing all sql statements.

$adapter = $application->getServiceManager()->get('adapter1'); 
$profiles = $adapter->getDriver()->getProfiler()->getProfiles();

Every profile is an array with associative key "sql" where the raw query is stored.

$firstSqlQuery = $profiles[0]['sql'];