Question

I built up a grid with mass action similiar to this examples:

AbstractMassAction.php

Mass Cancel

When executing the code (see below) I get the following error message:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause', query was: SELECT main_table.* FROM my_custom_table AS main_table WHERE (IN('47', '48', '49')) AND ( IN('49'))

It seems that the code cannot identify the table columns when using the filter. Does anyone know what is the reason for this?

My Mass Action:

use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Vendor\MyModule\Model\ResourceModel\MyModel\CollectionFactory;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;


/**
 * Class ExportXML
 */
class ExportXml extends AbstractMassAction {


    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $fileFactory;

    /**
     * @var OrderFactory $orderFactory
     */
    private $orderFactory;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param CollectionFactory $collectionFactory
     * @param Filter $filter
     */
    public function __construct(
            \Magento\Backend\App\Action\Context $context, 
            CollectionFactory $collectionFactory,
            Filter $filter) {
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context, $filter);
    }

    /**
     * Export rates grid to XML format
     *
     * @return ResponseInterface
     */
    protected function massAction(AbstractCollection $collection) {
        ...

My Abstract Mass Action

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use vendor\module\Model\ResourceModel\DebitReportModel\CollectionFactory;



abstract class AbstractMassAction extends \Magento\Backend\App\Action {
    /**
     * Authorization level of a basic admin session
     *
     * @see _isAllowed()
     */
    const ADMIN_RESOURCE = 'Micobo_DebitPayment::manage';
    /**
     * @var string
     */
    protected $redirectUrl = '*/*/index';
    /**
     * @var Filter
     */
    protected $filter;
    /**
     * @var object
     */
    protected $collectionFactory;
    /**
     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(Context $context, Filter $filter)
    {
        parent::__construct($context);
        $this->filter = $filter;
//        $this->collectionFactory = $collectionFactory;
    }
    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    public function execute()
    {
        try {
            $collection = $this->filter->getCollection($this->collectionFactory->create());
            return $this->massAction($collection);
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
            /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            return $resultRedirect->setPath($this->redirectUrl);
        }
    }
    /**
     * Return component referer url
     * TODO: Technical dept referer url should be implement as a part of Action configuration in in appropriate way
     *
     * @return null|string
     */
    protected function getComponentRefererUrl()
    {
        return $this->filter->getComponentRefererUrl()?: 'customer/*/index';
    }
    /**
     * Execute action to collection items
     *
     * @param AbstractCollection $collection
     * @return ResponseInterface|ResultInterface
     */
    abstract protected function massAction(AbstractCollection $collection);
}
Was it helpful?

Solution

It creates an issue because you didn't define $_idFieldName in your ResourceModel Collection

Please add this line in Vendor\MyModule\Model\ResourceModel\MyModel\Collectionfile.

     /**
     * @var string
     */
    protected $_idFieldName = 'YOUR_PRIMARY_KEY';

Where $_idFieldName is your table primary key.

OTHER TIPS

In my case, the problem appeared because I put parent class invocation before the methods defining. And even though I had

protected $_idFieldName = 'YOUR_PRIMARY_KEY';

that didn't work me.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top