views:

37

answers:

1

I am trying to store custom routes for the Zend Framework inside the database. I have the process down that will create the route, but the problem I am running into is that when I am adding the routes it looks like Zend has not yet created its connection to the database.

Does anyone know where this processes initially happens or how I can force the database to connect from the init_routes function inside Bootstrap.php?

Thanks

Josh Pennington

UPDATE:

What I am doing from Bootstrap.php is calling a model that will return all the Zend_Controller_Router_Route_Static objects for the vendors. Here is the code I am using inside Bootstrap.php

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();

$vendor_routes = new Application_Model_Vendor();
$vendor_routes = $vendor_routes->getStaticRoutes();

The code inside the getStaticRoutes() function is as follows:

public function getStaticRoutes() {
    $select = $this->select();
    $select->from($this)
        ->where("featured = 1");
    $rows = $this->fetchAll($select);

    foreach($rows as $row) {
        print_r($row);
    }
}

This function is contained in a model that extends Zend_Db_Table_Abstract

The error that I am getting is as follows:

<b>Fatal error</b>:  Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Application_Model_Vendor' in /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php:754
Stack trace:
#0 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract-&gt;_setupDatabaseAdapter()
#1 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract-&gt;_setup()
#2 /var/www/vhosts/weddingdir/weddingdir/application/Bootstrap.php(17): Zend_Db_Table_Abstract-&gt;__construct()
#3 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap-&gt;_initRoutes()
#4 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource('routes')
#5 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_bootstrap(NUL in <b>/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php</b> on line <b>754</b><br /> 

Thanks again!

+2  A: 

It should connect on demand. If you have an equivalent init method for initialising your database connection, you just need to ensure that this is bootstrapped before your routes, like this:

protected function _initRoutes()
{
    // this will trigger the _initDb method. 'db' should match
    // the name of that method
    $this->bootstrap('db');

    // routes stuff here
}

protected function _initDb()
{
    // db stuff here
}

Edit: okay, looks like you just need to tell Zend_Db_Table to use your DB connection. In code you'd do:

Zend_Db_Table_Abstract::setDefaultAdapter($db);

in application.ini I think you can do:

resources.db.isDefaultTableAdapter = true

which I'm guessing you don't currently have. See if that fixes your issue. See the DB section on http://framework.zend.com/manual/en/zend.application.available-resources.html for a fuller example.

Tim Fountain
My Bootstrap.php does not have the _initDb() inside of it. I would assume it connects to the database when it needs to (it does everywhere else in the application) but inside of Bootstrap it does not. I am thinking that its because the database connection info is stored in the application.ini file and that it maybe has not parsed that file yet or something.
Josh Pennington
The application.ini stuff is run before all of the _init methods, so you should be fine. Can you post an example of how you are accessing/using the db class in your route method? $this->getResource('db'); should give you the DB class as setup by your application.ini data, so you could try var_dump()'ing that to see what you get.
Tim Fountain
I added some more information in my initial post.
Josh Pennington
Thanks. I think you just need to set the default adapter for Zend_Db_Table - I've edited my answer to include an example of how to do this.
Tim Fountain
This is indeed the right approach. You must bootstrap the 'db' in your initRoutes method first to make sure it is initialised.
David Caunt