开发者

How to get records from outside database in Zend?

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

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

Now I want to access some other database but with same table structure as local database. How can I switch my dat开发者_如何学运维abase for that specific controller/action where I want to show data for other database.

Thanks.


You can use Zend_Application_Resource_Multidb

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

Similar question(s) on Stackoverflow

  1. Can't connect to external database with Zend Framework but mysql connect works.

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.

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

So I checked this function in 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');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