3

I've been following this thread but I couldn't get it to work, How do I get my Collection to use a different DB connection?.

I'm trying to load a collection from an external database separate from the main magento database.

config.xml:

<otherdb_setup>
    <connection>
        <host><![CDATA[127.0.0.1]]></host>
        <username><![CDATA[root]]></username>
        <password><![CDATA[]]></password>
        <dbname><![CDATA[otherdb]]></dbname>
        <model>mysql4</model>
        <initstatements>SET NAMES utf8</initstatements>
        <type>pdo_mysql</type>
        <active>1</active>
    </connection>
</otherdb_setup>
<otherdb_write>
    <connection>
        <use>otherdb_setup</use>
    </connection>
</otherdb_write>
<otherdb_read>
    <connection>
        <use>otherdb_setup</use>
    </connection>
</otherdb_read>

Then on my collection file (class Custom_Module_Model_Mysql4_Mod_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract):

public function _construct()
{
    $this->_init('custom_module/mod');

    $resource = Mage::getSingleton('core/resource');
    $conn = $resource->getConnection('otherdb_read');

    $this->setConnection($conn);
}

I'm stuck on this part.

1

If you take a loiok at Mage_Catalog_Model_Resource_Product for an example, you will see that they use __construct():

/**
 * Initialize resource
 */
public function __construct()
{
    parent::__construct();
    $this->setType(Mage_Catalog_Model_Product::ENTITY)
         ->setConnection('catalog_read', 'catalog_write');
    // ...

I suggest you emulate that in your model:

public function __construct()
{
    parent:__construct();
    $this->setConnection('otherdb_read', 'otherdb_write');
}

I'd also suggest you implement the unit test that Vinai provided as an example into your framework to ensure it's configured correctly for you.

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.