SQL Output Debugging with Zend Framework

Zend Framework – at first I wasn’t much impressed. Bloated, over-rated, excessive. I was wrong, now I’m loving it 100%!

As I build more and more larger and more complex websites and web-based applications based around the Zend Framework, I attempt to improve the systems I build every time – more efficient coding, quicker database queries, less queries, etc.

One thing that I’ve wanted for a while, though, is the ability to view the time taken to run each query on any particular page, the total time to execute all queries, and the ability to view all queries. By this I mean I don’t want to re-code all my class’s and controllers in order to show the SQL, but instead create a simple bit of code to do it for me. Surely, Zend being its awesome-self should support something like this?

After a little searching around – why yes, yes it does! :)

Here’s my version of a simple output debug-type thing to show total query execution time, each query itself and each queries time, the average execution time & some more. Anyway, on your bootstrap file where you’re defining what database to use, enter this below…

'profiler' => true

For example then…

'host'     => 'localhost',
'username' => 'dbuser',
'password' => 'dbpass',
'dbname'   => 'dbname',
'profiler' => true

Then, further down on your bootstrap file, after $controller->dispatch(); copy/paste this…

// Zend framework debug cool stuff - www.dazecoop.co.uk
$debug = false;
if (($debug == true) || (strstr($_SERVER['REQUEST_URI'],'debug=1'))) {
echo '<div style="font-family:arial;padding:10px;background:#efefef;font-size:11px;position:absolute;top:0px;right:0px;">
<a href="javascript:void(0);" onclick="if ($(\'#debug-panel\').css(\'display\')==\'block\'){$(\'#debug-panel\').slideUp();}else{$(\'#debug-panel\').slideDown();}" style="color:#000;">Debug</a></div>';
echo '<div id="debug-panel" style="width:850px;font-family:arial;position:absolute;top:0px;left:0px;display:none;background:#efefef;font-size:11px;color:#000;padding:20px;">';
$profiler = $db->getProfiler();
$totalTime    = $profiler->getTotalElapsedSecs();
$queryCount   = $profiler->getTotalNumQueries();
$longestTime  = 0;
$longestQuery = null;
foreach ($profiler->getQueryProfiles() as $query) {
if ($query->getElapsedSecs() > $longestTime) {
$longestTime  = $query->getElapsedSecs();
$longestQuery = $query->getQuery();
}
$queries .= '<small>('.round($query->getElapsedSecs(),5).' seconds)</small> '.$query->getQuery().'<hr style="border-top:1px solid #cccccc" />';
}
echo 'Executed <strong>' . $queryCount . '</strong> queries in <strong>' . $totalTime . ' seconds</strong>' . "<br />";
echo 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "<br />";
echo 'Queries per second: ' . $queryCount / $totalTime . "<br />";
echo 'Longest query length: ' . $longestTime . "<br />";
echo "Longest query: \n" . $longestQuery . "<br />";
echo '<hr />'.$queries;
echo '</div>';
}

Although making sure you’ve got jQuery already installed before using the above code. And make sure to subscribe to my RSS feed for possible future awesomeness :)

 

Christmas / New Years. A bit late!

Happy New Year everyone. Good 2009 greetings. A bit late I know!

Not much news to be honest, on anything that I usually write about at least. Not much going on with the Mini lately, lack of funds after Xmas, plus MOT & Insurance to pay for on my Nissan 200s this month!

On the plus side, a brake upgrade was due on my 200sx and I’ve opted for the superb (I hope) big brake upgrade from DB-Power. I went for the 345mm Brembo front disks (vented and grooved), although I’ve been told they are so popular they’re currently out of stock, and since my MOT is rapidly approaching, I instead went for the slightly smaller 330mm Brembo’s. On that note then, good stopping power on both my cars – always a bonus!

And a final note to tell the world about Grab A Mascot – the latest project to come out of work and the 3rd project entirely by me from the company. I’m really pleased with it and I’ve taken just about 3 weeks of development time to complete it, from scratch. Its based on Zend Framework with a little touch of jQuery. Check it out! :)

That is all for now – possible photos of my new Brembo stoppers soon…

 

Linking CSS stylesheet on Zend Framework specific pages

Every so often, at the beginning of a new project at work, I almost always have to add a page-specific stylesheet to a certain page on the project. And usually (almost always it seems) I forget the exact code to enter at the top of the .phtml page.

And I thought I should blog it, so that at least I remember it for future use…

This is for Zend Framework-built sites only, of course, and so long as this is in your <head> section of your bootstrap Index file…

<?php
echo $this->headTitle();
echo $this->headScript();
echo $this->headStyle();
echo $this->headLink();
?>

…Then, on the page we’re talking about, before output on your .phtml file, add the following code

$this->headLink()->appendStylesheet('/path/to/page-styles.css');

Done! So there you are. Some of you might have already known that, maybe some of you have no idea what I’m talking about – but at least I know where to find this damn code again!