2011-08-10 4 views
2

내 기본 데이터베이스 어댑터를 bootstrap.php 파일에 등록하려고합니다. 그래서 내가 어디에 있든 액세스 할 수 있습니다.Zend 데이터베이스 어댑터 - bootstrap.php에 등록 하시겠습니까?

//bootstrap.php 
protected function _initDb() 
{ 
    $dbAdapter = Zend_Db::factory(Zend_Registry::get('configuration') 
             ->resources->db->adapter, 
            Zend_Registry::get('configuration') 
             ->resources->db->params->toArray()); 

    Zend_Registry::set('dbAdapter', $dbAdapter); 
    Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); 
} 

그때 내 모델 중 하나에 전화를 시도하고 말하면서 :이 지금까지 내 코드입니다

Call to undefined method Application_Model_Example::_getdbAdapter() in... 
:

//exampleModel.php 
$select = $this->_getDbAdapter() 
       ->select() 
       ->from(array('t' => $this->_getTable()->getName()), 
         array('name')).... 

그러나 난 그냥 오류를 얻고있다

그래서 분명히 현재 수업 내에서 그것을 찾고있어 그것을 찾을 수 없습니다 ...

+0

application.ini에서이 작업을 시도해 보셨습니까? 나는 그것을하는 것이 더 좋은 곳이라고 생각한다. – vascowhite

+0

뭐하는거야? 내가 응용 프로그램 ini에서 내 db 호스트, 이름 및 암호 등을 선언했지만 난 어댑터가 – Pjn2020

+0

만들려고 noe 이미 만들어져 있습니다. 이거 읽었 니? http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.db – vascowhite

답변

1

당신은 이것을 Model_Example에서 필요로합니다.

public function _getSqlAdapter() 
{ 
    return Zend_Registry::get('dbAdapter'); 
} 

또는 직접 당신이 그것을 레지스트리에서 어댑터를 호출 할 나타나지 않습니다 제공된 코드에서 Zend_Db_Table::getDefaultAdapter() 대신

+0

감사합니다. 어딘가에이 메서드를 넣을 수 있도록 모든 모델에서 필요가 없으며 $ select = $ this -> _ getDbAdapter()를 말할 수 있습니까? – Pjn2020

+0

추상 모델을 만들어서 모든 모델 내에서 확장하거나'Zend_Db_Table :: getDefaultAdapter() '를 호출 할 수 있습니다 – yokoloko

+0

감사합니다 every1 많이 감사드립니다. – Pjn2020

0

$this->_getDbAdapter()의 전화. 사용할 필요가 있습니다 Zend_Registry::get('dbAdapter');

Application_Model_Example의 클래스는 무엇입니까?

0

내 부트 스트랩에 Zend_Db_Table::setDefaultAdapter($dbAdapter);이 있습니다 (그 Zend_Db_Table이 아니라 Zend_Db_Table_Abstract입니다).

내 모델에서 다음

, 난 그냥 모델을 Zend_Db_Table을 확장 가정

$select = $this->->select() 
    ->from(array('t' => $this->_getTable()->getName()), array('name')).... 

를 사용해야합니까?

관련 문제