This is my function, if i update it without image it throws the an error . Update with image it works Fine. How to fix this
**
1 exception(s):
Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/magento/project/vendor/magento/framework/Data/Form/Element/Image.php on line 51
**
Save.php
public function execute()
{
$data = $this->getRequest()->getPostValue();
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
$model = $this->_objectManager->create('Namespace\Banner\Model\Banner');
$id = $this->getRequest()->getParam('banner_id');
if ($id) {
$model->load($id);
//$model->setCreatedAt(date('Y-m-d H:i:s'));
}
if(!isset($data['image']['delete']) || ( isset($data['image']['delete'])$data['image']['delete'] != '1')){
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('banner'));
if($result['error']==0)
{
$data['image'] = 'banner' . $result['file'];
}
} catch (\Exception $e) {
//unset($data['image']);
$this->messageManager->addException($e, __('$e->getMessage()'));
}
}
//var_dump($data);die;
if(isset($data['image']['delete']) && $data['image']['delete'] == '1')
$data['image'] = '';
$model->setData($data);
try {
$model->save();
$this->messageManager->addSuccess(__('The Banner has been saved.'));
$this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['banner_id' => $model->getId(), '_current' => true]);
}
return $resultRedirect->setPath('*/*/');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\RuntimeException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the Banner.'));
}
$this->_getSession()->setFormData($data);
return $resultRedirect->setPath('*/*/edit', ['banner_id' => $this->getRequest()->getParam('banner_id')]);
}
return $resultRedirect->setPath('*/*/');
}
Edit.php
public function execute()
{
// 1. Get ID and create model
$id = $this->getRequest()->getParam('banner_id');
$model = $this->_objectManager->create('Namespace\Banner\Model\Banner');
// 2. Initial checking
if ($id) {
$model->load($id);
if (!$model->getId()) {
$this->messageManager->addError(__('This item no longer exists.'));
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
// 3. Set entered data if was error when we do save
$data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
// 4. Register model to use later in blocks
$this->_coreRegistry->register('banner', $model);
// 5. Build edit form
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->_initAction();
$resultPage->addBreadcrumb(__('Namespace'), __('Namespace'));
$resultPage->addBreadcrumb(
$id ? __('Edit Item') : __('New Item'),
$id ? __('Edit Item') : __('New Item')
);
$resultPage->getConfig()->getTitle()->prepend($id ? __('Edit Item') : __('New Item'));
//$resultPage->getConfig()->getTitle()->prepend($model->getId() ? $model->getTitle() : __('New Item'));
return $resultPage;
}