Magento切换数据库,使用不同数据库

Magento切换数据库,使用不同数据库

1. 在app/etc/local.xml 中,添加新的 数据库选项
<?xml version="1.0"?>
<config>
<global>
<install>
<date><![CDATA[Tue, 05 Jul 2011 03:23:52 +0000]]></date>
</install>
<crypt>
<key><![CDATA[80eb4be66ce28df745f27c75f2604d33]]></key>
</crypt>
<disable_local_modules>false</disable_local_modules>
<resources>
<db>
<table_prefix><![CDATA[]]></table_prefix>
</db>
<default_setup>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[hello]]></dbname>
<active>1</active>
</connection>
</default_setup>
<vip_space_read>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[hello_dev_test]]></dbname>
<model>mysql4</model>
<initStatements>SET NAMES utf8</initStatements>
<type>pdo_mysql</type>
<active>1</active>
</connection>
</vip_space_read>
</resources>
<session_save><![CDATA[files]]></session_save>
</global>
<admin>
<routers>
<adminhtml>
<args>
<frontName><![CDATA[hello_admin]]></frontName>
</args>
</adminhtml>
</routers>
</admin>
</config>
  2. 在需要使用的不同数据库的model resource中,重写 _setResource 方法,例如
class Hello_Vip_Model_Entity_Vip_Adapter extends Mage_Core_Model_Mysql4_Abstract
{
protected $_logFile = 'vip.adapter.log';
protected function _construct()
{
$this->_setResource(array('read' =>'vip_space_read', 'write'
=>'vip_space_read'));
}
  经过以上两步,就能在某个model中使用不同的数据库
English »
AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBengaliBelarusianBulgarianCatalanChinese (Simp)Chinese (Trad)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHebrewHindiHungarianIcelandicIndonesianIrishItalianJapaneseKannadaKoreanLaoLatinLatvianLithuanianMacedonianMalayMalteseNorwegianPersianPolishPortugueseRomanianRussianSerbianSlovakSlovenianSpanishSwahiliSwedishTamilTeluguThaiTurkishUkrainianUrduVietnameseWelshYiddish  
Options :  History :  Help :  Feedback
Text-to-speech function is limited to 100 characters

你可能感兴趣的:(Magento切换数据库,使用不同数据库)