Mohammad Adnan
Mohammad Adnan

Reputation: 77

magento connections to two databases

I have a problem in magento. i want to connect two databases with magento.one database will be the main database and the other will be for stores. I dont know how to do it.By this time my connection is in the file app/etc/local.xml.....my local.xml is follows please help false [mysql4]]> 1

Upvotes: 3

Views: 2204

Answers (1)

Zachary Schuessler
Zachary Schuessler

Reputation: 3682

There may be a more elegant solution that what I've implemented, but my method works. I did this for an osCommerce import/export module.


/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 -->

This gives you the ability to call oscommercedb within your models. The above code goes within the <resources> block.

Let's take a look at a model now.


/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);
}

If you run into a specific problem let me know.

Upvotes: 2

Related Questions