1

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

Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
Jaisa
  • 2,606
  • 10
  • 53
  • 117

3 Answers3

2

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

$imageName = $post['image'][0]['file'];
$post['image'] = $imageName;
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
  • Thank you . I have solved the issue but it is properly working for new action But it is failed to work for edit action in UI form – Jaisa Nov 15 '17 at 13:23
0

In your save action instead of $postModel->setData($post); set data individually. Like below

$postModel->setColumnName($post['param_name_of_column']);

Do this for all the Column and for image you can do like below

$postModel->setImage($post['image'][0]['name']);

and Then save the model using $postModel->save();

Piyush
  • 5,893
  • 9
  • 34
  • 64
-1

First,add use Magento\Framework\App\Filesystem\DirectoryList to this class.

Then add below for add code for upload image

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->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
    $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){
    $path = 'emizen_banner' .$result['file'];
    }
    //sucessFully upload
} catch (\Exception $e) {
}
TBI Infotech
  • 4,788
  • 1
  • 13
  • 30