2010-02-01 5 views
1

내 젠드 프레임 워크 모듈 중 하나에서 다른 데이터베이스와 다른 데이터베이스를 사용하고 싶습니다. The Manual suggestsapplication.ini의 리소스 앞에 모듈 이름을 접두사로 붙이면이 문제를 해결할 수 있지만 제대로 작동하지는 않습니다.Zend Framework의 모듈 별 데이터베이스 설정

application.ini의 관련 비트는 다음과 같습니다 terms 모듈의 이름입니다

 
resources.modules[] = "" 

resources.db.adapter = "PDO_MYSQL" 
resources.db.params.host = "localhost" 
resources.db.params.dbname = "maindb" 
resources.db.params.username = "dbuser" 
resources.db.params.password = "..." 
resources.db.params.charset = "utf8" 

terms.resources.db.params.dbname = "termsdb" 

.

이 작업을 수행하기 위해 부트 스트랩에 넣을 특별한 것이 있습니까?

답변

0

편집 : 아래 OP 최초의 코멘트에 관해서

...

내가 모듈 DB 리소스 설정에 정성 들여 질수 없음.하지 내가 제공 한 것은 내가 믿는 이론에서 작동해야한다. 만약 내가 그 리소스를 사용하지 말아야 elfted Zend_Application_Resource_Db의 확장에 수정해야할지 모르겠다. 고유 한 연결 이름을 기반으로 db db 연결을 가져올 수있는 자체 사용자 지정 리소스가 있습니다.

class MyLib_Db extends Zend_Db 
{ 
    protected $_instance = null; 
    protected $_connections = array(); 

    /** 
    * Standard Zend Framework unified constructor 
    * @param null|array An array of options that will be passed to setOptions 
    */ 
    public function __construct($options = null) 
    { 
    } 

    /** 
    * Standard Zend Framework setOptions implementation 
    * @param array $options and array of options from config 
    * @return MyLib_Db 
    */ 
    public function setOptions(array $options) 
    { 
    } 

    /** 
    * Set the class instance 
    * @param MyLib_Db 
    * @return MyLib_Db 
    */ 
    public static function setInstance($instance) 
    { 
    } 

    /** 
    * Get/create the singleton instance 
    * @return MyLib_Db 
    */ 
    public static function getInstance() 
    { 
    } 

    /** 
    * Get a Zend_Db adapter Instance by unique name 
    * Searches self::$_connections for the $name param as an array key 
    * @param String $name unique connection name 
    * @return Zend_Db_Adpater_Abstract|null 
    */ 
    public function getConnection($connectionName) 
    { 
    } 

    /** 
    * Add a connection instance to the registry 
    * Adds/creates an Zend_Db_Adapter instance to the connection registry with 
    * the string key provided by $name. If $connection is an array|Zend_Config it 
    * should match the format used by Zend_Db::factory as it will be passed to this 
    * function. If $name is null then the database name will be used. 
    * @param Zend_Db_Adapter_Abstract|Zend_Config|array The connection to register 
    * @param string|null $name A unique name for the connection 
    * @return MyLib_Db 
    */ 
    public function addConnection(Zend_Db_Adapter_Abstract $connection, $name = null) 
    { 
    } 

    /** 
    * Remove/Destroy the specified connection from the registry 
    * @param string $name the connection name to remove 
    * @return MyLib_Db 
    */ 
    public function removeConnection($name) 
    { 
    } 
} 

그래서 기본적으로 DB에 대한 내 응용 프로그램 리소스를 생성하고 선행하는의 인스턴스를 반환 : 그러나 나는 그래서 그것을 이런 식으로 뭔가를 보이는 Zend_Db를 확장하는 MyLib_Db 클래스가 그 :-)

에 ellaborate 수 있습니다 수업. 생성하는 동안 구성에 설정 한 모든 어댑터를 만들고이 클래스 인스턴스에 이름을 등록합니다 (또한 Zend_Db_Table과 함께 사용할 기본 플래그를 찾고 일부 다른 작업을 수행함).

그런 다음 MyLib_Db::getInstance()->getConnection($name); 을 사용하거나 부트 스트랩에서 MyLib_Db 인스턴스를 얻은 다음 getConnection을 호출하십시오.

개인적으로 나는이 방법을 선호합니다. 연결이 넓어 지거나 더 유연한 재사용이 가능한 특정 모듈에 연결되어 있지 않기 때문입니다. 저의 Zend 프로젝트 대부분이 Zend_Db 대신 Doctrine을 사용했기 때문에 저는 실제로이 프로젝트를 몇 개의 프로젝트에만 사용했다고 말했습니다.


이 사실 난 당신이 modules.terms.resources.db.* 같은 설정에서 모듈의 섹션에 그것을 넣어 필요가 있다고 생각 :-) 도움이

희망. 이렇게하면 모듈 부트 스트랩에로드됩니다. 또는 Terms_Bootstrap_initDb으로 설정할 수 있습니다.

개인적으로 나는 특별한 DB 관리 클래스를 사용하고 대신 내 리소스에 넣습니다. 단일 또는 다중 어댑터 설정을 처리합니다. 다음에 $db이 자원 배열에서 검색된 관리자임을 나타냅니다.

$dbAdapter = $db->getConnection('terms');

+0

의 경우에 알려 주시기 바랍니다. 부트 스트랩 내에서 설정하는 방법에 대해 자세히 설명해 주시겠습니까? – Tamlyn

+0

@ Tamlin : 위의 편집을 참조하십시오. – prodigitalson

0

은 다음처럼 :

moduleName.host = "localhost" 
moduleName.username = "user" 
moduleName.password = "pass" 
moduleName.dbname = "dbname" 
moduleName.charset = "utf8" 

는 다음과 같은 클래스를 생성 모듈의 model/DbTable/ 폴더에

$params = $GLOBALS['application']->getOption('moduleName'); 
$dbAdapter = Zend_Db::factory('PDO_MYSQL', array(
    'host' => $params['host'], 
    'dbname' => $params['dbname'], 
    'username' => $params['username'], 
    'password' => $params['password'], 
    'charset' => $params['charset'], 
)); 
Zend_Registry::set('moduleSpecific', $dbAdapter); 

그리고 어떤 init 함수에서 모듈의 Bootstrap.php에 다음을 추가 Zend_Db_Table_Abstract 대신 ModuleName_Model_DbTable_Tablemain을 확장하여 테이블 준비를하면 준비가 완료됩니다.

class ModuleName_Model_DbTable_Tablemain extends Zend_Db_Table_Abstract 
{ 
    /** 
    * 
    * wrapper class constructor used to set db adaptor on the fly 
    * @author Krishan 
    */ 
    function __construct(){ 

     parent::__construct($adapter = Zend_Registry::get('moduleSpecific')); 

    } 
} 

날 첫 번째 제안은 나를 위해 작동하지 않는 문제

관련 문제