0

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();
asked Feb 7, 2019 at 5:37
5
  • can you provide a little more code from your controller? what is your $value object and what data does it contain beside the 'country_pic' you add manually? Commented Feb 7, 2019 at 8:44
  • @HelgeB $value = $this->_objectManager->create( 'Webkul\Marketplace\Model\Seller' )->load($autoId); This is $value, I am customizing webkul marketplace extension to save multiselect product selling countries vendor attribute. Commented Feb 7, 2019 at 12:47
  • ok, if you have only the 4 lines in your controller affecting the $value object and no other values it's really strange. In this case I would suggest to dump the data of the value object and see what's in there. Commented Feb 7, 2019 at 13:00
  • @HelgeB please check the controller above. Commented Feb 7, 2019 at 13:23
  • see my answer, does that solve your problem? Commented Feb 7, 2019 at 18:17

2 Answers 2

0

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 :-)

answered Feb 7, 2019 at 13:30
6
  • Hi @HelgeB Thankyou for your response but, After adding the above code i still got error -Notice: Array to string conversion Commented Feb 8, 2019 at 4:35
  • Can you please dump the data of the value object just before the save call? Then we can see, which array is still in your data Commented Feb 8, 2019 at 6:14
  • Hi, when we use array in html it is not dumping data, but for single value I dump and this is the output - string(2) "AL" . Commented Feb 11, 2019 at 4:49
  • I printed the value print_r($fields); and got this output- Array ( [form_key] => pYm9J1Kr5Ddx2H0g [twitter_id] => [facebook_id] => [instagram_id] => [gplus_id] => [youtube_id] => [vimeo_id] => [pinterest_id] => [contact_number] => [taxvat] => [background_width] => [return_policy] => [shipping_policy] => [country_pic] => AX [sell_country] => Array ( [0] => AW [1] => BM [2] => BT [3] => BO [4] => BA ) ) Commented Feb 11, 2019 at 5:21
  • Ok, that should work if you unset the sell_country key before you call addData($fields). Can you please update your question with the actual code? Please dump also $value->getData() just before calling the save() method. Commented Feb 11, 2019 at 6:16
0

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);
answered Feb 7, 2019 at 6:10
2
  • I did if (array_key_exists('sell_country', $fields)) { $fields['sell_country'] = implode(',', $fields['sell_country']); $value->setCountryPic($fields); } $value->save(); and this both if (array_key_exists('sell_country', $fields)) { $fields['sell_country'] = implode(',', $fields['sell_country']); $value->setCountryPic($fields['sell_country']); } $value->save(); Commented Feb 7, 2019 at 6:20
  • But still no effect Commented Feb 7, 2019 at 6:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.