Subesh Pokhrel

Magento Developers Blog

Magento External Database Connector Released

Formarly Known as The Three Tree External Database Connector
Magento External Database Connector module will be helpful for the “Developers” who want to connect to the external Database server (other than the database used by Magento). This module will help the developer to set up a connection object (Read & Write) with the configuration configured in Admin Settings and use that connection to do database transactions on external database.
All you have to do is set the settings of your external database connection in your Magento Settings and extend your model/collection class with the class of this module and use other codes as you have been using in Magento to select/update/delete records.
It will be as simple as Mage::getModel(‘mymodule/mymodel’)->setData(‘name’,’The Next Tree’)->save()
Here is the explanation of how you can use the External Database Connector module’s classes to define your
  1. Model
  2. Resource Model
  3. Collection
Lets take a simple example. Say you have a table (of course in an external database) named tbl_magician, which has two just two columns id and name. First thing set up an entity definition in the config.xml file of your module or you can add in this module’s config as well. [source language=”xml”] <entities> <!– Add As many tables config here –> <magician> <!– Table alias –> <table>tbl_magician</table> <!– Actual Table Name –> </magician> <!– Add As many tables config here –> </entities> [/source]

Model Defination

All of your Model that uses the external source has to be extended by Sp_Edb_Model_Abstract. If you see on this class you can find that the connection has been established based on the settings you do on Magento backend. An example for your model class will be something like this. [source language=”php”] <?php class Sp_Edb_Model_Magician extends Sp_Edb_Model_Abstract { /** * Initialize resources */ public function __construct() { parent::__construct(); $this->_init(‘edb/magician); } } [/source] If you are wondering where to set up your connection settings go to System–>Configuration–>SP EDB and add your settings there.


Resource Model

Similary your resource model should extend one another class of the module as shown below. [source language=”php”] <?php class Sp_Edb_Model_Mysql4_Magician extends Sp_Edb_Model_Mysql4_Abstract { protected function _construct() { $this->_init(‘edb/magician’, ‘id’); } } [/source]

Collection

As you might have already guessed yes..! collection also needs to be extended by one another class of the module as below [source language=”php”] <?php class Sp_Edb_Model_Mysql4_Magician_Collection extends Sp_Edb_Model_Mysql4_Collection_Abstract { /** * Local constructor * */ protected function _construct() { $this->_init(‘edb/magician’); } } [/source]
All those parent classes used are themselves extended from the classes which Magento uses for Model or Resource or Collection. So there is no any functionality missing and you can use the same old methods for insert/update or select.
After setting up all these classes you can now just freely use these functions without keeping external connection in mind. [source language=”php”] $model = Mage::getModel(‘edb/magician’); // To save $model->setName(‘The three Tree’)->save(); $model->setName(‘TTT’)->save(); // Get Collection $collection = $model->getCollection(); $collection->getSelect()->where(‘main_table.id = ?’, 1); [/source] If you have any confusion we would be happy to address that.The module is now available on Magento Connect. FAQ’s