Daan
Daan

Reputation: 7925

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

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

Upvotes: 0

Views: 480

Answers (1)

Daan
Daan

Reputation: 7925

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'];

Upvotes: 1

Related Questions