0

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

索引控制器.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 
4

1 回答 1

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

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

我想应该是:

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

Application_Model_PgeLine2DMapper 用在线路控制器上?

于 2012-06-25T09:38:49.057 回答