문제

I created a custom module with a multiselect type input.

My InstallSchema.php file contains this for this field.

->addColumn(
    'elegible_products',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    500,
    ['nullable => false'],
    'Products'
)

My Main.php file looks like

$fieldset->addField(
    'elegible_products',
    'multiselect',
    [
        'label' => __('Products'),
        'title' => __('Products'),
        'name' => 'elegible_products',
        'values' => \Ayakil\FreeGift\Block\Adminhtml\Freegifts\Grid::getValueArray4(),
        'disabled' => $isElementDisabled
    ]
);

This is my Grid.php file

static public function getOptionArray4()
{
    $data_array = array(); 
    $data_array[0] = 'a1';
    $data_array[1] = 'a2';

    return($data_array);
}

static public function getValueArray4()
{
    $data_array = array();
    foreach(\Ayakil\FreeGift\Block\Adminhtml\Freegifts\Grid::getOptionArray4() as $k => $v) {
        $data_array[] = array('value' => $k, 'label' => $v);
    }

    return($data_array);
}

If i tried to save the form i am getting an error like below.

{"0":"Notice: Array to string conversion in /home/muja/www/ayakil-greens/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3105","1"

How can add Multiselect in custom admin form and save the data and retrieve the data here?

도움이 되었습니까?

해결책

It seems like in your save controller, you need to set multiselect's value as a string. For eg :

$data['elegible_products'] = implode(',',$data['elegible_products']);

And then, you need to save records.

Hope, It will helpful for you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top