2016-08-22 2 views
0

나는 다음 XML이 :config.xml에 변수를 추가 할 수 있습니까?

내가 admin_core_data에서 (호스트, 사용자 이름, 암호 및 DBNAME) 등의 모든 정보를 추가하고 외부 데이터베이스에 연결을 읽고 싶은
<config> 
    <global> 
     <resources> 
     <dbresource> 
      <connection> 
       <host><![CDATA[localhost]]></host> 
       <username><![CDATA[root]]></username> 
       <password><![CDATA[1234]]></password> 
       <dbname><![CDATA[db_test]]></dbname> 
      </connection> 
     </dbresource> 
     ... 
    ... 

. system.xml을 사용하여 admin_core_data에 데이터를 쓰거나 읽는 방법을 알고 있습니다.

config.xml을 사용하여 외부 데이터베이스에 연결할 수 있지만 직원이 "admin/system/configuration"에서 쉽게 변경할 수 있도록해야합니다. XML 파일을 편집 할 수 없기 때문입니다.

또는 다른 어떤 아이디어들,이 상황에서 무엇을해야합니까?

조언이 있으십니까?

고맙습니다.

+0

아래 참조 걸릴 수 있습니다. config.xml에서 기본값을 정의 할 수 있습니다.이 매개 변수를 사용하여 연결하는 클래스는'Mage :: getStoreConfig ('path/to/config')'를 사용하여 값을 읽고이를 사용하여 데이터베이스 연결 인스턴스를 만들어야합니다 –

+0

config.xml 대신 local.xml을 사용하여 외부 데이터베이스에 연결 하시겠습니까? –

답변

0

저장소 구성을 가져 오는 Magento 표준 방법을 사용할 수 있습니다.

$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName'); sectionName, groupName 및 fieldName은 모듈의 etc/system.xml 파일에 정의되어야합니다.

위의 코드는 현재 표시된 저장소의 구성 값을 자동으로 가져옵니다.

일부 system.xml 파일이 Magento에서 어떻게 정의되어 있는지 확인하여 쉽게 모듈에 추가 할 수 있습니다.

+0

감사합니다. 지금 시험하겠습니다. –

1

기능을 구현할 수있는 방법이 약간 다릅니다. 새 system.xml을 추가 한 다음 madel 파일에서 데이터베이스 구성을 업데이트해야합니다.

const XML_CONFIG_EDB_HOST = 'edb_settings/dbconnection/host'; 
const XML_CONFIG_EDB_DBNAME = 'edb_settings/dbconnection/dbname'; 
const XML_CONFIG_EDB_USERNAME = 'edb_settings/dbconnection/username'; 
const XML_CONFIG_EDB_PASSWORD = 'edb_settings/dbconnection/password'; 
const EXTERNAL_RESOURCE_NAME = 'edb_connection'; 

/* @var array */ 

protected $_config; 

/* @var Varien_Db_Adapter_Pdo_Mysql */ 
protected $_connection; 

public function __construct() { 
    $this->_setConnection(); 
} 

/** 
* Gets the Config Settings in the Admin Settings for DB Connection 
* @return array 
*/ 
public function getConfig() { 
    if (!$this->_config) { 
     $this->_config = array(); 
     // Setting the Values from the Admin Config 
     $this->_config['host'] = Mage::getStoreConfig(self::XML_CONFIG_EDB_HOST); 
     $this->_config['dbname'] = Mage::getStoreConfig(self::XML_CONFIG_EDB_DBNAME); 
     $this->_config['username'] = Mage::getStoreConfig(self::XML_CONFIG_EDB_USERNAME); 
     $this->_config['password'] = Mage::getStoreConfig(self::XML_CONFIG_EDB_PASSWORD); 

     // Setting the default Values 
     $this->_config['initStatements'] = 'SET NAMES utf8'; 
     $this->_config['model'] = 'mysql4'; 
     $this->_config['type'] = 'pdo_mysql'; 
     $this->_config['pdoType'] = ''; 
     $this->_config['active'] = '1'; 
    } 
    return $this->_config; 
} 

/** 
* Gets External DB Connection Resource 
* 
* @return Varien_Db_Adapter_Pdo_Mysql 
*/ 
public function getConnection() { 
    if (!$this->_connection) { 
     $this->_setConnection(); 
    } 
    return $this->_connection; 
} 

private function _setConnection() { 
    if (!$this->_connection) { 
     $this->_connection = Mage::getSingleton('core/resource')->createConnection(self::EXTERNAL_RESOURCE_NAME, 'pdo_mysql', $this->getConfig()); 
    } 
} 

public static function getConnectionName() { 
    return self::EXTERNAL_RESOURCE_NAME; 
} 

당신은 당신이 각 매개 변수에 대한 system.xml을 항목을 추가해야이 작업을 수행하려면 링크 External database configuration

+0

고맙습니다. 지금 시험하겠습니다. –

+0

작은 문제가 생겼으니 이제 magento가 2 개의 연결을 만듭니다. config.xml 및 모델 자체에서 가져옵니다. 어떻게 구성에서 연결을 제거 할 수 있습니까? 자원으로 무엇을 부를 필요가 있습니까? ? –

관련 문제