wydydygibus Posted February 10, 2016 Share Posted February 10, 2016 Hi, if anyone know how to create new table in database? At all it is good practice? - create table for own module.Thanks. Link to comment Share on other sites More sharing options...
yaniv14 Posted February 10, 2016 Share Posted February 10, 2016 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. 2 Link to comment Share on other sites More sharing options...
wydydygibus Posted February 10, 2016 Author Share Posted February 10, 2016 Thank you very much! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now