Is it possible to connect to another database from Magento and accessing data?
If I need to create a module, how can I create a module to access another database? Is there any tutorial which tells about the same from scratch?
Any Idea?
databasemagento-1.7module
Is it possible to connect to another database from Magento and accessing data?
If I need to create a module, how can I create a module to access another database? Is there any tutorial which tells about the same from scratch?
Any Idea?
Best Answer
First thing you need to do is create a connection in your module's config.xml. It should look similar to the
default_setup
in your/app/etc/local.xml
. Here you can specify the host to be localhost and then set a different dbname or you can specify a different host completely. I have also used a socket before which works also.Now after this you will be able to connect to this database an perform queries as follows:
If you want to do this via a model then you can specify the
read
,write
andsetup
resources as follows. This will again be done inside theresources
node in your config.xml and you should replacetest
with what your model has been setup as.The model itself will try to find it's connection information in the function
getConnection
/app/code/core/Mage/Core/Model/Resource.php
. If you log the$name
passed in you will see values likepoll_write
,tag_write
andcms_read
where the first part matches the models section in the config.xml, in our case you would seetest_write
,test_read
ortest_setup
. If it cannot find a connection matching this then it will use the default connectionscore_read
,core_write
orcore_setup