How to set a profiler class to Zend-DB

zend-db

#1

How can it set a profiler class for https://docs.zendframework.com/zend-db/ component ?

Thanks.


#2

zend-db contains a Profiler class. Basic usage:

$profiler = new Zend\Db\Adapter\Profiler\Profiler();
$adapter  = new Zend\Db\Adapter\Adapter(
    [
        // Driver parameters
    ],
    null, // Platform
    null, // Result prototype
    $profiler
);

$resultSet = $adapter->query(
    'SELECT * FROM album',
    Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
);

var_dump($profiler->getLastProfile());
var_dump($profiler->getProfiles());

The adapter can also create a profiler for you:

$adapter  = new Zend\Db\Adapter\Adapter(
    [
        // Driver parameters
       'profiler' => true,
    ]
);

$resultSet = $adapter->query(
    'SELECT * FROM album',
    Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
);

var_dump($adapter->getProfiler()->getLastProfile());
var_dump($adapter->getProfiler()->getProfiles());

And with a table gateway:

$tableGateway = new Zend\Db\TableGateway\TableGateway('album', $adapter // the profiler is set);
$resultSet    =  $tableGateway->select();

if ($tableGateway->getAdapter() instanceof \Zend\Db\Adapter\Adapter) {
    var_dump($tableGateway->getAdapter()->getProfiler()->getProfiles());
}