Jump to content

Create Table In Db


wydydygibus

Recommended Posts

It's very common to create DB tables for your module in case you need it.

If your module only need to store configurations you can use ps_configuration table.

If you need DB tables for your module its best to run sql query during module installation process.

 

for example:

 

public function install()
        {
            // Run sql for creating DB tables
            Db::getInstance()->execute('
                       'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'table_name` (
                                `id_something` int(11) unsigned NOT NULL AUTO_INCREMENT,
                                `id_product` INT( 11 ) UNSIGNED NOT NULL,
                                `some_field` varchar(255) NOT NULL,
                                PRIMARY KEY (`id_something`),
                                UNIQUE  `SOMETHING_UNIQ` (  `id_product` )
                                ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
            '); 
 
            return parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('displayHome');
        }  
 
Also its recommended to add delete table function inside the uninstall function.
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...