2012-06-25 4 views
0

나는 crontoller에서 db 연결을 저장하고 zend 레지스트리를 사용하여 다른 매퍼 클래스에서 사용하려고합니다. 내가 어떻게 문제를 겪고 있는지 알 수 없다.다른 db 연결을 저장하는 방법

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

기본 애플릿에서 가져옵니다. setDbTable 함수를 다음과 같이 변경하면 현재 작동합니다. '$ dbAdapter = Zend_Db :: factory ("pdo_sqlite", array ("dbname"=> $ filename));' – shorif2000

관련 문제