How to create a table for admin controller prestashop

Here is how you can create a database table at the module instalation. If you have a small module you can save all settings in prestashop settings table, but if you need to save large data you need a new table only for your module. It is not so complicated to create this table, you only need to write few lines of sql where you tell to prestashop what columns should create in your table. 

The functions to create the table should look like the following:

           'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'my_table` (
                `main_id` int(100) NOT NULL AUTO_INCREMENT,
                `name` varchar(100) NOT NULL,
                `data` text NOT NULL,
                `data_new` int(100) NOT NULL,
                PRIMARY KEY (`main_id`)

When installing the module, prestashop will find the above function and it will create the table for you. Now you can access all the data from table using prestashop default functions, I will show the main methods in next tutorials, how to get data from a custom form and save them in the database using few lines of code. Very important to use _DB_PREFIX_ and not harcode the database prefix because if you want to install the module on other prestashop things can go wrong. The above function should be called when module is installed so you have to call it in install() function of your module. This is how you can create a table for your admin controller in prestashop. 

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x