Monday, September 19, 2011

Magento mysql table install, create and upgrade scripts using custom module

Here is the code to create or update magento mysql table. This module helps to create new table.
"app/etc/modules/Company_Track.xml"

  
      
          true
          local
      
  

"app/code/local/Company/Track/etc/config.xml"

   
    
        0.1.0
   


    
       
            
                Company_Track
                Company_Track_Model_Mysql4_Setup
                
  
                    core_setup
                
        
    



"app/code/local/Company/Track/Model/Mysql/Setup.php"
class Company_Track_Model_Mysql4_Setup extends Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
{
}
Here is the code to create table query.
"app/code/local/Company/Track/sql/track_setup/mysql4-install-0.1.0.php"
$installer = $this;
 
$installer->startSetup();
 
$installer->run("
 
-- DROP TABLE IF EXISTS {$this->getTable('customer_track')};
CREATE TABLE IF NOT EXISTS {$this->getTable('customer_track')} (
`id` INT UNSIGNED NOT NULL ,
`sku` varchar(64) NOT NULL ,
`customer_id` INT NOT NULL ,
`region` varchar(255) NOT NULL ,
`country_id` char(2) NOT NULL
) ;

"); 
$installer->endSetup();
If you want to update mysql table using update query you have to use the below coding. Here im going to remove last name required option to not required.
"app/code/local/Company/Track/sql/track_setup/mysql4-upgrade-0.1.0-0.1.1.php"
/* @var $eav Mage_Eav_Model_entity_Setup */
$eav = Mage::getModel('eav/entity_setup', 'core_setup');
$eav->updateAttribute('customer_address', 'lastname', 'is_required', false);

No comments:

Post a Comment