2012-06-25 26 views
0

我想從一個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 

回答

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

是這條線80的/選擇/ eposdatatransfer /應用/模型/ PgeLine2DMapper.php?

我想這應該是:

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

Application_Model_PgeLine2DMapper在流水線控制器使用?

+0

這取自默認adpater。我設法將setDbTable函數更改爲以下版本,現在可以使用。 ($ dbAdapter = Zend_Db :: factory(「pdo_sqlite」,array(「dbname」=> $ filename));'' – shorif2000