2

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');

  • Is the collection class based on Mage_Core_Model_Resource_Db_Collection_Abstract or Mage_Eav_Model_Entity_Collection_Abstract? – Vinai Nov 26 '13 at 22:25
  • My Collection extends Mage_Reports_Model_Resource_Order_Collection. One sec and I'll try to dig down to see if it extends from either of those. – user2045 Nov 26 '13 at 22:28
  • It extends from Mage_Core_Model_Resource_Db_Collection_Abstract – user2045 Nov 26 '13 at 22:31
2

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();
    }
}
  • I updated my original post to include the whole constructor. As far as I know I am using the sales/order resource model because I am just rewriting the query but still using the order table. If there's something else you need, please let me know. Thank you. – user2045 Nov 26 '13 at 23:34
  • Updated my answer. – Vinai Nov 27 '13 at 6:43
  • Yeah sorry, that isn't the issue. I just didnt copy properly. I updated the OP. Please continue to help me. – user2045 Nov 27 '13 at 16:08
  • Also, having done a bit of debbugging, it looks like my collection's $this->_conn doesn't change upon calling $this->setCollection() – user2045 Nov 27 '13 at 18:06

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.