与两个数据库的magento连接

时间:2012-04-17 10:23:27

标签: php magento

我在magento有问题。我想用magento.one数据库连接两个数据库将是主数据库,另一个将用于商店。我不知道怎么做。这次我的连接是在文件app / etc / local.xml中.....我的local.xml如下 请帮忙                                                                                                               假                                                                                                                                                                                              [mysql4]] GT;                                                               1                              

1 个答案:

答案 0 :(得分:2)

可能有一个更优雅的解决方案,我已经实现了,但我的方法有效。我为osCommerce导入/导出模块做了这个。


/httpdocs/app/etc/config.xml

        <!-- osCommerce db/read/write -->
        <oscommercedb>
            <connection>
                <host>localhost</host>
                <username>root</username>
                <password>pass</password>
                <dbname>oscommerce_database_name</dbname>
                <model>mysql4</model>
                <initstatements>SET NAMES utf8</initstatements>
                <type>pdo_mysql</type>
                <active>1</active>
            </connection>
        </oscommercedb>
        <oscommercedb_write>
            <connection>
                <use>oscommercedb</use>
            </connection>
        </oscommercedb_write>
        <oscommercedb_read>
            <connection>
                <use>oscommercedb</use>
            </connection>
        </oscommercedb_read>
        <!-- end osCommerce db -->

这使您能够在模型中调用oscommercedb。上面的代码在<resources>块内。

现在让我们来看看模型。


/httpdocs/app/code/local/Company/Extension/Model/OsCustomers.php

class Company_Extension_Model_OsCustomers extends Mage_Core_Model_Abstract
{

protected $_name = 'customers'; // name of the table

/**
 * Returns rowset of tables for customers
 *
 * @return Zend_Db_Table_Rowset
 */
public function getAllOscommerceCustomers()
{
    $read = Mage::getSingleton('core/resource')->getConnection('oscommercedb');

    $stmt = $read->select();

    $stmt->from(array('c' => 'customers'))
         ->join(array('a' => 'address_book'), 'a.address_book_id = c.customers_default_address_id')
         ->joinLeft('zones', 'zones.zone_id = a.entry_zone_id')
         ->join('countries','a.entry_country_id = countries.countries_id', array('countries_iso_code_2'));

    return $read->fetchAll($stmt);
}

如果您遇到特定问题,请与我联系。