Magento – Ui form: image not saved in DB in magento 2

image-uploadmagento2magento2.2ui-form

I need to save below fields in DB. All other fields are saved except Image.But images are saved in pub/media folder.Please suggest me a solution

Here, i have mentioned my controller for save.php

    <?php

    namespace Namespace\HomeSlider\Controller\Adminhtml\Post;

    class Save extends \Magento\Backend\App\Action
    {

        public function __construct(
        \Magento\Backend\App\Action\Context $context, \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
        )
        {
            $this->resultRedirectFactory = $resultRedirectFactory;
            parent::__construct($context);
        }

        public function execute()
        {
            $id = $this->getRequest()->getParam('id');
            $postModel = $this->_objectManager->create('Namespace\HomeSlider\Model\Post');

            if ($id) {
                $postModel = $postModel->load($id);
            }  
            $post = $this->getRequest()->getParams();
      //     echo<pre>;
    //       print_r($post); 
  //         echo</pre>;
     //        exit;
            if(empty($post['id'])) {
                 $post['id'] = null;
             }

            $postModel->setData($post);
            $postModel->save();
            return $this->resultRedirectFactory->create()->setPath('homeslider/post/index');
        }

    }

The parameters are below what i get. But the image is stored as array of array format . How do I overcome this.
enter image description here

Best Answer

Just add these two line before $postModel->setData($post)

$imageName = $post['image'][0]['file'];
$post['image'] = $imageName;
Related Topic