When i tried to edit an image in custom module i am getting an error like below.
1 exception(s):
Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3101
I have a fix for this. replacing this code set
case 'longtext':
$value = (string)$value;
if ($column['NULLABLE'] && $value == '') {
$value = null;
}
break;
With
case 'longtext':
if(!is_array($value)) $value = (string)$value;
else $value = '';
if ($column['NULLABLE'] && $value == '') {
$value = null;
}
break;
I want to know how can i do this in proper way rather than directly editing the core file.
UPDATE
My modules Save.php
try{
$uploader = $this->_objectManager->create(
'Magento\MediaStorage\Model\File\Uploader',
['fileId' => 'image']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
$imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
/** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
$mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save($mediaDirectory->getAbsolutePath('news_news'));
if($result['error']==0)
{
$data['image'] = 'news_news' . $result['file'];
}
Best Answer
The reason behind this error you are passing the array to the column that is causing the issue. You can use the following code to avoid this error.
Reference