Type Error occurred when creating object
-
14-04-2021 - |
Question
I am having the massaction "delete" in the ui_component and it is then process by a controller MassDelete in my adminhtml folder.
like this:
<?php
namespace Apriljune\Testimonial\Controller\Adminhtml\Testimonial;
use Exception;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Apriljune\Testimonial\Model\ResourceModel\Testimonial\Collection as Testimonial;
/**
* Class MassDelete
*
* @package Apriljune\Testimonial\Controller\Adminhtml\Testimonial
*/
class MassDelete extends Action
{
/**
* @var Testimonial
*/
protected $testimonial;
/**
* @param Context $context
* @param Testimonial $testimonial
*/
public function __construct( Context $context, Testimonial $testimonial )
{
parent::__construct($context);
$this->testimonial = $testimonial;
}
/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$selectedIds = $this->getRequest()->getParams()['selected'];
if (!is_array($selectedIds)) {
$this->messageManager->addErrorMessage(__('Please select one or more testimonial.'));
} else {
try {
$collectionSize = count($selectedIds);
foreach ($selectedIds as $_id) {
$testimonial = $this->testimonial->getItems()[$_id];
$testimonial->delete();
}
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $collectionSize));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
Testimonial Class:
<?php
namespace Apriljune\Testimonial\Controller\Adminhtml\Testimonial;
class Testimonial extends \Magento\Backend\App\Action
{
protected $resultPageFactory = false;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend((__('Testimonial')));
return $resultPage;
}
protected function _isAllowed()
{
return true;
}
}
and massaction is like this in ui-component
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
</item>
</argument>
<action name="delete">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">delete</item>
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="url" path="*/*/MassDelete"/>
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Delete</item>
<item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
</item>
</item>
</argument>
</action>
</massaction>
Now when i select any row and delete it it redirect to the error page and show the exception
Type Error occurred when creating object: Apriljune\Testimonial\Controller\Adminhtml\Testimonial\MassDelete\Interceptor
i have already tried the solutions,
already done with this process as well
rm -rf var/di/* pub/static/* generated/*
rm -rf var/cache/* var/view_preprocessed/*
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy
bin/magento cache:flush
But still the error is there any hint why it is showing error? any typo or logical error? BTW i am using 2.3.3 version of magneto
La solution
Add this file
<?php
namespace Apriljune\Testimonial\Controller\Adminhtml\Testimonial;
class MassDelete extends \Magento\Backend\App\Action
{
protected $_filter;
protected $_collectionFactory;
protected $testimonialFactory;
public function __construct(
\Magento\Ui\Component\MassAction\Filter $filter,
\Apriljune\Testimonial\Model\ResourceModel\Testimonial\CollectionFactory $collectionFactory,
\Apriljune\Testimonial\Model\TestimonialFactory $testimonialFactory,
\Magento\Backend\App\Action\Context $context
) {
$this->_filter = $filter;
$this->_collectionFactory = $collectionFactory;
$this->testimonialFactory = $testimonialFactory;
parent::__construct($context);
}
public function execute() {
try{
$logCollection = $this->_filter->getCollection($this->_collectionFactory->create());
foreach ($logCollection as $item) {
$testimonial_items = $this->testimonialFactory->create();
$testimonial_items->load($item->getId());
$testimonial_items->delete();
}
$this->messageManager->addSuccess(__('Log Deleted Successfully.'));
}catch(Exception $e){
$this->messageManager->addError($e->getMessage());
}
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('apriljune_testimonial/testimonial/testimonial');
}
}
after added above code please run below command
bin/magento setup:upgrade
bin/magento setup:static-content:deploy
bin/magento cache:flush
Autres conseils
You should to define messageManager and resultFactory in __contruct.
<?php
namespace Apriljune\Testimonial\Controller\Adminhtml\Testimonial;
use Exception;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Apriljune\Testimonial\Model\ResourceModel\Testimonial\Collection as Testimonial;
/**
* Class MassDelete
*
* @package Apriljune\Testimonial\Controller\Adminhtml\Testimonial
*/
class MassDelete extends Action
{
/**
* @var Testimonial
*/
protected $testimonial;
/**
* Message manager interface
*
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;
/**
* @var \Magento\Framework\Controller\ResultFactory
*/
protected $resultFactory;
/**
* MassDelete constructor.
* @param Context $context
* @param Testimonial $testimonial
*/
public function __construct( Context $context, Testimonial $testimonial )
{
parent::__construct($context);
$this->testimonial = $testimonial;
$this->messageManager = $context->getMessageManager();
$this->resultFactory = $context->getResultFactory();
}
/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$selectedIds = $this->getRequest()->getParams()['selected'];
if (!is_array($selectedIds)) {
$this->messageManager->addErrorMessage(__('Please select one or more testimonial.'));
} else {
try {
$collectionSize = count($selectedIds);
foreach ($selectedIds as $_id) {
$testimonial = $this->testimonial->getItems()[$_id];
$testimonial->delete();
}
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $collectionSize));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange