1

I have 3 databases (in addition to the magento one) connected to magento with models (non-eav) defined for the tables.

They all have their read and write connections defined in the local.xml

        <cloudvm_db>
            <connection>
                <host><![CDATA[SERVER]]></host>
                <username><![CDATA[USER]]></username>
                <password><![CDATA[PASS]]></password>
                <dbname><![CDATA[cloudvm]]></dbname>
                <model>mysql4</model>
                <type>pdo_mysql</type>
                <active>1</active>
            </connection>
        </cloudvm_db>
        <cloudvm_read>
            <connection>
                <use>cloudvm_db</use>
            </connection>
        </cloudvm_read>
        <cloudvm_write>
            <connection>
                <use>cloudvm_db</use>
            </connection>
        </cloudvm_write>

then this is defined as the connection for the model in the modules config.xml

<models>
    <cloud_workervm>
      <class>Cloud_WorkerVM_Model</class>
      <resourceModel>cloud_workervm_resource</resourceModel>
    </cloud_workervm>
    <cloud_workervm_resource>
      <class>Cloud_WorkerVM_Model_Resource</class>
      <entities>
        <virtualmachine>
          <table>virtualmachine</table>
        </virtualmachine>
      </entities>
    </cloud_workervm_resource>          
  </models>
  <resources>
    <cloud_workervm_write>
      <connection>
        <use>cloudvm_write</use>
      </connection>
    </cloud_workervm_write>
    <cloud_workervm_read>
      <connection>
        <use>cloudvm_read</use>
      </connection>
    </cloud_workervm_read> 
  </resources>

what I am trying to do is write a function(s) that you can pass the name of a model e.g. "cloud_workervm/cloudvm" and return the name of (or an object of) the read connection and the write connection

0

Please follow bellow steps

Config.xml

    <?xml version="1.0"?>
<config>
    <modules>
        <AR_Externaldb>
            <version>0.1.0</version>
        </AR_Externaldb>
    </modules>
    <frontend>
        <routers>
            <externaldb>
                <use>standard</use>
                <args>
                    <module>AR_Externaldb</module>
                    <frontName>externaldb</frontName>
                </args>
            </externaldb>
        </routers>
        <layout>
            <updates>
                <externaldb>
                    <file>externaldb.xml</file>
                </externaldb>
            </updates>
        </layout>
    </frontend>
    <admin>
        <routers>
            <externaldb>
                <use>admin</use>
                <args>
                    <module>AR_Externaldb</module>
                    <frontName>externaldb</frontName>
                </args>
            </externaldb>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <externaldb>
                    <file>externaldb.xml</file>
                </externaldb>
            </updates>
        </layout>
    </adminhtml>
    <global>
        <models>
            <externaldb>
                <class>AR_Externaldb_Model</class>
                <resourceModel>externaldb_mysql4</resourceModel>
            </externaldb>
            <externaldb_mysql4>
                <class>AR_Externaldb_Model_Mysql4</class>
                <entities>
                    <vehicle><table>BaseVehicle</table></vehicle>
                </entities>
            </externaldb_mysql4>
        </models>
        <resources>
            <!-- End DB -1 --> 
            <externaldb_write>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_write>
            <externaldb_read>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_read>
            <externaldb_setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </externaldb_setup>
            <externaldb_database>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[testsql]]></username>
                    <password><![CDATA[test123]]></password>
                    <dbname><![CDATA[database-1]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </externaldb_database>
            <!-- End DB-1 --> 
            <!-- DB -2 -->
            <pcdb_write>
                <connection>
                  <use>pcdb_database</use>
                </connection>
            </pcdb_write>
            <pcdb_read>
                <connection>
                  <use>pcdb_database</use>
                </connection>
            </pcdb_read>
            <pcdb_setup>
                <connection>
                  <use>core_setup</use>
                </connection>
            </pcdb_setup>
            <pcdb_database>
                <connection>
                  <host><![CDATA[localhost]]></host>
                  <username><![CDATA[test]]></username>
                  <password><![CDATA[test123]]></password>
                  <dbname><![CDATA[database-2]]></dbname>
                  <model>mysql4</model>
                  <type>pdo_mysql</type>
                  <active>1</active>
                </connection>
            </pcdb_database>
            <!-- End DB -2 -->
        </resources>
        <blocks>
            <externaldb>
                <class>AR_Externaldb_Block</class>
            </externaldb>
        </blocks>
        <helpers>
            <externaldb>
                <class>AR_Externaldb_Helper</class>
            </externaldb>
        </helpers>
    </global>
</config>

Bellow Read & write function use in model or resource

<?php
class AR_Externaldb_Model_Test extends Mage_Core_Model_Abstract
{
protected $_readAdaptor;
protected $_readPcdbAdaptor;
protected $_writeAdaptor;

public function readAdaptor()
{
    if(empty($this->_readAdaptor)) {
        $this->_readAdaptor = Mage::getSingleton('core/resource')->getConnection('externaldb_read');
    }
    return $this->_readAdaptor;
}

public function writeAdaptor()
{
    if(empty($this->_writeAdaptor)) {
        $this->_writeAdaptor = Mage::getSingleton('core/resource')->getConnection('externaldb_write');
    }
    return $this->_writeAdaptor;
}

public function readPcdbAdaptor()
{
    if(empty($this->_readPcdbAdaptor)) {
        $this->_readPcdbAdaptor = Mage::getSingleton('core/resource')->getConnection('pcdb_read');
    }
    return $this->_readPcdbAdaptor;
}

}

  • the layout update and router is unnecessary. I added a getReadAdapter and getWriteAdapter to each model resource in the end, I was trying to avoid having to modify each class. There must be a way to get it from the config xml, – Conrad Jones Nov 6 '15 at 20:52
2

For getting resource read collection try this:

Mage::getResourceModel('cloud_workervm/virtualmachine')->getReadConnection()

Format:

Mage::getResourceModel('ModelPrefix/entityname')->getReadConnection()

For getting resource write collection try this:

Mage::getResourceModel('cloud_workervm/virtualmachine')->getConnection('write')

Format:

Mage::getResourceModel('ModelPrefix/entityname')->getConnection('write')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.