سؤال

I am trying to save multi select value in DB. But getting error Array to string conversion in

/var/www/html/devstore/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2999

Template file code -

<div class="field profile">
                <label for="sell_country"> <?php echo __('Sell in Countries') ?> </label>
                <?php
                if($profile_hint_status && $helper->getProfileHintCountry()){?>
                    <img src="<?php echo $this->getViewFileUrl('Webkul_Marketplace::images/quest.png'); ?>" class='questimg' title="<?php echo $helper->getProfileHintCountry() ?>"/>
                <?php
                } ?>
                <div class="control">
                     <select name="sell_country[]" id="sell_country" multiple="multiple">
                        <option value="" selected="selected" disabled="disabled"><?php echo __('Select Country')?></option>
                    <?php foreach($block->getCountryOptionArray() as $country){?>
                        <option <?php
                        if($country['value']!=''){
                        echo ($partner['country_pic']==$country['value']?"selected='selected'":""); ?>value="<?php echo $country['value']; ?>"><?php echo $country['label']; ?></option>
                    <?php 
                      }
                    }   ?>
                    </select>

                </div>
            </div>

In controller file -
if (array_key_exists('sell_country', $fields)) {
                        $value->setCountryPic(implode(',', $fields['sell_country']);
                    }
                    $value->save();

Update 1 - This is the execute method of controller - public function execute() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create();

    if ($this->getRequest()->isPost()) {
        try {
            if (!$this->_formKeyValidator->validate($this->getRequest())) {
                return $this->resultRedirectFactory->create()->setPath(
                    '*/*/editProfile',
                    ['_secure' => $this->getRequest()->isSecure()]
                );
            }

            $fields = $this->getRequest()->getParams();
            $errors = $this->validateprofiledata($fields);
            $sellerId = $this->helper->getCustomerId();
            $storeId = $this->helper->getCurrentStoreId();
            $img1 = '';
            $img2 = '';
            if (empty($errors)) {
                $autoId = 0;
                $collection = $this->_objectManager->create(
                    'Webkul\Marketplace\Model\Seller'
                )
                ->getCollection()
                ->addFieldToFilter('seller_id', $sellerId)
                ->addFieldToFilter('store_id', $storeId);
                foreach ($collection as $value) {
                    $autoId = $value->getId();
                }
                $fields = $this->getSellerProfileFields($fields);
                // If seller data doesn't exist for current store
                if (!$autoId) {
                    $sellerDefaultData = [];
                    $collection = $this->_objectManager->create(
                        'Webkul\Marketplace\Model\Seller'
                    )
                    ->getCollection()
                    ->addFieldToFilter('seller_id', $sellerId)
                    ->addFieldToFilter('store_id', 0);
                    foreach ($collection as $value) {
                        $sellerDefaultData = $value->getData();
                    }
                    foreach ($sellerDefaultData as $key => $value) {
                        if (empty($fields[$key]) && $key != 'entity_id') {
                            $fields[$key] = $value;
                        }
                    }
                }

                // Save seller data for current store
                $value = $this->_objectManager->create(
                    'Webkul\Marketplace\Model\Seller'
                )->load($autoId);
                $value->addData($fields);
                if (!$autoId) {
                    $value->setCreatedAt($this->_date->gmtDate());
                }
                $value->setUpdatedAt($this->_date->gmtDate());
                $value->save();

                if ($fields['company_description']) {
                    $fields['company_description'] = str_replace(
                        'script',
                        '',
                        $fields['company_description']
                    );
                }
                $value->setCompanyDescription($fields['company_description']);

                if (isset($fields['return_policy'])) {
                    $fields['return_policy'] = str_replace(
                        'script',
                        '',
                        $fields['return_policy']
                    );
                    $value->setReturnPolicy($fields['return_policy']);
                }

                if (isset($fields['shipping_policy'])) {
                    $fields['shipping_policy'] = str_replace(
                        'script',
                        '',
                        $fields['shipping_policy']
                    );
                    $value->setShippingPolicy($fields['shipping_policy']);
                }

                $value->setMetaDescription($fields['meta_description']);

                /**
                 * set taxvat number for seller
                 */
                if ($fields['taxvat']) {
                    $customer = $this->_objectManager->create(
                        'Magento\Customer\Model\Customer'
                    )->load($sellerId);
                    $customer->setTaxvat($fields['taxvat']);
                    $customer->setId($sellerId)->save();
                }

                $target = $this->_mediaDirectory->getAbsolutePath('avatar/');
                try {
                    /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
                    $uploader = $this->_fileUploaderFactory->create(
                        ['fileId' => 'banner_pic']
                    );
                    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                    $uploader->setAllowRenameFiles(true);
                    $result = $uploader->save($target);
                    if ($result['file']) {
                        $value->setBannerPic($result['file']);
                    }
                } catch (\Exception $e) {
                    if ($e->getMessage() != 'The file was not uploaded.') {
                        $this->messageManager->addError($e->getMessage());
                    }
                }
                try {
                    /** @var $uploaderLogo \Magento\MediaStorage\Model\File\Uploader */
                    $uploaderLogo = $this->_fileUploaderFactory->create(
                        ['fileId' => 'logo_pic']
                    );
                    $uploaderLogo->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                    $uploaderLogo->setAllowRenameFiles(true);
                    $resultLogo = $uploaderLogo->save($target);
                    if ($resultLogo['file']) {
                        $value->setLogoPic($resultLogo['file']);
                    }
                } catch (\Exception $e) {
                    if ($e->getMessage() != 'The file was not uploaded.') {
                        $this->messageManager->addError($e->getMessage());
                    }
                }

                if (array_key_exists('sell_country', $fields)) {
                    $value->setCountryPic($fields['sell_country']);
                }
                $value->save();
هل كانت مفيدة؟

المحلول

Your error comes from the following line

$value->addData($fields);

Here you add all the data you get from your form inlcuding the array. Anyway I don't see the implode in your controller. Put the code

 if (array_key_exists('sell_country', $fields)) {
     $value->setCountryPic(implode(',', $fields['sell_country']);
     unset($fields['sell_country']);
 }

before that line and that should work.

Please notice that you save the $value model twice and you also use the variable $value in other context in the code. That works but it's not really a good practice :-)

نصائح أخرى

I did almost like this one before and it worked for me.

Before set data in model I used implode like:

if(isset($data['country_pic'])){
    $data['country_pic']=implode(',',$data['country_pic']);
}

$model->setData($data);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top