1

My massaction functionalities are working fine. According to CodingStandard it shows this warning.

----------------------------------------------------------------------
 38 | WARNING | Model LSD method delete() detected in loop
----------------------------------------------------------------------

I have attached my MassDelete code below.

<?php

namespace modulename\Wallet\Controller\Adminhtml\Transaction;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use modulename\Wallet\Model\ResourceModel\Transaction\CollectionFactory;

class MassDelete extends \Magento\Backend\App\Action
{
    public function __construct(
        Context $context,
        Filter $filter,
        CollectionFactory $collectionFactory
    ) {
        $this->_filter = $filter;
        $this->_collectionFactory = $collectionFactory;
        parent::__construct($context);
    }
    public function execute()
    {
        $data = $this->getRequest()->getParams();
        $collection = $this->_collectionFactory->create();
        $collection->addFieldToFilter('transaction_id', ['in' => $data]);
        $recordDeleted = 0;
        foreach ($collection->getItems() as $auctionProduct) {
            $auctionProduct->setId($auctionProduct->getId());
            $auctionProduct->delete();
            $recordDeleted++;
        }
        $this->messageManager->addSuccess(
            __('A total of %1 record(s) have been deleted.', $recordDeleted)
        );

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

LSD comes from load/save/delete. You should not call these methods inside loops.
But in this case, if you want to hide the dirt under the rug you can crete a new method in your class

private function deleteItem($item)
{
    $item->delete();
}

and replace the line $auctionProduct->delete(); with

$this->deleteItem($auctionProduct);

and the code sniffer will stop complaining.

  • You're welcome. If it works, mark the answer as accepted in order to help future readers. – Marius Feb 9 '18 at 7:22
  • in your post please edit this line $this-deleteItem($auctionProduct); u have missed $this-> – chris Feb 9 '18 at 7:30
  • Done. Sorry about this. – Marius Feb 9 '18 at 7:33
0
  • You're using the delete() method inside a loop which in not recommended and has to be avoided.
  • ok. Thanks. Do you know any alternative method for this ? – chris Feb 8 '18 at 10:51
  • I have done like this only.. but it shows warning. – chris Feb 8 '18 at 11:16
0

If hiding the LSD delete method behind the private visibility domain doesn't impress PHPCS you may try calling your method inside an array_walk loop instead of foreach. From what I know array_walk is less performant than foreach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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