zend multidb for sqlite

时间:2012-06-22 16:21:39

标签: php zend-framework centos zend-db

我正在尝试设置multidb。多个数据库连接将由用户使用表单动态输入。

bootstrap.php中

private $_session = null;

    public function init() {

        Zend_Session::start();

        $this->_session = new Zend_Session_Namespace();

    }

    protected function _initDb()
    {
        $resource = $this->getPluginResource('multidb');
        Zend_Registry::set("multidb", $resource);

    }

indexcontroller.php

public function indexAction()
{
    // action body

    $request = $this->getRequest();
    $form    = new Application_Form_Project();

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($request->getPost())) {
            $x = $form->getValues();                
            //return $this->_helper->redirector('index');
            $dbAdapter = Zend_Db::factory("pdo_sqlite", array("dbname"=>"/../data/db/".$x["username"].".db"));
            Zend_Db_Table::setDefaultAdapter($dbAdapter);

            //print_r($dbAdapter);
            Zend_Registry::set('index', $dbAdapter);

            $this->_redirect('/line');


        }
    }

    $this->view->form = $form;

}

Application_Model_PgeLine2DMapper

protected $_dbTable;

public function setDbTable($dbTable)
{
    $multidb = Zend_Registry::get("multidb");


    $registry = Zend_Registry::getInstance();
    print_r($registry);
    foreach ($registry as $index => $value) {
        echo "Registry index $index contains: ";
        var_dump($value);
        echo  "<br>";
    }

    if (is_string($dbTable)) {
        $dbTable = new $dbTable($multidb->getDb('db1'));
    }
    if (!$dbTable instanceof Zend_Db_Table_Abstract) {
        throw new Exception('Invalid table data gateway provided');
    }
    $this->_dbTable = $dbTable;
    return $this;
}

public function getDbTable()
{
    if (null === $this->_dbTable) {
        $this->setDbTable('Application_Model_DbTable_PgeLine2D');
    }
    return $this->_dbTable;
}

public function save(Application_Model_PgeLine2D $pgeLine2D)
{
    $data = array(
            'PGA_Name'   => $pgeLine2D->getPGA_Name()
    );

    if( null === ($id = $pgeLine2D->getPGA_Id()) ) {
        unset($data['id']);
        $this->getDbTable()->insert($data);
    } else {
        $this->getDbTable()->update($data, array('PGA_Id = ?' => $id));
    }
}

public function find($id, Application_Model_PgeLine2D $pgeLine2D)
{
    $result = $this->getDbTable()->find($id);
    if (0 == count($result)) {
        return;
    }
    $row = $result->current();
    $pgeLine2D->setPGA_Id($row->PGA_Id)
        ->setPGA_Name($row->PGA_Name);
}

public function fetchAll()
{
    $resultSet = $this->getDbTable()->fetchAll();
    $entries   = array();
    foreach ($resultSet as $row) {
        $entry = new Application_Model_PgeLine2D();
        $entry->setPGA_Id($row->PGA_Id)
            ->setPGA_Name($row->PGA_Name);
        $entries[] = $entry;
    }
    return $entries;
}

错误我得到的是

Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'Configuration array must have a key for 'dbname' that names the database instance' in /opt/eposdatatransfer/library/Zend/Db/Adapter/Pdo/Sqlite.php:110 Stack trace: #0 /opt/eposdatatransfer/library/Zend/Db/Adapter/Abstract.php(183): Zend_Db_Adapter_Pdo_Sqlite->_checkRequiredOptions(Array) #1 /opt/eposdatatransfer/library/Zend/Db/Adapter/Pdo/Sqlite.php(94): Zend_Db_Adapter_Abstract->__construct(Array) #2 /opt/eposdatatransfer/library/Zend/Db.php(270): Zend_Db_Adapter_Pdo_Sqlite->__construct(Array) #3 /opt/eposdatatransfer/library/Zend/Application/Resource/Multidb.php(99): Zend_Db::factory('PDO_SQLITE', Array) #4 /opt/eposdatatransfer/library/Zend/Application/Bootstrap/BootstrapAbstract.php(683): Zend_Application_Resource_Multidb->init() #5 /opt/eposdatatransfer/library/Zend/Application/Bootstrap/BootstrapAbstract.php(626): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('multidb') #6 /opt/eposdatatransfer/library/Zend/Application/Bootst in /opt/eposdatatransfer/library/Zend/Db/Adapter/Pdo/Sqlite.php on line 110 

0 个答案:

没有答案