简体   繁体   中英

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 database 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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM