我正在尝试通过添加multiselect选项来扩展Manufacturers类。看起来像这样:
public function renderForm()
{
global $shopOptions;
$this->fields_form_override = array(
array(
'type' => 'select',
'label' => $this->l('Shops'),
'name' => 'shops',
'class' => 'chosen',
'multiple' => true,
'required' => true,
'options' => array(
'query' => $shopOptions,
'id' => 'id',
'name' => 'name'
),
),
);
return parent::renderForm();
}
在此处检索数据:
public function __construct() {
parent::__construct();
global $shopOptions;
$shopSQL = 'SELECT id_test_shop, name FROM '._DB_PREFIX_.'test_shop WHERE active=1';
$result = Db::getInstance()->ExecuteS($shopSQL);
foreach ($result as $row) {
$shopOptions[] = array(
"id" => (int)$row["id_test_shop"],
"name" => $row["name"]
);
}
}
然后我像这样修改Manufacturer.php中的定义变量:
$definition["fields"]["shops"] = array('type' => self::TYPE_STRING)
这一切似乎都奏效,但是数据库中保存的值为空白,可能是因为它不知道如何处理多选值。
我基本上是想在数据库中放置一个字符串,例如“ 1,2,3”。因此,我尝试修改processsave函数:
public function processSave()
{
if (Tools::isSubmit('submitAddManufacturer')) {
$_POST['shops'] = implode(',', Tools::getValue('shops'));
}
return parent::processSave();
}
但这只会使其崩溃,并显示“未定义索引:shops []”。
有人知道如何将选择值的ID作为内含的字符串保存到数据库吗?
谢谢!
答案 0 :(得分:0)
我认为您需要修改/创建Manufacturer.php
文件的方法add
和update
,并使用它们将数据保存到新属性$this->shops
中。像这样:
public function add($auto_date = true, $null_values = false)
{
if($shops = Tools::getValue('shops')) {
$this->shops = implode(',', $shops);
}
return parent::add($auto_date, $null_values);
}
并与更新类似:
public function update($null_values = false)
{
if($shops = Tools::getValue('shops')) {
$this->shops = implode(',', $shops);
}
return parent::update($null_values);
}