LinkedIn Twitter RSS Reset

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.

 <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>

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.

<?php
class Sp_Edb_Model_Magician extends Sp_Edb_Model_Abstract
{
	 /**
     * Initialize resources
     */
    public function __construct()
    {
		parent::__construct();
		$this->_init('edb/magician);
     }

}


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.

<?php
class Sp_Edb_Model_Mysql4_Magician extends Sp_Edb_Model_Mysql4_Abstract
{

	protected function _construct()
    {
        $this->_init('edb/magician', 'id');
    }

}

Collection

As you might have already guessed yes..! collection also needs to be extended by one another class of the module as below

<?php
class Sp_Edb_Model_Mysql4_Magician_Collection extends Sp_Edb_Model_Mysql4_Collection_Abstract
{
	 /**
     * Local constructor
     *
     */
    protected function _construct()
    {
        $this->_init('edb/magician');
    }
}

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.

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

If you have any confusion we would be happy to address that.The module is now available on Magento Connect.

FAQ’s

30 Responses to “Magento External Database Connector Released”

  1. Attila Fulop
    April 27, 2011 at 3:28 pm #

    Thank you for this great stuff!

    However a few minor things are unclear for me.
    If you could attach a simple demo implementation project, that would be great.

    • November 9, 2011 at 8:17 am #

      That’s really shwred! Good to see the logic set out so well.

    • November 10, 2011 at 4:34 am #

      Hahahaha. I’m not too brghit today. Great post!

  2. Attila Fulop
    April 28, 2011 at 6:05 am #

    Is it normal that the Magento table prefix gets added on queries to the external connection?

    • November 9, 2011 at 7:47 pm #

      Yeah that’s what I’m talknig about baby–nice work!

  3. dice
    May 10, 2011 at 10:51 am #

    Plese tell me above step in detail .
    Which config file I need use app/etc/config.xml?
    Where is the Model Defination?
    How can I create Resource Model?
    Where I create Collection class?
    Where and how I need to check connection?

    • November 10, 2011 at 12:36 am #

      Thanks for sharing. Always good to find a real exrept.

  4. dice
    May 11, 2011 at 5:51 am #

    Ok i get it what i want to do thaks.
    But now the problem is
    When i go to System–>Configuration–>TTT EDB 404 page not not found error is coming & left side menu allso not showing.I also made changes in config.xml of that module.
    Any solution

    • admin
      May 12, 2011 at 3:42 am #

      After installation of the module please log out of the Magento admin and then view the configuration. I hope your problem will be solved. And please reply if it works. Thanks.

      • dice
        May 18, 2011 at 12:26 pm #

        thanks for reply,
        Its still showing same error.
        I am again reinstalling & log out but no chage plse help me out.
        I m still stuck on that issue.
        Or tell me any other solution for external db connection I really need it boss.
        Thanks…………..

      • November 9, 2011 at 3:51 pm #

        If you’re reaindg this, you’re all set, pardner!

      • November 9, 2011 at 6:10 pm #

        Glad I’ve finally found somhetnig I agree with!

    • November 9, 2011 at 10:29 am #

      Thanks alot – your answer solved all my prolbmes after several days struggling

  5. Anna
    July 25, 2011 at 1:30 pm #

    Hello,
    I have a magento store in which I gather different stores but my store function like a one store. Instead of uploading and updating the products I wud like to conect either to a feed either to their databases and once they change their price they will automaticly change in my store. Wud like to know if your extension does what I need.
    Thank you,Anna

    • admin
      July 25, 2011 at 2:56 pm #

      I am sorry this extension does not do that…but can be helpful if you build a module implementing this module.

      • November 10, 2011 at 1:22 am #

        You’re the one with the brains here. I’m wahtcing for your posts.

    • November 9, 2011 at 3:03 am #

      A bit surprised it seems to simple and yet uesful.

    • November 9, 2011 at 1:03 pm #

      That addresses several of my concerns acutally.

  6. October 2, 2011 at 2:40 pm #

    Thanks for the share!
    Nancy.R

  7. November 9, 2011 at 10:32 am #

    That’s raelly thinking out of the box. Thanks!

  8. November 9, 2011 at 6:54 pm #

    That’s relaly thinking out of the box. Thanks!

  9. November 14, 2011 at 5:59 am #

    Hi,

    could you tell me how to get last insert id ?

    BR,
    Isuru

  10. November 21, 2011 at 6:05 am #

    Thanks for sharing your opinion here. I really value it.

  11. November 26, 2011 at 3:33 pm #

    Hey mate! I quite agree with your opinion.

  12. December 21, 2011 at 8:48 am #

    Glad to read this blog! Keep it going!

  13. Prat
    February 13, 2012 at 8:02 pm #

    Hi Subesh,

    Thanks for the gr8 post..!!!
    but I have certain doubs as what exactly means external database?
    Can we use Oracle as an db & connect magento wih that?
    If it’s possile then plz tell me steps for the same..

    Thanks..

    • Subesh Pokhrel
      February 14, 2012 at 1:43 pm #

      Hi Prat,
      This module only works for MYSQL DB. This might be helpful with integrating Magento with external system which used MYSQL.

Trackbacks/Pingbacks

  1. TTT Edb FAQ’s | The Three Tree -Magento Developer's Workshop & Blog - May 12, 2011

    [...] First of all we would like to say that this is not a fully functional module (Don’t Panic..), meaning you will not be able work with tables of external database just by configuring the settings of the module. You will have to code your associated Model/Collection classes based on the http://www.thethreetree.com/2011/04/magento-external-database-connector-v1-0-0-released. [...]

  2. External Database Connector FAQ’s | Subesh Pokhrel's Blog - Magento Development Tips - February 7, 2012

    [...] First of all we would like to say that this is not a fully functional module (Don’t Panic..), meaning you will not be able work with tables of external database just by configuring the settings of the module. You will have to code your associated Model/Collection classes based on the Magento External Database Connector Released. [...]

  3. Magento External Database Connector v1.0.0 Released | The Three Tree -Magento Developer's Workshop & Blog - February 7, 2012

    [...] Posted by admin on Apr 12, 2011 in Free Extensions, News | 27 comments From Now Onwards This module will be managed from here http://subesh.com.np/2012/02/magento-external-database-connector-v1-0-0-released/ [...]

Leave a Comment