cancel
Showing results for 
Search instead for 
Did you mean: 

PDF is not uploading showing Type Error occurred when creating object error

PDF is not uploading showing Type Error occurred when creating object error

Hi Guys,

 

I am adding pdf upload feature in pre built event module & i wrote upload pdf code in save controller but it is showing below error

 

1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Webkul\EventManager\Controller\Adminhtml\Event\Save\Interceptor

 

My controller code is below

<?php
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_EventManager
* @author Webkul
* @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
namespace Webkul\EventManager\Controller\Adminhtml\Event;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;
/**
* Save class is a controller to save events
*/
class Save extends \Magento\Backend\App\Action
{
protected $_fileUploaderFactory;
/**
* @var \Webkul\EventManager\Model\EventFactory
*/
protected $event;

/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $cacheTypeList;

/**
* __construct function
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param \Webkul\EventManager\Model\EventFactory $Eventfactory
*/
public function __construct(
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Webkul\EventManager\Model\EventFactory $Eventfactory
) {
$this->_fileUploaderFactory = $fileUploaderFactory;
$this->event = $Eventfactory;
$this->cacheTypeList = $cacheTypeList;
$this->dateTime = $dateTime;
parent::__construct($context);
}

/**
* _isAllowed is used to authorize role
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Webkul_EventManager::EventManager');
}

/**
* Save event controller
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$eventObj = $this->event->create();
$event_data = $this->getRequest()->getParams();
$data = $event_data['event_data'];
try {
if (isset($data['event_id'])) {
$eventObj->load($data['event_id']);
$eventObj->setData($data);
$eventObj->save();
$this->messageManager->addSuccess(__("Event data Successfully Upadated"));
$this->cleanCache();
} else {
$today = $this->dateTime->date('m/d/y');
if (strtotime($today) <= strtotime($data['event_date'])) {


//file upload code start

$uploader = $this->_fileUploaderFactory->create(['fileId' => 'event_homepdf']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'pdf', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('mycustomfolder/');
$uploader->save($path);

//file upload code end





// $eventObj->setData($data);
//$eventObj->save();
$this->messageManager->addSuccess(__("Event has been successfully saved"));
$this->cleanCache();
} else {
$this->messageManager->addError(__("Event has not been successfully saved."));
$this->messageManager->addNotice(
__("The event date should be declared for the current date or future date.")
);
$this->cleanCache();
}

}
} catch (\Exception $e) {
$this->messageManager->addError(__("Some error occure please retry"));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect->setPath('eventmanager/event/eventpage');
}

/**
* CleanCache function is used clean particular cahce types
*/
public function cleanCache()
{
$types = ['layout','block_html','full_page'];
foreach ($types as $type) {
$this->cacheTypeList->cleanType($type);
}
}
}

Any one let is know where I am doing mistake & how to save pdf name in database I already increase pdf column in database table.

 

Thanks

Sanjeev

 

5 REPLIES 5

Re: PDF is not uploading showing Type Error occurred when creating object error

Hi @sarkis _salleh,

 

Can you please run below commands:

 

1. rm -rf generated/*

2. php bin/magento s:d:c

3. php bin/magento c:f

4. chmod 777 -R generated/*

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: PDF is not uploading showing Type Error occurred when creating object error

Hi @sarkis _salleh 

 

The interceptor file needs to be re-create. So kindly run di:compile and clean cache

php bin/magento setup:di:compile
php  bin/magento  cache:clean

It may help!
Problem Solved ? Click on 'Kudos' & Accept as Solution! 

Re: PDF is not uploading showing Type Error occurred when creating object error

Hi Nishu,

 

Thank you for your reply but still it is showing Some error occure please retry, Please check my controller class 

 


protected $_fileUploaderFactory;
protected $_filesystem;
/**
* @var \Webkul\EventManager\Model\EventFactory
*/
protected $event;

/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $cacheTypeList;

