Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Connection with second database
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 

Hi,

I need to read some data from other database (not magento db).
So I made following:

In app/etc/local.xml:

<config>
[...]
    
<global>
    
[...]
        
<resources>
        
[...]
            
<second_setup>
                <
connection>
                    <
host><![CDATA[localhost]]></host>
                    <
username><![CDATA[root]]></username>
                    <
password><![CDATA[]]></password>
                   <
dbname><![CDATA[other_db]]></dbname>
                   <
active>1</active>
               </
connection>
           </
second_setup>
[...]

In In app/etc/config.xml

<config>
    <global>
    
[...]
        
<resources>
        
[...]
             
<second_setup>
                <
connection>
                    <
host>localhost</host>
                    <
username></username>
                    <
password/>
                    <
dbname>other_db</dbname>

                    <
model>mysql4</model>
                    <
initStatements>SET NAMES utf8</initStatements>
                    <
type>pdo_mysql</type>
                    <
active>0</active>
                </
connection>
            </
second_setup>
            
[...]
            
<second_read>
                <
connection>
                    <use>
second_setup</use>
                </
connection>
            </
second_read>

            <
secondcore_read>
                <
connection>
                    <use>
second_read</use>
                </
connection>
            </
secondcore_read>
[...]

and then when I call in code:

$read Mage::getSingleton('core/resource')->getConnection('secondcore_read');
$select $read->select()->from('table_name' );

$result $read->fetchAll($select);

then I receive error message, that “magentodb.table_name” doesn’t exist. It still tries to get from magento db, not from other one.

I didn’t work so I added to Checkout/etc/config.xml:

<config>
[...]
    
<global>
    
[...]
        
<resources>
        
[...]
            
<checkoutsecond_read>
                <
connection>
                    <use>
secondcore_read</use>
                </
connection>
            </
checkoutsecond_read>
[...]

and I changed the code:

$read Mage::getSingleton('core/resource')->getConnection('checkoutsecond_read');
$select $read->select()->from('table_name' );

$result $read->fetchAll($select);

but the result was exactly this same.

What do I make wrong ?

I use magento 1.1.6

with best regards,
Rafal

 
Magento Community Magento Community
Magento Community
Magento Community
 
OSdave
Member
 
Total Posts:  62
Joined:  2007-09-01
Granada, Spain
 

hi Rafal,
Have you been able to connect to another db?
I need to do that as well and was wondering if you could point me into the good direction…
cheers

 
Magento Community Magento Community
Magento Community
Magento Community
 
LindyKyaw
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2008-01-02
Los Angeles
 

Please try following in your ResourceModel constructor:

protected function _construct()
{
$this->_resourcePrefix = ‘’;
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
OSdave
Member
 
Total Posts:  62
Joined:  2007-09-01
Granada, Spain
 

hi Lindy, thanx for answering.
I’m gonna search and try this and I’ll keep you posted.

 
Magento Community Magento Community
Magento Community
Magento Community
 
OSdave
Member
 
Total Posts:  62
Joined:  2007-09-01
Granada, Spain
 

well, I’m completely stuck.
This is what I’ve done so far:
I’ve created a new module with the module creator, filling the conection’s resource with the data of the 2nd database.
I made a simple method that get some info from the 2nd db and return it, but it throws errors.
I’ve put the $this->_resourcePrefix = ‘’; in the resourceModel constructor, but it doesn’t seems to change anything.
If anyone feels like giving more info, that would be an awesome christmas present!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top