1

I am trying to connect to two databases with Zend Framework. How ever it is throwing me the following error

#0 C:\xampp\php\zf\library\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\xampp\php\zf\library\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 C:\xampp\php\zf\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Select), Array)
#3 C:\xampp\php\zf\library\Zend\Db\Adapter\Abstract.php(736): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Select), Array)
#4 C:\xampp\htdocs\ne11133_multiDB\application\models\DbTable\CandyEligible.php(15): Zend_Db_Adapter_Abstract->fetchAll(Object(Zend_Db_Select))
#5 C:\xampp\htdocs\ne11133_multiDB\application\models\Index.php(179): Application_Model_DbTable_CandyEligible->test()
#6 C:\xampp\htdocs\ne11133_multiDB\application\controllers\IndexController.php(22): Application_Model_Index->getTest()
#7 C:\xampp\php\zf\library\Zend\Controller\Action.php(516): IndexController->indexAction()
#8 C:\xampp\php\zf\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#9 C:\xampp\php\zf\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#10 C:\xampp\php\zf\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#11 C:\xampp\php\zf\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#12 C:\xampp\htdocs\ne11133_multiDB\public\index.php(26): Zend_Application->run()
#13 {main}

So I took a look at that function in statement.php and it appears that none of the DB parameters are being passed in. Unfortunately I cannot figure out why and there really isn't very much when searching for a solution to this so I am hoping someone here can give me a hand. Below is the code related to this (names have been changed for security purposes)

application.ini - Am using dev

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout"

resources.multidb.candy.adapter = PDO_MYSQL
resources.multidb.candy.host = local
resources.multidb.candy.username = name
resources.multidb.candy.password = pass
resources.multidb.candy.dbname = db

resources.multidb.vt3.adapter = PDO_MYSQL
resources.multidb.vt3.host = not_local
resources.multidb.vt3.username = name
resources.multidb.vt3.password = pass
resources.multidb.vt3.dbname = db2

[staging : production]
resources.multidb.candy.adapter = PDO_MYSQL
resources.multidb.candy.host = local
resources.multidb.candy.username = name
resources.multidb.candy.password = pass
resources.multidb.candy.dbname = db

resources.multidb.vt3.adapter = PDO_MYSQL
resources.multidb.vt3.host = not_local
resources.multidb.vt3.username = name
resources.multidb.vt3.password = pass
resources.multidb.vt3.dbname = db2

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

resources.multidb.candy.adapter = PDO_MYSQL
resources.multidb.candy.host = local
resources.multidb.candy.username = name
resources.multidb.candy.password = pass
resources.multidb.candy.dbname = db

resources.multidb.vt3.adapter = PDO_MYSQL
resources.multidb.vt3.host = not_local
resources.multidb.vt3.username = name
resources.multidb.vt3.password = pass
resources.multidb.vt3.dbname = db2

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

resources.multidb.candy.adapter = PDO_MYSQL
resources.multidb.candy.host = 192.168.1.34
resources.multidb.candy.username = name
resources.multidb.candy.password = pass
resources.multidb.candy.dbname = db
resources.multidb.candy.default = FALSE

resources.multidb.vt3.adapter = PDO_MYSQL
resources.multidb.vt3.host = 192.168.1.32
resources.multidb.vt3.username = name
resources.multidb.vt3.password = pass
resources.multidb.vt3.dbname = db2
resources.multidb.vt3.default = TRUE

Bootstrap

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    public function __init()
    {
        $options = array(
            'layout'=>'layout',
            'layoutPath'=>'/application/layouts',
        );
        $layout = Zend_Layout::startMvc($options);
    }

    public function _initDbRegistry()
    {
        $this->bootstrap('multidb');
        $multidb = $this->getPluginResource('multidb');
        Zend_Registry::set('db_candy', $multidb->getDb('candy'));
        Zend_Registry::set('db_vt3', $multidb->getDb('vt3'));
    }
}

DB Model

<?php
class Application_Model_DbTable_CandyEligible extends Zend_Db_Table_Abstract
{
    protected $_name = 'table_name';
    protected $_adapter = 'db_candy';

    public function test()
    {
        $select = $this->_db->select();
        $select->from($this->_name);
        try{
            $results = $this->_db->fetchAll($select);
        }catch(Exception $exc){
            echo '<pre>'.$exc->getTraceAsString().'</pre>';
        }
        return $result;
    }
}

Since this is just at least function to see that it is connecting to the databases and I can read them the controller (and index model) is just echoing out the results

tereško
  • 58,060
  • 25
  • 98
  • 150
JoeyD473
  • 2,890
  • 2
  • 21
  • 25
  • What is the actual error message you're seeing? – christopher_b Mar 16 '12 at 15:18
  • It only gives me the stack trace - #0 C:\xampp\php\zf\library\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array) – JoeyD473 Mar 16 '12 at 15:29
  • It's probably throwing an exception. Can you catch it and check the message? – christopher_b Mar 16 '12 at 15:43
  • @christopher_b I tried catching the error where the first stack trace gives teh error and it gives me only a stack trace. The first line is as follows: '#0 C:\xampp\php\zf\library\Zend\Db\Statement.php(312): Zend_Db_Profiler->getQueryProfile(NULL)' I go to that statement and it is int eh same function – JoeyD473 Mar 16 '12 at 15:51
  • Did you try with $exc->getMessage() ? – christopher_b Mar 16 '12 at 15:54
  • @christopher_b Ok, It seems to be falling back to the default Database - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db2.table' doesn't exist It should be going for the candy table – JoeyD473 Mar 16 '12 at 16:17

2 Answers2

2

I do it in this way: in bootstrap

$multiDb = $this->getPluginResource('multidb');
Zend_Registry::set('multidb', $multiDb);

then for your models that uses candy db I create parent class like this

abstract class App_Db_CandyTable extends App_Db_Table
{
    public function __construct($config = array(), $definition = null)
    {
        // use dns connection
        $multiDbResource = Zend_Registry::get('multidb');
        $adapter = $multiDbResource->getDb('candy');
        parent::__construct($adapter);
    }   
}

That's it you inherit model class from App_Db_CandyTable and it will automatically select appropriate connection

radzserg
  • 1,258
  • 1
  • 13
  • 22
0

I don't think $_adapter is a property of the Zend Table class.

I think you can set the table's adapter by overriding the _setupDatabaseAdapter function:

protected function _setupDatabaseAdapter()
{
  $db = Zend_Registry::get('db_candy');
  $this->_db = $db;
  parent::_setupDatabaseAdapter();
}
christopher_b
  • 958
  • 5
  • 13