mysqlmagento

Can Magento Connect to Another MySQL database?


I was wondering if it is possible for Magento have a connection to its primary database, as well as another database that does not contain Magento's core information?

For example, I would like to be able to query tables from a WordPress install that is stored on a different database on a different server?

My first instinct was to create a new database connection using mysql_connect, but it does not feel right to do it this way.

Is there a more "proper" way to accomplish this?


Solution

  • here the complete explanation: http://fishpig.co.uk/magento-tutorials/create-an-external-database-connection-in-magento
    in short, you need to create a new resource and tell magento to use this resource for your model, ie, in the config.xml, inside <global> tag:

        <resources>
            <external_db>
                <connection>
                    <host><![CDATA[host]]></host>
                    <username><![CDATA[username]]></username>
                    <password><![CDATA[password]]></password>
                    <dbname><![CDATA[dbname]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </external_db>
            <yourmodelalias_read>
                <connection>
                    <use>external_db</use>
                </connection>
            </yourmodelalias_read>
        </resources>