通过自定义管理员、Opencart 2.0 将值编辑到数据库
Edit value to database through custom admin, Opencart 2.0
我正在尝试在 OpenCart 2.0.1.1 中制作一个简单的表单,将值存储在数据库中。
我已经完成了一个自定义管理页面,我将在其中进行一些我自己的扩展。这是简单的部分。
我已经在数据库中制作了表格和列,这工作正常,我得到了值,它显示在管理员的文本区域中。但问题是当我试图编辑文本时。当我更改文本区域的值并点击保存时,它就消失了!并且数据库中的列消失了。
我的控制器文件:
admin/controller/ekomet/design.php
<?phpclass ControllerEkometdesign extends Controller {
private $error = array();
public function index() {
$this->load->language('ekomet/design');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('setting/setting');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('design_name', $this->request->post);
}
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
$data['tab_general'] = $this->language->get('tab_general');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_stores'),
'href' => $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL')
);
if (isset($this->session->data['success'])) {
$data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$data['success'] = '';
}
$data['action'] = $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL');
$data['cancel'] = $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL');
$data['token'] = $this->session->data['token'];
$settings = $this->model_setting_setting->getSetting('design_name');
if (isset($this->request->post['config_ekomet_name'])) {
$data['config_ekomet_name'] = $this->request->post['config_ekomet_name'];
} else {
$data['config_ekomet_name'] = $this->config->get('config_ekomet_name');
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('ekomet/design.tpl', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'ekomet/design')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ($this->error && !isset($this->error['warning'])) {
$this->error['warning'] = $this->language->get('error_warning');
}
return !$this->error;
}}
我查看了 setting.php 控制器,我确实想在我的数据库中使用设置 table 来存储我的值(无需再次发明轮子)。正如我上面所说。它确实传递数据库中的值。但是更新不了
我的模板文件:
admin/view/template/ekomet/design.tpl
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-ekomet-name">E-komet namn</label>
<div class="col-sm-10">
<input type="text" name="config_ekomet_name" value="<?php echo $config_ekomet_name; ?>" placeholder="<?php echo $entry_ekomet_name; ?>" id="input-ekomet-name" class="form-control" />
<?php if ($error_ekomet_name) { ?>
<div class="text-danger"><?php echo $error_ekomet_name; ?></div>
<?php } ?>
</div>
</div>
我没有制作自己的模型文件,因为我想使用连接到设置的那个。我需要制作自己的模型才能完成这项工作吗?
您为代码和关键字段使用了错误的前缀
确保如果 code = abc
那么 key = abc_def
替换
$this->model_setting_setting->editSetting('design_name', $this->request->post);
有
$this->model_setting_setting->editSetting('config', $this->request->post);
所以它将是 code = config
和 key = config_ekomet_name
我正在尝试在 OpenCart 2.0.1.1 中制作一个简单的表单,将值存储在数据库中。
我已经完成了一个自定义管理页面,我将在其中进行一些我自己的扩展。这是简单的部分。
我已经在数据库中制作了表格和列,这工作正常,我得到了值,它显示在管理员的文本区域中。但问题是当我试图编辑文本时。当我更改文本区域的值并点击保存时,它就消失了!并且数据库中的列消失了。
我的控制器文件:
admin/controller/ekomet/design.php
<?phpclass ControllerEkometdesign extends Controller {
private $error = array();
public function index() {
$this->load->language('ekomet/design');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('setting/setting');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('design_name', $this->request->post);
}
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
$data['tab_general'] = $this->language->get('tab_general');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_stores'),
'href' => $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL')
);
if (isset($this->session->data['success'])) {
$data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$data['success'] = '';
}
$data['action'] = $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL');
$data['cancel'] = $this->url->link('ekomet/design', 'token=' . $this->session->data['token'], 'SSL');
$data['token'] = $this->session->data['token'];
$settings = $this->model_setting_setting->getSetting('design_name');
if (isset($this->request->post['config_ekomet_name'])) {
$data['config_ekomet_name'] = $this->request->post['config_ekomet_name'];
} else {
$data['config_ekomet_name'] = $this->config->get('config_ekomet_name');
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('ekomet/design.tpl', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'ekomet/design')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ($this->error && !isset($this->error['warning'])) {
$this->error['warning'] = $this->language->get('error_warning');
}
return !$this->error;
}}
我查看了 setting.php 控制器,我确实想在我的数据库中使用设置 table 来存储我的值(无需再次发明轮子)。正如我上面所说。它确实传递数据库中的值。但是更新不了
我的模板文件:
admin/view/template/ekomet/design.tpl
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-ekomet-name">E-komet namn</label>
<div class="col-sm-10">
<input type="text" name="config_ekomet_name" value="<?php echo $config_ekomet_name; ?>" placeholder="<?php echo $entry_ekomet_name; ?>" id="input-ekomet-name" class="form-control" />
<?php if ($error_ekomet_name) { ?>
<div class="text-danger"><?php echo $error_ekomet_name; ?></div>
<?php } ?>
</div>
</div>
我没有制作自己的模型文件,因为我想使用连接到设置的那个。我需要制作自己的模型才能完成这项工作吗?
您为代码和关键字段使用了错误的前缀
确保如果 code = abc
那么 key = abc_def
替换
$this->model_setting_setting->editSetting('design_name', $this->request->post);
有
$this->model_setting_setting->editSetting('config', $this->request->post);
所以它将是 code = config
和 key = config_ekomet_name