PrestaShop:使用ObjectModel保存MultiSelect

I'm trying to extend the Manufacturers class by adding a multiselect option. This is what that looks like:

 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();

}

Data is retrieved here:

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"]
        );
    }

}

I then modified the definition variable in Manufacturer.php like this:

$definition["fields"]["shops"] = array('type' => self::TYPE_STRING)

This all seems to work but the values saved in the database are blank, probably because it doesn't know how to deal with the multiselect values.

I'm basically trying to have a string like "1,2,3" placed in the database. I therefore tried to modify the processsave function:

public function processSave()
{
    if (Tools::isSubmit('submitAddManufacturer')) {
        $_POST['shops'] = implode(',', Tools::getValue('shops'));
    }

    return parent::processSave();
}

But that just makes it crash and it says "Undefined index: shops[]".

Does anyone know how I can have it save the ids of the select values to the database as an imploded string?

Thanks!

I think that you need to modify/create your Manufacturer.php file's methods add and update and use them to save your data into your new property $this->shops. Something like this:

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);
}

and similar with update:

public function update($null_values = false)
{
    if($shops = Tools::getValue('shops')) {
        $this->shops = implode(',', $shops);
    }

    return parent::update($null_values);
}