Magento 2 Image not saving in Database. My custom field is img
public function execute()
{
$data = $this->getRequest()->getPostValue();
/*print_r($data);exit();*/
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
$model = $this->_objectManager->create('Urds\OfferBanner\Model\Banner');
$id = $this->getRequest()->getParam('offerbanner_id');
if ($id) {
$model->load($id);
$model->setCreatedAt(date('Y-m-d H:i:s'));
}
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('emizen_banner'));
if($result['error']==0)
{
$data['image'] = 'emizen_banner' . $result['file'];
}
} catch (\Exception $e) {
//unset($data['image']);
}
//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', ['offerbanner_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', ['offerbanner_id' => $this->getRequest()->getParam('offerbanner_id')]);
}
return $resultRedirect->setPath('*/*/');
}