Zend_Db_Select: Select query cannot join with another table

When I executed a query created with Zend_Db_Select in a Zend_Db_Table, I got the following error message:

Select query cannot join with another table

What is happening here? A very simple select statement with a join is not allowed in Zend_Db_Select?
To solve this mysterious matter, I decided to Google the error message, off course. Luckily, one of the first results I got was the Zend Framework manual, a page on Zend_Db_Table. Luckily? Not really, because the documentation only mentions “You can allow JOIN clauses on a select to allow multi-table lookups”. That’s great, but… HOW?

But as always, the documentation comes to the rescue! On the Zend_Db_Select page a comment points in the right direction. There exists an undocumented function “setIntegrityCheck(false)“. And if you see the code of Zend_Db_Table_Select, in the assemble() function, it becomes even more clear: there is indeed a parameter “$_intgrityCheck” that can ensure that selected columns are only from the primary table.

So I pass this function in my Zend_Db_Select statement, and now it is possible to do joins!

View helpers and variables not available in layout

I got a really strange problem while implementing a menu with Zend_Navigation. I added this piece of code in the Bootstrap.php:

    protected function _initNavigation()
    {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/navigation.ini');
 
        $navigation = new Zend_Navigation($config);
        $view->navigation($navigation);
    }

This is fine for creating a menu or breadcrumbs, but there is one big problem: after this code is in use, all custom variables and the View Helper Include path are not available any more in the layout phtml file.

For example “echo $this->name” just echoes null. Or the custom view helper “$this->myHelper()” cannot be found because the View Helper include path only contains the path to Zend View Helpers.

And writing the same code in the phtml file of a simple action does work!

So what is the relation between the code above and the problems mentioned?

It all starts at the first line of the function “_initNavigation()”: there is a bootstrap function for the layout. Unfortunately it is necessary to add another line to not have the problems above. It is needed to also bootstrap the view! So the code becomes:

    protected function _initNavigation()
    {
        $this->bootstrap('layout');
        $this->bootstrap('view');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/navigation.ini');
 
        $navigation = new Zend_Navigation($config);
        $view->navigation($navigation);
    }

Now the layout AND the view are bootstrapped, and the custom view helpers and variables can again be used in the layout phtml file.

I found this interesting issue in the ZF Jira: ZF-9537. It has already been reported, but it is not an issue (see status). So this is supposed to be normal behaviour? Does anyone has a good explanation for this?

Zend Navigation: Breadcrumbs minimum depth

Creating breadcrumbs with Zend Framework is really easy:

  1. Create a menu structure (ini file, php array, …)
  2. add a small peace of code in the view file: “echo $this->navigation()->breadcrumbs()”
  3. open your page and you see… nothing

This is what happened to me on the home page of my most recent project.  All though the ZF manual indicates you just have to echo the breadcrumbs, nothing appeared.

The problem was this: my menu started at level zero, and ZF sets the minimum level that will be displayed to one. Luckily there is the documentation, but it is really annoying that the method “setMinDepth()” is not in the API, so it took me much longer to find out this method existed.