PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, June 29, 2022

[FIXED] How to reinstall database on Prestahop Module?

 June 29, 2022     php, prestashop, smarty     No comments   

Issue

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).



Answered By - Petr Hejda
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing