如何从Zend的外部数据库中获取记录?

时间:2022-01-13 00:36:30

I am using following configuration in application.ini for database;

我在application.ini中使用以下配置作为数据库;

resources.db.adapter = "Mysqli"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.dbname = "dbname"
resources.db.params.charset = "utf8"

After that using standard Model/Mapper/DbTable classes to access database like this

之后使用标准的Model / Mapper / DbTable类来访问这样的数据库

Now I want to access some other database but with same table structure as local database. How can I switch my database for that specific controller/action where I want to show data for other database.

现在我想访问一些其他数据库,但具有与本地数据库相同的表结构。如何切换我想要显示其他数据库数据的特定控制器/操作的数据库。

Thanks.

谢谢。

1 个解决方案

#1


6  

You can use Zend_Application_Resource_Multidb

您可以使用Zend_Application_Resource_Multidb

There is an example of its usage here, under the heading Different user/host.

这里有一个使用它的例子,在不同的用户/主机标题下。

Similar question(s) on *

类似的问题*

  1. Can't connect to external database with Zend Framework but mysql connect works.
  2. 无法使用Zend Framework连接到外部数据库但是mysql connect工作正常。

EDIT: I also discovered that even when I had the following lines in my Model inherited from Zend_Db_Table_Abstract it was not connecting to the remote DB.

编辑:我还发现,即使我在继承自Zend_Db_Table_Abstract的模型中有以下行,它也没有连接到远程数据库。

protected $_schema  = 'otherdb';
protected $_adapter = 'db_remote'; 

So I checked this function in Zend_Db_Table_Abstract

所以我在Zend_Db_Table_Abstract中检查了这个函数

protected function _setupDatabaseAdapter()
    {
        if (! $this->_db) {
            $this->_db = self::getDefaultAdapter();
            if (!$this->_db instanceof Zend_Db_Adapter_Abstract) {
                require_once 'Zend/Db/Table/Exception.php';
                throw new Zend_Db_Table_Exception('No adapter found for ' . get_class($this));
            }
        }
    }

If its the same for you, add the following lines to your constructor:

如果它与您相同,请将以下行添加到构造函数中:

public function __construct() {
    $front = Zend_Controller_Front::getInstance();
    $bootstrap = $front->getParam('bootstrap');
    $resource = $bootstrap->getPluginResource('multidb');
    $this->_db = $resource->getDb('chdpg');
}

#1


6  

You can use Zend_Application_Resource_Multidb

您可以使用Zend_Application_Resource_Multidb

There is an example of its usage here, under the heading Different user/host.

这里有一个使用它的例子,在不同的用户/主机标题下。

Similar question(s) on *

类似的问题*

  1. Can't connect to external database with Zend Framework but mysql connect works.
  2. 无法使用Zend Framework连接到外部数据库但是mysql connect工作正常。

EDIT: I also discovered that even when I had the following lines in my Model inherited from Zend_Db_Table_Abstract it was not connecting to the remote DB.

编辑:我还发现,即使我在继承自Zend_Db_Table_Abstract的模型中有以下行,它也没有连接到远程数据库。

protected $_schema  = 'otherdb';
protected $_adapter = 'db_remote'; 

So I checked this function in Zend_Db_Table_Abstract

所以我在Zend_Db_Table_Abstract中检查了这个函数

protected function _setupDatabaseAdapter()
    {
        if (! $this->_db) {
            $this->_db = self::getDefaultAdapter();
            if (!$this->_db instanceof Zend_Db_Adapter_Abstract) {
                require_once 'Zend/Db/Table/Exception.php';
                throw new Zend_Db_Table_Exception('No adapter found for ' . get_class($this));
            }
        }
    }

If its the same for you, add the following lines to your constructor:

如果它与您相同,请将以下行添加到构造函数中:

public function __construct() {
    $front = Zend_Controller_Front::getInstance();
    $bootstrap = $front->getParam('bootstrap');
    $resource = $bootstrap->getPluginResource('multidb');
    $this->_db = $resource->getDb('chdpg');
}