cancel
Showing results for 
Search instead for 
Did you mean: 

Adding mass delete custom model

Adding mass delete custom model

hi,

I have my own custom model working correctly both in frontend and admin, only thing is missing is I cannot mass delete records, when I select records and confirm I want to delete them I get:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause', query was: SELECT COUNT(*) FROM `hoop_contacts_contact` AS `main_table` WHERE (`` IN('2', '3', '4', ...

so it looks like the id column (hoop_contacts_contact_id) it's somehow not passed to the generated query. But I can modify and delete single records.

 

Anybody can point me in the right direction?

Thanks a lot

4 REPLIES

Re: Adding mass delete custom model

Hello Rakesh,

thanks a lot for your answer. Here it is:

<?php
namespace Hoop\Contacts\Controller\Adminhtml\Contact;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Hoop\Contacts\Model\ResourceModel\Contact\CollectionFactory;

class MassDelete extends \Magento\Backend\App\Action
{
    /**
     * @var Filter
     */
    protected $filter;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;


    /**
     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
    {
        parent::__construct($context, $filter);
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
    }
    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    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('*/*/');
    }
}

Re: Adding mass delete custom model

Your Massdelete.php file is looking like correct, Could you share your listing grid ui_components listing.xml file in which you have declared 

<massaction name="listing_massaction">

command? 

If Issue Solved, Click Kudos/Accept As solutions.

Re: Adding mass delete custom model

the only xml where I have defined the listing_massaction command is Hoop/Contacts/view/adminhtml/ui_component/hoop_contacts_contacts.xml:

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts_data_source</item>
            <item name="deps" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">hoop_contacts_contacts_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string">Add New</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/Contact/new</item>
            </item>
        </item>
    </argument>
    <dataSource name="hoop_contacts_contacts_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Hoop\Contacts\Ui\Component\Listing\DataProviders\Hoop\Contacts\Contacts</argument>
            <argument name="name" xsi:type="string">hoop_contacts_contacts_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">hoop_contacts_contact_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    </item>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <paging name="listing_paging"/>
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts.hoop_contacts_contacts_columns.ids</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_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="hoop_contacts_contacts/contact/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>
    </listingToolbar>
    <columns name="hoop_contacts_contacts_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="hoop_contacts_contact_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <actionsColumn name="actions" class="Hoop\Contacts\Ui\Component\Listing\Column\Hoopcontactscontacts\PageActions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">107</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </actionsColumn>
        <column name="firstname">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Firstname</item>
                    <item name="sortOrder" xsi:type="number">105</item>
                </item>
            </argument>
        </column>
        <column name="lastname">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Lastname</item>
                    <item name="sortOrder" xsi:type="number">106</item>
                </item>
            </argument>
        </column>
        <column name="email">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Email</item>
                    <item name="sortOrder" xsi:type="number">107</item>
                </item>
            </argument>
        </column>
        <column name="company">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Company</item>
                    <item name="sortOrder" xsi:type="number">108</item>
                </item>
            </argument>
        </column>
        <column name="phone">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Phone</item>
                    <item name="sortOrder" xsi:type="number">109</item>
                </item>
            </argument>
        </column>
        <column name="message">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Message</item>
                    <item name="sortOrder" xsi:type="number">110</item>
                </item>
            </argument>
        </column>
        <column name="newsletter">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Newsletter</item>
                    <item name="sortOrder" xsi:type="number">111</item>
                </item>
            </argument>
        </column>
        <column name="creation_time">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Created at</item>
                    <item name="sortOrder" xsi:type="number">111</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

thanks again!

Re: Adding mass delete custom model

no help on this topic? I really would like to solve...

 

thanks