MassAction is Custom Module: Type Error occurred when creating object

Questions

QuestionsCategory: Magento 2 QuestionsMassAction is Custom Module: Type Error occurred when creating object
Shoaib asked 5 years ago

I Have a MaaDelete Action in my Admin UI_Component GRID, 
everything is working fine but when i select a record and delete a record it giving me error: Type Error occurred when creating object

 
Here my massaction code from ui_component

<massaction name=”listing_massaction”>
<argument name=”data” xsi:type=array>
<item name=”config” xsi:type=array>
<item name=”selectProvider” xsi:type=”string”>apriljune_testimonial_testimonial_listing.apriljune_testimonial_testimonial_listing.spinner_columns.ids</item>
<item name=”indexField” xsi:type=”string”>entity_id</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 items</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>

And here is the Controller:

<?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(*/*/’);
}
/**
* @return bool
*/

protected function _isAllowed()
{
return $this->_authorization->isAllowed(‘Apriljune_Testimonial::Testimonial);
}
}
1 Answers
Qaisar Satti Staff answered 5 years ago

Hi,

There are many errors in your code. First

$selectedIds = $this->getRequest()->getParams()[‘selected’];

should be

$selectedIds = $this->getRequest()->getParams('selected');

Another thing will be

$collectionSize = count($selectedIds);
foreach ($selectedIds as $_id) {
$testimonial = $this->testimonial->getItems()[$_id];
$testimonial->delete();
}

should be

$items = $this->testimonial->addFieldToFilter('entity_id', array('in' => $selectedIds));
foreach ($items as $item) {
  $item->delete();
}