Zend_Db_Profiler

As I am newcomer from Cake framework, I liked the possibility to see the database queries. So i decided to make it also available at ZF. Zend_Db_Profiler is right tool for that. I have added a bit design but you can off curse change it like you want.

Here comes code. If you don’t want to make this directory like i have my/controller/action/helper/ you need to change the class name.


< ?php

class My_Controller_Action_Helper_Profiler extends Zend_Controller_Action_Helper_Abstract {

    public function postDispatch() {

        $db = Zend_Registry::get('db');
        $profiler = $db->getProfiler();

        if (!$profiler->getEnabled()) {
            exit();
        }

        $css = '
            <style type="text/css">
            .line {
                background-color: red;
            }

            td {
                border-style: solid;
                border-color: #900000;
                border-width: 1px;
                font-family: sans-serif;
            }
            </style>
        ';

        $this->getResponse()->appendBody($css);
        $this->getResponse()->appendBody('<hr class="line" /><table>');

        if ($profiler->getQueryProfiles() != null) {
            foreach ($profiler->getQueryProfiles() as $profile) {
                $this->getResponse()->appendBody("
                  <tr>
                    <td>{$profile->getQuery()}</td>
                    <td>{$profile->getElapsedSecs()}</td>
                  </tr>
                ");
            }       
        }

        $this->getResponse()->appendBody("</table>")
            ->appendBody("<p>Total elapsed secs: " . $profiler->getTotalElapsedSecs() . "</p>")
            ->appendBody("<p>Total num of queries: " . $profiler->getTotalNumQueries() . '</p><hr class="line" />');

    }

}
?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s