Debugging Zend Framework using firebug

You can use firebug to debug your Zend Framework application. May be you are using print_r(), var_dump() or Zend_Debug::dump($var), they prints the information in your application. But, with the help of FirePHP add-on for Firebug, we will be able to dump our variable on firebug console :D

First install the FirePHP add-on for your Firebug. Then you can use the following snippet anywhere to dump your variables to firebug console -

$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log( $writer );
$logger->log('My Sample Log', Zend_Log::DEBUG);

The second parameter of the Zend Logger is a constant. You can use 0-7 (zero to seven) as a parameter. Here is the details – Continue reading

Get the executed query in Zend Framework

It seems very difficult when you are working with Zend Framework and you are getting some error for some SQL query. It’s hard to debug this issue for you sometimes. Then you may need to know what was the query executed by Zend_Db_Table_Abstract that was generating the precious(!) error?. Well, that’s simple like the snipped below.

May be you have a Profile Model that extends Zend_Db_Table_Abstract and you have a getUserByName function.

class Application_Model_Profile extends Zend_Db_Table_Abstract {
	protected $_name = 'users';

	function getUserByName( $user_name ) {
        $row = $this->fetchRow("username = '$user_name'");

        if (!$row) {
            return false;
        }

        return $row->toArray();
    }
}

We will write our function to debug the query like this:

class Application_Model_Profile extends Zend_Db_Table_Abstract {
	protected $_name = 'users';

	function getUserByName( $user_name ) {
        $this->_db->getProfiler()->setEnabled(true); //start the profiler
        $row = $this->fetchRow("username = '$user_name'");
        $query = $this->_db->getProfiler()->getLastQueryProfile()->getQuery(); //get the last executed query
        var_dump( $query ); //show the query
        $this->_db->getProfiler()->setEnabled(false); //disable the profiler

        if (!$row) {
            return false;
        }

        return $row->toArray();
    }
}

What we did?

  1. We enabled the profiler before executing the query
  2. Then we executed our query
  3. Stored our raw query in a variable and print it
  4. Finally disabled the profiler

That might help you to debug the SQL :D