So I have extended a Magento collection to obtain my own collection only I want to use a different DB connection. What I have attempted to do is define the connection in config.xml like so:
<global>
<resources>
<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[db_username]]></username>
<password><![CDATA[db_password]]></password>
<dbname><![CDATA[db_name]]></dbname>
<model>mysql4</model>
<type>pdo_mysql</type>
<active>1</active>
</connection>
</externaldb_database>
</resources>
</global>
and then in my overriding constructor, do:
class VMR_Customreports_Model_Resource_Storeoverview_Collection extends Mage_Reports_Model_Resource_Order_Collection
{
public function __construct()
{
parent::__construct();
$resource = Mage::getSingleton('core/resource');
$conn = $resource->getConnection('externaldb_read');
$this->setConnection($conn);
}
.....
}
This doesn't appear to have any effect. I would have expected an error if it didn't work. What do I do?
UPDATE: After more debugging, I note that this code below returns the original db connection and not the one I defined in my XML. Why is that?
$conn = $resource->getConnection('externaldb_read');
Best Answer
A quick check (see test case below) actually shows what you are doing should be just peachy.
For that reason, I suspect some error in your configuration. Please edit your post to include your collection class with at least it's
__construct
and_construct
methods.Also, if you are not using the
sales/order
resource model, please include it with it's constructor methods, too.UPDATE:
I just noticed you are using
$resource->getConnection('reportingdb_read');
in your constructor.In the configuration XML the connection is called
<externaldb_read>
.Note:
reportingdb_read
!=externaldb_read
Maybe thats just a copy & paste error in your post, but if not, you have to use
$resource->getConnection('externaldb_read');
If that isn't the issue, please update your post, again, and I'll help dig further.