magento项目中使用多个数据库的方法

1. 在app/etc/local.xml 中,添加新的数据库选项


<?xml version="1.0"?>
<!--
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   Mage
 * @package    Mage_Core
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.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中使用不同的数据库


[文章标题] magento项目中使用多个数据库的方法

[文章作者]曾健生

[作者邮箱][email protected]

[作者QQ]190678908

[博客]  http://blog.csdn.net/newjueqi









你可能感兴趣的:(数据库,function,session,File,resources,Magento)