/**
* __construct function
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param \Webkul\EventManager\Model\EventFactory $Eventfactory
*/
public function __construct(
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Webkul\EventManager\Model\EventFactory $Eventfactory
) {
$this->_fileUploaderFactory = $fileUploaderFactory;
$this->event = $Eventfactory;
$this->cacheTypeList = $cacheTypeList;
$this->dateTime = $dateTime;
parent::__construct($context);
}

/**
* _isAllowed is used to authorize role
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Webkul_EventManager::EventManager');
}

/**
* Save event controller
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$eventObj = $this->event->create();
$event_data = $this->getRequest()->getParams();
$data = $event_data['event_data'];
try {
if (isset($data['event_id'])) {
$eventObj->load($data['event_id']);
$eventObj->setData($data);
$eventObj->save();
$this->messageManager->addSuccess(__("Event data Successfully Upadated"));
$this->cleanCache();
} else {
$today = $this->dateTime->date('m/d/y');
if (strtotime($today) <= strtotime($data['event_date'])) {


//file upload code start

$uploader = $this->_fileUploaderFactory->create(['fileId' => 'event_homepdf']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'pdf', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('mycustomfolder/');
$uploader->save($path);

//file upload code end


//$data['event_homepdf']=$path;


$eventObj->setData($data);
$eventObj->save();
$this->messageManager->addSuccess(__("Event has been successfully saved"));
$this->cleanCache();
} else {
$this->messageManager->addError(__("Event has not been successfully saved."));
$this->messageManager->addNotice(
__("The event date should be declared for the current date or future date.")
);
$this->cleanCache();
}

}
} catch (\Exception $e) {
$this->messageManager->addError(__("Some error occure please retry"));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect->setPath('eventmanager/event/eventpage');
}

/**
* CleanCache function is used clean particular cahce types
*/
public function cleanCache()
{
$types = ['layout','block_html','full_page'];
foreach ($types as $type) {
$this->cacheTypeList->cleanType($type);
}
}

Re: PDF is not uploading showing Type Error occurred when creating object error

I am using below code to upload pdf but it is not working

 

try{
$target = $this->_mediaDirectory->getAbsolutePath('mycustomfolder/');
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->_fileUploaderFactory->create(['fileId' => 'event_homepdf']);
/** Allowed extension types */
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png', 'pdf', 'doc']);
/** rename file name if already exists */
$uploader->setAllowRenameFiles(true);
/** upload file in folder "mycustomfolder" */
$result = $uploader->save($target);
if ($result['event_homepdf']) {
$this->messageManager->addSuccess(__('File has been successfully uploaded'));
}
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}

Re: PDF is not uploading showing Type Error occurred when creating object error

Hi @sarkis _salleh , 

 

To debug please replace below code of execute function in controller

 

 

$eventObj->load($data['event_id']);
$eventObj->setData($data);
$eventObj->save();
$this->messageManager->addSuccess(__("Event data Successfully Upadated"));
$this->cleanCache();
} else {
$today = $this->dateTime->date('m/d/y');
if (strtotime($today) <= strtotime($data['event_date'])) {


//file upload code start

$uploader = $this->_fileUploaderFactory->create(['fileId' => 'event_homepdf']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'pdf', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = $this->_filesystem->getDirectoryRead(\Magento\Framework\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('mycustomfolder/');
$uploader->save($path);

//file upload code end


//$data['event_homepdf']=$path;


$eventObj->setData($data);
$eventObj->save();
$this->messageManager->addSuccess(__("Event has been successfully saved"));
$this->cleanCache();
} else {
$this->messageManager->addError(__("Event has not been successfully saved."));
$this->messageManager->addNotice(
__("The event date should be declared for the current date or future date.")
);
$this->cleanCache();
}

}
} catch (\Exception $e) {

echo $e->getMessage(); exit; 
$this->messageManager->addError(__("Some error occure please retry"));
}
$resultRedirect = $this->resultFactory->create(Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect->setPath('eventmanager/event/eventpage');
}

 

Through the code, we will get the real error from the controller. Please post the real error message.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!