Create Own Module in Magento – Part -3 – How to call Mysql Table in magento

magento-banner
Table is created but how read & write the value into DB:
Once table is create you to read & write into your database table. For that how to use your table. Confic file code refer Part-1
  1. Simple way (simple direct query ) wherever you want to read & write then call Database connection and read & write the value to DB.
Create resource:
$resource = Mage::getSingleton(‘core/resource’);
For read the value from Database
$readConnection = $resource->getConnection(‘core_read’);
For write the value to DB
$writeConnection = $resource->getConnection(‘core_write’);
Ex: Select Query
$resource = Mage::getSingleton(‘core/resource’);
$readConnection = $resource->getConnection(‘core_read’);
$query = ‘SELECT * FROM ‘ . $resource->getTableName(‘jute_travelinfo’);
$results = $readConnection->fetchAll($query);
 Update query:
$table = ‘jute_travelinfo’;
$no = 101911;
$eid = 1;
$query = “UPDATE {$table} SET travelinfo_number = ‘{$no}’ WHERE entity_id = “
. (int)$eid;
$writeConnection->query($query);
  1. Using Magento getModel():
This is the proper way to read & write the value into DB in magento. Here i want to access my table value using below way.
getModel(“travelinfo/travelinfo”)
For this we have to create some model file as like below
Jute/Travelinfo/Model/Travelinfo.php
In this file, you have to declare you getmodel like
class Jute_Travelinfo_Model_Travelinfo extends Mage_Core_Model_Abstract
{
            protected function _construct(){
            parent::_construct();
             $this->_init(“travelinfo/travelinfo”);
   }
}
Creat Setup file
Jute/Travelinfo/Model/ Resource/Mysql4/Setup.php
class Jute_ Travelinfo_Model_Resource_Mysql4_Setup extends Mage_Core_Model_Resource_Setup {
}
 
Yes now you installation and setup is ready to use your table. But based on what you have to access ? Here am going to access my database table values based on my Primary key value. Here primary key is an entity_id. For this we have to mention in our model file
Jute/Travelinfo/Model//Mysql4/Travelinfo.php
 class Jute_Travelinfo_Model_Mysql4_Travelinfo extends Mage_Core_Model_Mysql4_Abstract
{
            protected function _construct()
   {
       $this->_init(“travelinfo/travelinfo”, “entity_id”);
   }
}
yes now based on my table primary key value i can access my data from table. But how you want to get the collection. For that we have to mention our collection into collection.php file
Jute/Travelinfo/Model//Mysql4/Travelinfo/ Collection.php
class Jute_Travelinfo_Model_Mysql4_Travelinfo_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
            public function _construct(){
                           parent::_construct();
                                    $this->_init(“travelinfo/travelinfo”);
                        }
}
 Now you table collection is ready to read & write to use. You have to access you table like
getModel(‘travelinfo/travelinfo’)

…………………………………………………………………………………………………………………………………………………………………………………….

Corner of Blog :

“Save Water”

“A drop of water is worth more than a sack of gold to a thirsty man”

…………………………………………………………………………………………………………………………………………………………………………………….