Feb 17 2011

Logging queries in Doctrine 1.2

The developers of Doctrine have made the task of logging queries trivial, thanks to their event listener architecture.

One just needs to create a class which extends Doctrine_EventListener:

class QueryDebuggerListener extends Doctrine_EventListener
{
    protected $queries;

    public function preStmtExecute(Doctrine_Event $event)
    {   
        $query = $event->getQuery();
        $params = $event->getParams();

        //the below makes some naive assumptions about the queries being logged
        while (sizeof($params) > 0) {
            $param = array_shift($params); 

            if (!is_numeric($param)) {
                $param = sprintf("'%s'", $param);
            }   

            $query = substr_replace($query, $param, strpos($query, '?'), 1); 
        }   

        $this->queries[] = $query;
    }

    public function getQueries()
    {   
        return $this->queries;
    }
}

And add the event listener:

$c = Doctrine_Manager::connection($conn);
$queryDbg = new QueryDebuggerListener();
$c->addListener($queryDbg);

I like to use the above to get a feel for how many queries are being performed.

For example, I was using $foo->Bar['field'] to retrieve a field value from a relation at one point. The code can result in another query, which could be avoided by using a join on the original query for the record. See the Doctrine documentation for a clearer example.

Feb 05 2011

Redirecting from an old route in Zend Framework

Firstly ensure your old route has been created:

routes.new.route = "new"
routes.new.defaults.controller = index
routes.new.defaults.action = new
routes.new.defaults.module = default
routes.new.defaults.id = ""
routes.new.reqs.id = \d+

routes.old.route = "old"
routes.old.defaults.controller = index
routes.old.defaults.action = old
routes.old.defaults.module = default
routes.old.defaults.id = ""
routes.old.reqs.id = \d+

In the controller:

class IndexController extends Zend_Controller_Action 
{
    ...

    public function oldAction()
    {
        $redirector = $this->_helper->getHelper('Redirector');
        $redirector->setCode(301);
        return $redirector->gotoRoute($this->_getAllParams(), 'new');
    }
}

For reference, examples developed with Zend Framework 1.11.2

Feb 05 2011

Setting a default page title in Zend Framework and modifying per action

There may be a better way of doing this, I'm open to suggestions. Still, my goal was to have a default page title, manipulating when required in an action.

I initially tried to just use $this->headTitle('foobar'); in my layout and prepend in the controller action. This didn't work out too well, due to the order in which they're executed - append and prepend are reversed. Not too intuitive.

Therefore, I created an _init method in the Bootstrap class:

public function _initTitle()
{
    $view = $this->bootstrap('view')->getResource('view'); 
    $view->headTitle('foobar');
}

Modifying the title in the action is very simple:

public function fooAction()
{
    $this->view->headTitle()->prepend('foo | ');
}

Accessing the foo action will yield:

<title>foo | foobar</title>

For reference example was written with Zend Framework 1.11.2.

Info

I'm Ade Slade, a PHP web developer.