Hello, I hope you can help me. I have this situation: I want to implement the MassDelete action but it is not working. This is what I have in the code.
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$collectionSize = $collection->getSize();
foreach ($collection as $item) {
$item->delete();
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
} <block class="Magento\Backend\Block\Widget\Grid\Massaction" name="blog_post_massaction" as="grid.massaction">
<arguments>
<argument name="massaction_id_field" xsi:type="string">post_id</argument>
<argument name="form_field_name" xsi:type="string">ids</argument>
<argument name="use_select_all" xsi:type="string">1</argument>
<argument name="options" xsi:type="array">
<item name="disable" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="string">manageblog/operation/massDelete</item>
</item>
</argument>
</arguments>
</block>
You can do so by replacing your for each with the following code:
foreach ($collection as $item) {
$model=$this->registrationFactory->create()->load($item->getId());
$model->delete();
}
Take a couple references from the core, e.g.:
\Magento\Customer\Controller\Adminhtml\Index\MassDelete::massAction():
protected function massAction(AbstractCollection $collection)
{
$customersDeleted = 0;
foreach ($collection->getAllIds() as $customerId) {
$this->customerRepository->deleteById($customerId);
$customersDeleted++;
}
//snip...
}
Hello @juancamilo1a94
you can use the below code to execute:
$collection = $this->filter->getCollection($this->collectionFactory->create());
if (isset($collection) && $collection->getSize() > 0) {
$collection->walk('delete');
}
Please follow the below code for massaction delete
$collection = $this->filter->getCollection($this->collectionFactory->create());
$count = 0;
foreach ($collection as $model) {
$model->delete();
$count++;
}Thanks.