仅在安装模块时在现有 Prestashop 数据库中添加字段

Add a field in existing Prestashop database only when a module is installed

我会在我的 Prestashop 数据库的客户 table 中添加一个字段,但只有在我安装模块时才会这样做。

我在我的模块定义中做到了:

public function install()
{
    Db::getInstance()->execute('ALTER TABLE '. _DB_PREFIX_.'_customer ADD id_field VARCHAR(60) DEFAULT NULL');

    return parent::install();
}

public function uninstall()
{
    Db::getInstance()->execute('ALTER TABLE '. _DB_PREFIX_.'_customer DROM COLUMN id_field');

    return parent::uninstall();
}

现在,我看到我们可以覆盖 class 但我找不到如何在我的模块文件夹中覆盖它。例如,我会在客户 class 中添加新字段,以便能够在我的模块中对其进行管理。

我该怎么办?

在您的模块文件夹中添加 override/classes/Customer.php 代码:

<?php
class Customer extends CustomerCore
{
    // the method etc. i. e.:
    public function __construct($id = null)
    {
        parent::__construct($id);

        if (Module::isInstalled('yourmodule') && Module::isEnabled('yourmodule'))
        {
             // your code
        }
    }
}

并重置模块。