如何存储不同的数据库连接

时间:2012-06-25 09:24:38

标签: php zend-framework centos zend-db

我正在尝试从crontoller存储数据库连接,并尝试使用zend注册表在不同的映射器类中使用它。我无法弄清楚我是如何遇到问题的。

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;

}

PgeLine2DMapper.php

<?php

class Application_Model_PgeLine2DMapper
{
protected $_dbTable;

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


    $registry = Zend_Registry::getInstance();
    //print_r($registry);

    /*
    try {
        //$db = Zend_Db::factory('Pdo_sqlite', $registry['multidb']);
        //$db->getConnection();

        //Zend_Db::factory("pdo_sqlite", array("dbname"=>"/../data/db/".$x["username"].".db"));
    } catch (Zend_Db_Adapter_Exception $e) {
        // perhaps a failed login credential, or perhaps the RDBMS is not running
    } catch (Zend_Exception $e) {
        // perhaps factory() failed to load the specified Adapter class
    }
    */

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

    if (is_string($dbTable)) {
        $dbTable = new $dbTable();
    }
    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()
{
    $registry = Zend_Registry::getInstance();
    $resultSet = $registry['index']->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: Call to undefined method Zend_Application_Resource_Multidb::getDbTable() in /opt/eposdatatransfer/application/models/PgeLine2DMapper.php on line 80 

1 个答案:

答案 0 :(得分:0)

$resultSet = $registry['index']->getDbTable()->fetchAll();

是/opt/eposdatatransfer/application/models/PgeLine2DMapper.php的第80行?

我想它应该是:

$resultSet = $this->getDbTable()->fetchAll();

Application_Model_PgeLine2DMapper用于线路控制器?