0

It's my massAction controller:

<?php
/**
 * Created by PhpStorm.
 * User: pkushnerevich
 * Date: 19.10.18
 * Time: 18.20
 */

namespace Aty\TestTask\Controller\Adminhtml\AddedList;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;

class MassDelete extends \Magento\Backend\App\Action
{
    protected $filter;

    protected $collectionFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Ui\Component\MassAction\Filter $filter,
        \Aty\TestTask\Model\ResourceModel\InsSku\CollectionFactory $collectionFactory)
    {
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create())->walk('delete');
        $collectionSize = $collection->getSize();

        $this->messageManager->addSuccess(__('A total of %1 element(s) have been deleted.', $collectionSize));

        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('*/*/');
    }
}

Exception: Some transactions have not been committed or rolled back

After debugger I found, that it's here

vendor/magento/framework/Model/AbstractModel.php

 public function beforeDelete()
    {
        if (!$this->_actionValidator->isAllowed($this)) {
            throw new \Magento\Framework\Exception\LocalizedException(
                new \Magento\Framework\Phrase('Delete operation is forbidden for current area')
            );
        }

        $this->_eventManager->dispatch('model_delete_before', ['object' => $this]);
        $this->_eventManager->dispatch($this->_eventPrefix . '_delete_before', $this->_getEventData());
        $this->cleanModelCache();
        return $this;
    }
2
  • are you sure you getting proper collection data in variable $collection after line $collection = $this->filter->getCollection($this->collectionFactory->create())->walk('delete'); ? Oct 19, 2018 at 18:36
  • @Shashank Kumrawat , no but it must, I think, delete getted element
    – Pasha
    Oct 19, 2018 at 20:07

1 Answer 1

1

you need to do code like this.

namespace Aty\TestTask\Controller\Adminhtml\AddedList;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Amasty\TestTask\Model\ResourceModel\InsSkuFactory;

class MassDelete extends \Magento\Backend\App\Action
{
    protected $filter;

    protected $collectionFactory;
    protected $InsSkuFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Ui\Component\MassAction\Filter $filter,
        \Amasty\TestTask\Model\ResourceModel\InsSku\CollectionFactory $collectionFactory,
        InsSkuFactory $InsSkuFactory)
    {
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
        $this->InsSkuFactory = $InsSkuFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $collectionSize = $collection->getSize();

        foreach ($collection as $item) 
        {
            $model = $this->InsSkuFactory->create();
            $model->load($item->getId());
            $model->delete();
        }

        $this->messageManager->addSuccess(__('A total of %1 element(s) have been deleted.', $collectionSize));

        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('*/*/');
    }
}
7
  • it's doesn't work( the same error
    – Pasha
    Oct 22, 2018 at 6:35
  • now you need to first confirm that, you are getting proper result into collection. Oct 22, 2018 at 6:56
  • Argument 1 passed to Magento\Framework\Model\ResourceModel\Db\AbstractDb::load() must be an instance of Magento\Framework\Model\AbstractModel, string given, called in app/code/Aty/TestTask/Controller/Adminhtml/AddedList/MassDelete.php on line 45
    – Pasha
    Oct 22, 2018 at 7:02
  • load doesn't work
    – Pasha
    Oct 22, 2018 at 7:02
  • Is InsSkuFactory model or ResourceModel?
    – Pasha
    Oct 22, 2018 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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