Magento – Magento 2 Custom Module Delete Issue [SOLVED]

collection;customdeletemagento2module

I'm using Magento 2 Version 2.1.0

{folder path}\magento2\app\code\Custom\Module\view\adminhtml\ui_component\custom_module_index.xml

<massaction name="listing_massaction">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="selectProvider" xsi:type="string">custom_module_index.custom_module_index.custom_module_columns.ids</item>
            <item name="indexField" xsi:type="string">posts_id</item>
        </item>
    </argument>
    <action name="delete">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">delete</item>
                <item name="label" xsi:type="string" translate="true">Delete</item>
                <item name="url" xsi:type="url" path="posts/massDelete"/>
                <item name="confirm" xsi:type="array">
                    <item name="title" xsi:type="string" translate="true">Delete items</item>
                    <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                </item>
            </item>
        </argument>
    </action>
</massaction>

{folder path}\magento2\app\code\Custom\Module\Controller\Adminhtml\Posts\MassDelete.php

Only giving code for execute() method

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('*/*/');
    }

When i'm trying to Delete record from listing, It's giving below error.

2 exception(s): Exception #0 (Zend_Db_Statement_Exception):
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where
clause', query was: SELECT COUNT(*) FROM posts AS main_table WHERE
(“ IN('')) Exception #1 (PDOException): SQLSTATE[42S22]: Column not
found: 1054 Unknown column '' in 'where clause'

I already compile the di.xml file & also try to remove folder from var/generation. Still same issue & error.

Best Answer

Check if you have following line in your Model\ResourceModel\Module\Collection.php file:

protected $_idFieldName = 'module_id';