How to Dump the Generated Zend_Db SQL Query

The Typical PHP Approach

Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query.

$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);

So eventually when you want to dump this “complex” query, or whatever query there is, you can simply “echo” it and see what’s its syntax.

// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";
 
// dump and debug the wrong query
die($sql);
 
// this line won't be executed
$resource = mysql_query($sql);

So far so good, but things appear to be a bit different when you start to work with Zend Framework. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.

OK you’ve two options. Using Zend_Db_Select or … not.

Dump Zend_Db_Select Query

Debugging queries generated with Zend_Db_Select is as easy as the following snippet. Let’s say there’s a db table “users” and typically the model is called Users.

class Users extends Zend_Db_Table
{
	protected $_name = 'users';
 
	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');
 
		$rows = $this->fetchAll($select);
	}
}

Obviously this code is wrong, because the “where” clause is wrong and we don’t pass any values to it. So now the question is how can we dump this SQL. Well, since we use Zend_Db_Select, so the simplest way to dump the SQL is by calling __toString(). The code above will become as follows.

class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;
 
	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');
 
		die($select->__toString());
 
		$rows = $this->fetchAll($select);
	}
}

That’s great, but how can we dump update/insert queries for instance?

Dump with Zend_Db_Profiler

The way you should dump insert or update queries is a bit different. You should use Zend_Db_Profiler, which is a more complex way to debug (profile) your queries. You can not only debug insert or update queries, of course, but any queries generated by the Zend_Db abstraction. Let’s see how.

Now from a controller perspective this model can be just called as:

$users = new Users();
$users->update(array('name' => 'my name'), 'id =');

Obviously this code is wrong, again because of the where clause and now the question is how we can debug the generated SQL. The answer is: using Zend_Db_Profiler. Looking back again to the model, this should look like this.

class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;
 
	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);
 
		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');
 
		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());
 
		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
	}
}

The thing is that since Zend_Db is using prepared statements, the last row (the one with the Zend_Debug::…) will dump something like the following line.

UPDATE `users` SET `name` = ? WHERE (id =)

So as you can see, actually here you don’t see the name’s value, which should be “my name”. To see this you should call getQueryParams().

class Users extends Zend_Db_Table
{
	protected $_name = 'users';
 
	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);
 
		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');
 
		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQueryParams());
 
		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);

Now the debug info will be something like this.

UPDATE `users` SET `name` = ? WHERE (id =)
array
	1 => string 'my name' (length 7)

Calling this from the controller there are some changes. Take a look at the example bellow.

$users = new Users();
 
// enable the profiler
$users->getAdapter()->getProfiler()->setEnabled(true);
 
// execute (or at least try to)
$users->update(array('name' => 'my name'), 'id =');
 
// debugdump the SQL
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQuery());
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQueryParams());
 
// don't forget to disable the profiler is
// you don't need it anymore
$users->getAdapter()->getProfiler()->setEnabled(false);

In this case you should use the longer model_name->getAdapter()

One thought on “How to Dump the Generated Zend_Db SQL Query

  1. Thanks for this post; I found it very helpful.

    One correction, which I discovered when trying out this technique – the profiler seems to be accessible from the database adapter, rather than from Zend_Db_Table, so you need to do:


    $this->getAdapter()->getProfiler();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>