强制 Zend Framework 比平常更早连接到数据库

发布于 2024-09-27 11:31:12 字数 2164 浏览 5 评论 0原文

我正在尝试在数据库内存储 Zend Framework 的自定义路由。我已经完成了创建路由的过程,但我遇到的问题是,当我添加路由时,Zend 似乎尚未创建与数据库的连接。

有谁知道这个过程最初发生在哪里,或者我如何强制数据库从 Bootstrap.php 中的 init_routes 函数连接?

更新:

我在 Bootstrap.php 中所做的事情是调用一个模型,该模型将为供应商返回所有 Zend_Controller_Router_Route_Static 对象。这是我在 Bootstrap.php 中使用的代码

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

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

getStaticRoutes() 函数内的代码如下:

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

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

该函数包含在扩展 Zend_Db_Table_Abstract 的模型中

我收到的错误如下:

<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 /> 

再次感谢!

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?

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->_setupDatabaseAdapter()
#1 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract->_setup()
#2 /var/www/vhosts/weddingdir/weddingdir/application/Bootstrap.php(17): Zend_Db_Table_Abstract->__construct()
#3 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap->_initRoutes()
#4 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('routes')
#5 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(NUL in <b>/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php</b> on line <b>754</b><br /> 

Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-10-04 11:31:12

它应该按需连接。如果您有一个等效的 init 方法来初始化数据库连接,您只需要确保在您的路由之前引导该方法,如下所示:

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
}

编辑:好的,看起来您只需要告诉 Zend_Db_Table 使用您的数据库连接。在代码中你可以这样做:

Zend_Db_Table_Abstract::setDefaultAdapter($db);

在 application.ini 中我认为你可以这样做:

resources.db.isDefaultTableAdapter = true

我猜你目前还没有。看看这是否能解决您的问题。请参阅 http://framework.zend.com/ 上的数据库部分Manual/en/zend.application.available-resources.html 以获得更完整的示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文