6

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);
}
19

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.

  • Thank you! I did not find any hint that this field is required... Now it is working fine :-) – Tenobi Apr 7 '16 at 7:30
  • Glad to hear the it works for you. @Tenobi – Keyur Shah Apr 7 '16 at 9:24
  • @KeyurShah Thanks man you save my day while solving some one else's error – Murtuza Zabuawala May 10 '18 at 12:31
  • 1
    Glad that it helps you buddy :) @MurtuzaZabuawala – Keyur Shah May 10 '18 at 12:50
  • It's working for me! – Chirag Patel Aug 28 at 10:56
1

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.

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.