Search code examples
phpprestashopsmarty

How to reinstall database on Prestahop Module?


I'm trying to customize a Prestashop module and I need to add two new colonnes in a table.

The problem is that the database is already created, I guess it did when the module initializing. So if I change the code now, changes does not appear and I keep receiving this mistake:

enter image description here

There is no way to do it in configuration and I suppose if I delete the module my code will dissepear.

How can I do ?

Maybe asking the delete and the create method in a common method (just one time).

I'm new with Smarty and PHP, I see it's not a usual method where the table is created.

Could you help me ?

I'm new with Smarty, how can I do that ? It looks like it's not a method where the table is created.


$sql = array();
$sql[] =
    'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'opartdevis` (
        `id_opartdevis` int(10) NOT NULL AUTO_INCREMENT,
        `id_shop` int(10) NOT NULL,
        `id_cart` int(10) NOT NULL,
        `id_customer` int(10) NOT NULL,
        `name` varchar(128),
        `message_visible` TEXT,
        `id_customer_thread` int(10),
        `date_add` DATETIME NOT NULL,
        `status` int(2) DEFAULT 0,
        `id_order` int(10) NULL,
        `id_ordered_cart` int(10) NULL,
        `remise` int(10),
        `delivery` int(10),
        PRIMARY KEY (`id_opartdevis`)
    ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';

//1.6.1.0 specific price bug fix
if (version_compare(_PS_VERSION_, '1.6.1.0', '=')) {
    $sql[] = "ALTER TABLE "._DB_PREFIX_."specific_price DROP INDEX id_product_2";
    $sql[] = "ALTER TABLE "._DB_PREFIX_."specific_price ADD INDEX id_product_2 (id_product,id_shop,id_shop_group,id_currency,id_country,id_group,id_customer,id_product_attribute,from_quantity,id_specific_price_rule,id_cart,`from`,`to`)";
}

foreach ($sql as $s) {
    if (!Db::getInstance()->execute($s)) {
        return false;
    }
}

Thanks in advance

Malaury


Solution

  • You need to revert all DB changes when you're uninstalling the module.

    Define (or append to existing) uninstall() function in your base module file and run DROP TABLE query on the created table. Example:

    public function uninstall() {
        if (!parent::uninstall()) {
            return false;
        }
    
        $sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'opartdevis`';
        if (!Db::getInstance()->execute($sql)) {
            return false;
        }
    
        return true;
    }
    

    This way, when you're uninstalling the module, the table is going to be deleted (and recrteated when you install it again).