#StackBounty: #magento2 #module #category-selector Magento 2 How to save category selector data for custom module?

Bounty: 50

I’ve used below code to show category selector on custom module. Its show wihout any error. Below is code for showing category selector on form.

$fieldset->addType(
        'categories',
        'MagentoCatalogBlockAdminhtmlProductHelperFormCategory'
);




$fieldset->addField(
        'category',
        'categories',
        [
            'name' => 'category',
            'label' => __('Category'),
            'title' => __('Category')
        ]
    );

I’ve save data using controller using below code as its array i need to convert it in string to save.

if ($data['category']):
  $data['category'] = implode(",",$data['category']);
endif;

its saved successfully. but when i edit data its thorow me error.

[2017-05-03 12:54:58] report.CRITICAL: exception 'Exception' with message 'Notice: Array to string conversion in /var/www/html/magento/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2930' in /var/www/html/magento/vendor/magento/framework/App/ErrorHandler.php:61

Am i missing something here?

Update:

below is form code:

class Main extends MagentoBackendBlockWidgetFormGeneric implements MagentoBackendBlockWidgetTabTabInterface
{
     /**
     * @var MagentoCmsModelWysiwygConfig
     */
    protected $_wysiwygConfig;

    /**
     * @var MagentoStoreModelSystemStore
     */
    protected $_systemStore;

    /**
     * @param MagentoBackendBlockTemplateContext $context
     * @param MagentoFrameworkRegistry $registry
     * @param MagentoFrameworkDataFormFactory $formFactory
     * @param MagentoStoreModelSystemStore $systemStore
     * @param MagentoCmsModelWysiwygConfig $wysiwygConfig
     * @param array $data
     */

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoFrameworkRegistry $registry,
        MagentoFrameworkDataFormFactory $formFactory,
        MagentoStoreModelSystemStore $systemStore,
        MagentoCmsModelWysiwygConfig $wysiwygConfig,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        $this->_wysiwygConfig = $wysiwygConfig;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    public function getTabLabel()
    {
        return __('Item Information');
    }


    public function getTabTitle()
    {
        return __('Item Information');
    }


    public function canShowTab()
    {
        return true;
    }


    public function isHidden()
    {
        return false;
    }

    /**
     * Prepare form before rendering HTML
     *
     * @return $this    
     */
    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('current_kensium_advisor_items');
        /** @var MagentoFrameworkDataForm $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('item_');
        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Item Information')]);
        if ($model->getId()) {
            $fieldset->addField('id', 'hidden', ['name' => 'id']);
        }
        $fieldset->addType(
        'categories',
        'MagentoCatalogBlockAdminhtmlProductHelperFormCategory'
       );

    $fieldset->addField(
            'advisor_name',
            'text',
            [
                'name' => 'advisor_name',
                'label' => __('Name'),
                'title' => __('Name'),
                'required' => true
            ]
        );

Update:
I’ve updated code here for module.

https://github.com/kul3101/Category-Selector/tree/master/Test/Category/app/code/Test/Category


Get this bounty!!!

Leave a Reply