【发布时间】:2021-12-31 13:30:26
【问题描述】:
我克隆了一个 prestashop 1.7 模块,一切正常,没有任何问题。我想做的(请)是将数据保存在 prestashop 数据库本身中,但我不能这样做我希望我能更好地解释自己
代码
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Stagenti extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'stagenti';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Lab';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Lab Agenti');
$this->description = $this->l('Gestionale per agenti di commercio');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
}
public function install()
{
if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL);
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
//$values[] = Tools::getValue('SOMETEXT_TEXT_'.$lang['id_lang']);
Configuration::updateValue('STAGENTI_TEXT_' . $lang['id_lang'], '', true);
Configuration::updateValue('STAGENTI_TEXT2_' . $lang['id_lang'], '', true);
}
include(dirname(__FILE__).'/sql/install.php');
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('displayBanner');
}
public function uninstall()
{
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
//$values[] = Tools::getValue('SOMETEXT_TEXT_'.$lang['id_lang']);
Configuration::deleteByName('STAGENTI_TEXT_' . $lang['id_lang']);
Configuration::deleteByName('STAGENTI_TEXT2_' . $lang['id_lang']);
}
include(dirname(__FILE__).'/sql/uninstall.php');
return parent::uninstall();
}
public function getContent()
{
if (((bool)Tools::isSubmit('submitStagentiModule')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');
return $output . $this->renderForm();
}
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitStagentiModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
. '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Impostazioni'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'col' => 9,
'type' => 'text',
'desc' => $this->l('inserisci lo slogan che desideri far visualizzare'),
'name' => 'STAGENTI_TEXT2',
'label' => $this->l('Messaggio Slogan'),
'autoload_rte' => true,
'lang' => true
),
array(
'col' => 9,
'type' => 'textarea',
'desc' => $this->l('se inserisci una img le misure devono essere 1140 x 270 px'),
'name' => 'STAGENTI_TEXT',
'label' => $this->l('Messaggio riepilogo'),
'autoload_rte' => true,
'lang' => true
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
protected function getConfigFormValues()
{
$languages = Language::getLanguages(false);
$values = array();
foreach ($languages as $lang) {
$values['STAGENTI_TEXT'][$lang['id_lang']] = Configuration::get('STAGENTI_TEXT_' . $lang['id_lang']);
$values['STAGENTI_TEXT2'][$lang['id_lang']] = Configuration::get('STAGENTI_TEXT2_' . $lang['id_lang']);
}
return $values;
}
protected function postProcess()
{
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
Configuration::updateValue('STAGENTI_TEXT_' . $lang['id_lang'], Tools::getValue('STAGENTI_TEXT_' . $lang['id_lang']), true);
Configuration::updateValue('STAGENTI_TEXT2_' . $lang['id_lang'], Tools::getValue('STAGENTI_TEXT2_' . $lang['id_lang']), true);
}
}
public function hookBackOfficeHeader()
{
if (Tools::getValue('module_name') == $this->name) {
$this->context->controller->addJS($this->_path . 'views/js/back.js');
$this->context->controller->addCSS($this->_path . 'views/css/back.css');
}
}
public function hookHeader()
{
$this->context->controller->addJS($this->_path . 'views/js/front.js');
$this->context->controller->addCSS($this->_path . 'views/css/front.css');
}
public function hookDisplayBanner()
{
$some_string = Configuration::get('STAGENTI_TEXT_' . $this->context->language->id);
$this->context->smarty->assign([
'module_dir' => $this->_path,
'stagenti_message' => $some_string
]);
$some_string = Configuration::get('STAGENTI_TEXT2_' . $this->context->language->id);
$this->context->smarty->assign([
'module_dir' => $this->_path,
'stagenti_message5' => $some_string
]);
return $this->display(__FILE__, 'message.tpl');
}
}
数据库
<?php
$sql = array();
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . '` (
`id_stagenti` int(11) NOT NULL AUTO_INCREMENT,
`stagenti_text` text NOT NULL,
`stagenti_text2` text NOT NULL,
PRIMARY KEY (`id_infomess`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
foreach ($sql as $query) {
if (Db::getInstance()->execute($query) == false) {
return false;
}
}
【问题讨论】:
-
您要保存哪些数据?您还创建了表吗?正如我在您的 sql 中看到的那样 - '如果不存在则创建表
' . _DB_PREFIX_ . '.... 没有表名!因此,如果您的数据库前缀 (DB_PREFIX ) 是例如 ps_ 您应该在其后添加一些名称,例如 stagenti 或其他任何东西! -
非常好心回答我,是的,表已创建,是的,我想保存数据
-
不清楚你指的是什么数据......所以你的模块视图中有一个表单,对吗?我假设有 2 个字段,并在其中放置了一些要保存在 2 个表列 stagenti_text 和 stagenti_text2 中的字符串。是这样吗?
-
是的,我想在数据库中保存 stagenti_text 和 stagenti_text2
-
你可以像平常一样使用
Db::getInstance()->execute("YOUR SQL QUERY")创建一个MySQL表。
标签: php database prestashop