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

2

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

$imageName = $post['image'][0]['file'];
$post['image'] = $imageName;
  • 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();

  • I have solved the issue in other way. But it is failed to load exiting image what i was stored in DB while pressing edit – Jaisa Nov 15 '17 at 13:28
  • check this answer magento.stackexchange.com/a/201018/20064 – Piyush Nov 15 '17 at 13:30
  • Am i need to set each colum like this way which you mentioned above ? – Jaisa Nov 15 '17 at 13:48
  • Is this a optimized way? – Jaisa Nov 15 '17 at 13:48
  • No, it is same as Qaisar's Answer – Piyush Nov 15 '17 at 13:52
-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) {
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.