02/09/08

Zend framework: Logging Database Queries to FireBug

Logging database queries to FireBug is sinfully simple with the new component Zend_Db_Profiler_Firebug in ZF 1.6, now available, you can download it here Zend Framework Download Page.

Requirements:

  1. Firefox Browser ideally version 3 but version 2 is also supported.
  2. Firebug Firefox Extension.
  3. FirePHP Firefox Extension.

More information on requirements at the Zend Framework Documentation -  Profiling with Firebug

Let’s look at some examples.

< ?php
// Instatiate the database
$db = Zend_Db::factory('Pdo_Mysql',
    array(
        'host' => 'localhost',
        'dbname' => 'zf_feature_testing',
        'username' => 'user123',
        'password' => 'pass123'
    )
);
 
// Instantiate the profiler in your bootstrap file 
$profiler = new Zend_Db_Profiler_Firebug('All Database Queries:');
// Enable it
$profiler->setEnabled(true);
// Attach the profiler to your db adapter 
$db->setProfiler($profiler);
 
// Run your queries
$result1 = $db->fetchAll('SELECT * FROM zf_test');
$result2 = $db->fetchAll('SELECT * FROM zf_test where id = ?', 3);

Alternatively you can add the profiler parameters to the Zend_Db factory.

< ?php
// Instatiate the database, passing in the profiler parameters.
$db = Zend_Db::factory('Pdo_Mysql',
    array(
        'host' => 'localhost',
        'dbname' => 'zf_feature_testing',
        'username' => 'user123',
        'password' => 'pass123',
        'profiler' => array(
            'enabled' => true,
            'class' => 'Zend_Db_Profiler_Firebug'
        )
    )
);

Or from an .ini file using Zend_Config_Ini

$config = new Zend_Config_Ini('../application/config.ini', 'development');
$db = Zend_Db::factory($config->database);

config.ini

[development]
database.adapter                    = pdo_mysql
database.params.host                = localhost
database.params.username            = user123
database.params.password            = pass123
database.params.dbname              = zf_feature_testing
database.params.profiler.enabled    = true
database.params.profiler.class      = Zend_Db_Profiler_Firebug

Show me my profiling data?

Open FireBug, you will see a link under console.

Profiling with FireBug

Click it open and it will list all the queries that were run.

Profiling with FireBug

Enjoy!

Comments | RSS

#1
Christoph Dorn - All around PHP » Blog Archive » FirePHP and Zend Framework 1.6 said

[...] For a more detailed tutorial on how to use Zend_Db_Proflier_Firebug see here. [...]

#2
Christoph Dorn said

Thanks for the great tutorial!

#3
SunWuKung said

Hello Christoph!
Profiling db queries with FirePHP is like magic – thanks for showing this.
Is there a way to also display all db errors in a similar way? It would be very good to be able to see the db request that caused the error and not just a part of it like stack trace does.

Thanks.
SWK

#5
Дебаг в Zend Framework с использованием FirePHP said

[...] Zend framework: Logging Database Queries to FireBug [...]

#7
letsnurture said

hi,

Its an excellent news.

I have been working with zend framework based application.

So today, i thought let me search in google for “Zend framework firefox addons” with hope that i might get something interesting that would help me while developing the code.

So i found this nice components named “FirePHP”.

I will configure it in my local machine.

Thanks to the creator.

Grate job!!

Letsnurture

#8
Yodous said

This dump profiler into Firebug is realy cool. I use it in the development phase. Thanks to that, I was able to find sites, that generate a lots of queries.

Leave a Reply