1
votes

I am currently developing a module for prestashop but I can not find information regarding the registration of data in database. Do you have any idea how to do this?

I started by creating my form in my controller like this:

/**
 * Assign template vars related to page content
 * @see FrontController::initContent()
 */
public function initContent()
{
    parent::initContent();

    $this->setTemplate('upload.tpl');
}

public function renderForm()
{
    $fields_form = array(
        'form' => array(
            'input' => array(
                array(
                    'type' => 'file',
                    'label' => $this->l('Insert file here.'),
                    'name' => 'FILE_NAME',
                    'required' => true
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
            )
        ),
    );

    $helper = new HelperForm();
    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = 1;
    $helper->fields_value['FILE_NAME'] = "";
    $this->fields_form = array();
    $helper->submit_action = 'btnSubmit';

    return $helper->generateForm(array($fields_form));
}

Thank you

2

2 Answers

0
votes

Actually you have to handle response from the form yourself. Short answer is "do something like this in your module PHP main file" :

 protected function postProcess()
 {
    $errors = array();

    if (Tools::isSubmit('submitFormName'))
    {
        if (!Configuration::updateValue('FORM_INPUT_NAME', Tools::getValue('FORM_INPUT_NAME')))
            $errors[] = $this->l('Cannot update settings');
    } 
  }

Configuration::updateValue($key, $value) stores simple strings in DB. You can add some features (see Configuration.php class). The best way to handle it is to check what they did in modules/mailalerts/mailalerts.php to have a better idea how to do it. It's a good example.

0
votes

Matt Loye gave you an easy way to save data. But if you want to create your own table you have to do it during the install process. If you want more examples you can explore the modules that are already installed by default.

You might want to tell us the version you are using, because the modules are a little bit different between PS 1.6 and PS 1.7.

The example below is a part of a custom module that I wrote for PS 1.7.

Installation

public function install()
{
    if (!parent::install() || !$this->createTables()
    ) {
        return false;
    }
    return true;
}

protected function createTables()
{
    $res = (bool)Db::getInstance()->execute('
        CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'my_table` (
            `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
            `column1` varchar(255) NOT NULL,
            `column2` varchar(255) NOT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
    return $res;
}

Deinstallation

public function uninstall()
{
    if (!parent::uninstall() || !$this->deleteTables()){
        return false;
    }
    return true;
}

protected function deleteTables()
{
    return Db::getInstance()->execute('
        DROP TABLE IF EXISTS `'._DB_PREFIX_.'my_table`;
    ');
}

Registration of data

 protected function postProcess()
 {

    if (Tools::isSubmit('submitFormName'))
    {
        //INSERT
        Db::getInstance()->execute('
            INSERT INTO `'._DB_PREFIX_.'my_table'` (`column1`, `column2`)
            VALUES('.$column1.', '.$column2.')'
        );
        //OR DELETE DATA
        Db::getInstance()->execute('
            DELETE FROM `'._DB_PREFIX_.'my_table' `
            WHERE `id` = '.(int)$id
        );
    } 
}