PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Introduction

Zend_Db_Profiler can be enabled to allow profiling of queries. Profiles include the queries processed by the adapter as well as elapsed time to run the queries, allowing inspection of the queries that have been performed without needing to add extra debugging code to classes. Advanced usage also allows the developer to filter which queries are profiled.

Enable the profiler by either passing a directive to the adapter constructor, or by asking the adapter to enable it later.

<?php
$params 
= array(
    
'host'     => '127.0.0.1',
    
'username' => 'webuser',
    
'password' => 'xxxxxxxx',
    
'dbname'   => 'test'
    'profiler' 
=> true  // turn on profiler
                        // set to false to disable (disabled by default)
);

$db Zend_Db::factory('PDO_MYSQL'$params);

// turn off profiler:
$db->getProfiler()->setEnabled(false);

// turn on profiler:
$db->getProfiler()->setEnabled(true);

The value of the 'profiler' option is flexible. It is interpreted differently depending on its type. Most often, you should use a simple boolean value, but other types enable you to customize the profiler behavior.

A boolean argument sets the profiler to enabled if it is a TRUE value, or disabled if FALSE. The profiler class is the adapter's default profiler class, Zend_Db_Profiler.

<?php
$params
['profiler'] = true;
$db Zend_Db::factory('PDO_MYSQL'$params);

An instance of a profiler object makes the adapter use that object. The object type must be Zend_Db_Profiler or a subclass thereof. Enabling the profiler is done separately.

<?php
$profiler 
MyProject_Db_Profiler();
$profiler->setEnabled(true);
$params['profiler'] = $profiler;
$db Zend_Db::factory('PDO_MYSQL'$params);

The argument can be an associative array containing any or all of the keys 'enabled', 'instance', and 'class'. The 'enabled' and 'instance' keys correspond to the boolean and instance types documented above. The 'class' key is used to name a class to use for a custom profiler. The class must be Zend_Db_Profiler or a subclass. The class is instantiated with no constructor arguments. The 'class' option is ignored when the 'instance' option is supplied.

<?php
$params
['profiler'] = array(
    
'enabled' => true,
    
'class'   => 'MyProject_Db_Profiler'
);
$db Zend_Db::factory('PDO_MYSQL'$params);

Finally, the argument can be an object of type Zend_Config containing properties, which are treated as the array keys described above. For example, a file "config.ini" might contain the following data:

[main]
db.profiler.class   = "MyProject_Db_Profiler"
db.profiler.enabled = true

This configuration can be applied by the following PHP code:

<?php
$config 
= new Zend_Config_Ini('config.ini''main');
$params['profiler'] = $config->db->profiler;
$db Zend_Db::factory('PDO_MYSQL'$params);

The 'instance' property may be used as in the following:

<?php
$profiler 
= new MyProject_Db_Profiler();
$profiler->setEnabled(true);
$configData = array(
    
'instance' => $profiler
    
);
$config = new Zend_Config($configData);
$params['profiler'] = $config;
$db Zend_Db::factory('PDO_MYSQL'$params);

Zend Framework