Profiling queries with Zend
July 20th, 2010 by Richard
Zend is a powerful framework that offers a LOT of functionality. I’m going to take this moment to talk about using Firebug’s extension FirePHP, which will allow Zend to show you messages about what your application is doing. Now there are a lot of tutorials available which pretty much all say the same thing, which for me did not work. I’m one of those people who can be classified as a fringe case. For example, I have my database information in the /applications/configs/applications.ini file, not in the bootstrap.
[cc]
[production]
…rest of settings…
resources.db.adapter = “pdo_mysql”
resources.db.params.host = “localhost”
resources.db.params.username = “root”
resources.db.params.password = “password”
resources.db.params.dbname = “db-name”
[/cc]
This means that the tutorials won’t work for me because I can’t set $db (see the link above to find out what I’m talking about).
After a lot of head banging a colleague pointed me in the right direction and we finally got this working right:
The following function needs to be placed in the bootstrap file.
[cc]
protected function _initDebug()
{
$logger = new Zend_Log();
$writer = new Zend_Log_Writer_Firebug();
$logger->addWriter($writer);
$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
Zend_Registry::setInstance($registry);
$registry->logger = $logger;
}
[/cc]
This is telling Zend to create a Logger, let it write to Firebug, and register it so that it can be used anywhere in the application. Nice and simple. Now we need to turn it on. In the applications.ini file place the following code:
[cc]
[development : production]
…rest of settings…
resources.db.params.profiler.enabled = true
resources.db.params.profiler.class= Zend_Db_Profiler_Firebug
[/cc]
If you didn’t know, the square brackets indicate the application environment. This allows you to use different settings during development than when the site is in production (live), for example you can use a different set of database details.
Finally, in FireBug, make sure that the ‘Net’ console is ‘Enabled’.
That’s it. Quick and painless. Now whenever Zend executes a query, it will output the query to FireBug / FirePHP.
You can also use FirePHP to display information, as an alternative to putting echo, print_r and dump_var into your code to debug stuff.
[cc]
Zend_Registry::get(‘logger’)->log($message, Zend_Log::INFO);
[/cc]
$message can be a string or an array, and there are three log-types available: INFO, ERR, and WARN (also called CRIT), allowing you to prioritise all logs in FirePHP and view the debugging information based on the log-type.
Link to us
If you want to link to this blog, copy and paste the following HTML code to your website.










