How do I get my Collection to use a different DB connection?
-
16-10-2019 - |
Pergunta
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');
Solução
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.
// Test to confirm a connection set via setConnection() on a
// Mage_Reports_Model_Resource_Order_Collection instance is used
// to load the collection data.
class Reports_CollectionTest extends PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
require_once 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app('admin');
}
protected function getConnectionMock()
{
$mockSelect = $this->getMockBuilder('Varien_Db_Select')
->disableOriginalConstructor()
->getMock();
$mockConn = $this->getMockBuilder('Varien_Db_Adapter_Pdo_Mysql')
->disableOriginalConstructor()
->getMock();
$mockConn->expects($this->once())
->method('select')
->will($this->returnValue($mockSelect));
$mockConn->expects($this->once())
->method('fetchAll');
return $mockConn;
}
/**
* @test
*/
public function testCollectionConnection()
{
$mockCon = $this->getConnectionMock();
$collection = new Mage_Reports_Model_Resource_Order_Collection;
$collection->setConnection($mockCon);
$this->assertSame($mockCon, $collection->getConnection());
$collection->load();
}
}