3

I'm trying to perform mass delete action on my custom admin grid in Magento 2. but while I try to perform it displays this error.

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Magento\Framework\View\Element\UiComponent\DataProvider\Document::delete

MassDelete.php

<?php
namespace Helios\RequestQuote\Controller\Adminhtml\Post;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Helios\RequestQuote\Model\ResourceModel\RequestQuote\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)
{
    $this->filter = $filter;
    $this->collectionFactory = $collectionFactory;
    parent::__construct($context);
}

/**
 * This is Execute Action.
 */
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));

   }
}

di.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Helios\RequestQuote\Model\ResourceModel\RequestQuote\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">requestquote</argument>
            <argument name="resourceModel" xsi:type="string">Helios\RequestQuote\Model\ResourceModel\RequestQuote</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="example_post_grid_data_source" xsi:type="string">Helios\RequestQuote\Model\ResourceModel\RequestQuote\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Collextion.php

      <?php
  /**
  * Resource Quote Resource Collection
  */
  namespace Helios\RequestQuote\Model\ResourceModel\RequestQuote;

  class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  {
    /**
    * Resource initialization.
    */
    protected $_idFieldName = 'post_id';

    protected function _construct()
    {
        $this->_init('Helios\RequestQuote\Model\RequestQuote','Helios\RequestQuote\Model\ResourceModel\RequestQuote');
    }
  }

ui_component/layout.xml

      <?xml version="1.0" encoding="UTF-8"?>
  <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
      <!--Declare data source, columns list, button...-->
      <argument name="data" xsi:type="array">
          <item name="js_config" xsi:type="array">
              <item name="provider" xsi:type="string">helios_requestquote_grid.example_post_grid_data_source</item>
              <item name="deps" xsi:type="string">helios_requestquote_grid.example_post_grid_data_source</item>
          </item>

          <!--Declare the data source name which will be defined below-->
          <item name="spinner" xsi:type="string">example_post_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" translate="true">Add New Blog</item>
                  <item name="class" xsi:type="string">primary</item>
                  <item name="url" xsi:type="string">*/*/new</item>
              </item>
          </item>
      </argument>

      <!--The data source-->
      <dataSource name="example_post_grid_data_source">
          <argument name="dataProvider" xsi:type="configurableObject">
              <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
              <argument name="name" xsi:type="string">example_post_grid_data_source</argument>
              <argument name="primaryFieldName" xsi:type="string">id</argument>
              <argument name="requestFieldName" xsi:type="string">id</argument>
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                      <item name="update_url" xsi:type="url" path="mui/index/render"/>
                      <item name="storageConfig" xsi:type="array">
                          <item name="indexField" xsi:type="string">id</item>
                      </item>
                  </item>
              </argument>
          </argument>
      </dataSource>

      <!-- For Creating a container -->
      <container name="listing_top">
          <argument name="data" xsi:type="array">
              <item name="config" xsi:type="array">
                  <item name="template" xsi:type="string">ui/grid/toolbar</item>
                  <item name="stickyTmpl" xsi:type="string">ui/grid/sticky/toolbar</item>
              </item>
          </argument>
      </container>

      <!-- For Displaying Bookmarks -->
      <container name="listing_top">
          <bookmark name="bookmarks">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="storageConfig" xsi:type="array">
                          <item name="namespace" xsi:type="string">helios_requestquote_post_listing</item>
                      </item>
                  </item>
              </argument>
          </bookmark>
      </container>

      <!-- For Displying Paging -->
      <paging name="listing_paging">
          <argument name="data" xsi:type="array">
              <item name="config" xsi:type="array">
                  <item name="storageConfig" xsi:type="array">
                      <item name="provider" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.listing_top.bookmarks</item>
                      <item name="namespace" xsi:type="string">current.paging</item>
                  </item>
                  <item name="selectProvider" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.example_post_columns.ids</item>
              </item>
          </argument>
      </paging>

      <!-- For Displaying Mass Action -->
      <massaction name="listing_massaction">
          <argument name="data" xsi:type="array">
              <item name="config" xsi:type="array">
                  <item name="selectProvider" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.example_post_columns.ids</item>
                  <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                  <item name="indexField" xsi:type="string">post_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="*/*/delete" />
                      <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 to delete selected posts?</item>
                      </item>
                  </item>
              </argument>
          </action>
      </massaction>

      <!-- For Displaying Filters -->
     <filters name="listing_filters">
          <argument name="data" xsi:type="array">
              <item name="config" xsi:type="array">
                  <item name="columnsProvider" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.example_post_columns</item>
                  <item name="storageConfig" xsi:type="array">
                      <item name="provider" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.listing_top.bookmarks</item>
                      <item name="namespace" xsi:type="string">current.filters</item>
                  </item>
                  <item name="childDefaults" xsi:type="array">
                      <item name="provider" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.listing_top.listing_filters</item>
                      <item name="imports" xsi:type="array">
                          <item name="visible" xsi:type="string">helios_requestquote_grid.helios_requestquote_grid.example_post_columns.${ $.index }:visible</item>
                      </item>
                  </item>
              </item>
          </argument>
      </filters>

      <columns name="example_post_columns">
          <!--The list of columns-->
          <selectionsColumn name="ids">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="indexField" xsi:type="string">id</item>
                  </item>
              </argument>
          </selectionsColumn>
          <column name="id">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="filter" xsi:type="string">text</item>
                      <item name="sorting" xsi:type="string">asc</item>
                      <item name="label" xsi:type="string" translate="true">ID</item>
                  </item>
              </argument>
          </column>
          <column name="name">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="filter" xsi:type="string">text</item>
                      <item name="sorting" xsi:type="string">asc</item>
                      <item name="label" xsi:type="string" translate="true">Name</item>
                  </item>
              </argument>
          </column>
          <column name="email">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="filter" xsi:type="string">text</item>
                      <item name="sorting" xsi:type="string">asc</item>
                      <item name="label" xsi:type="string" translate="true">Email</item>
                  </item>
              </argument>
          </column>
          <column name="company">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="filter" xsi:type="string">text</item>
                      <item name="sorting" xsi:type="string">asc</item>
                      <item name="label" xsi:type="string" translate="true">Company</item>
                  </item>
              </argument>
          </column>
          <column name="phone">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="filter" xsi:type="string">text</item>
                      <item name="sorting" xsi:type="string">asc</item>
                      <item name="label" xsi:type="string" translate="true">Phone</item>
                  </item>
              </argument>
          </column>
          <column name="website">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="filter" xsi:type="string">text</item>
                      <item name="sorting" xsi:type="string">asc</item>
                      <item name="label" xsi:type="string" translate="true">Website</item>
                  </item>
              </argument>
          </column>
          <actionsColumn name="actions">
              <argument name="data" xsi:type="array">
                  <item name="config" xsi:type="array">
                      <item name="indexField" xsi:type="string">id</item>
                      <item name="sortOrder" xsi:type="number">200</item>
                  </item>
              </argument>
          </actionsColumn>
      </columns>
  </listing>

I tried each and every solutions on stack but it's not working. I tried using custom query at that time it's worknig perfectly but i dont want custom queries.

2 Answers 2

8

you need to make change in di.xml file.

Replace this Line: Company\Module\Model\ResourceModel\Test\Collection

Replace With this: Company\Module\Model\ResourceModel\Test\Grid\Collection

In virtual type: you need to add \Grid\ before collection

<virtualType name="Company\Module\Model\ResourceModel\Test\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">

Same in type:

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="company_module_listing_data_source" xsi:type="string">Company\Module\Model\ResourceModel\Test\Grid\Collection</item>
            </argument>
        </arguments>
    </type>

Run the command : php bin/magento setup:di:compile

0
0

I think you need to change these files:

di.xml:

you need to add eventobject and eventPrfix:

<?xml version="1.0"?>
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <virtualType name="Helios\RequestQuote\Model\ResourceModel\RequestQuote\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
          <arguments>
              <argument name="mainTable" xsi:type="string">requestquote</argument>
              <argument name="eventPrefix" xsi:type="string">requestquote_grid_collection</argument>
              <argument name="eventObject" xsi:type="string">requestquote_collection</argument>
              <argument name="resourceModel" xsi:type="string">Helios\RequestQuote\Model\ResourceModel\RequestQuote</argument>
          </arguments>
      </virtualType>
      <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
          <arguments>
              <argument name="collections" xsi:type="array">
                  <item name="example_post_grid_data_source" xsi:type="string">Helios\RequestQuote\Model\ResourceModel\RequestQuote\Grid\Collection</item>
              </argument>
          </arguments>
      </type>
  </config>

Collection.php

  <?php
  /**
  * Resource Quote Resource Collection
  */
  namespace Helios\RequestQuote\Model\ResourceModel\RequestQuote;

  class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  {
  /**
  * Resource initialization.
  */
  protected $_idFieldName = 'post_id';

  protected $_eventPrefix = 'requestquote_grid_collection';

  protected $_eventObject = 'requestquote_collection';

  protected function _construct()
  {
      $this->_init('Helios\RequestQuote\Model\RequestQuote','Helios\RequestQuote\Model\ResourceModel\RequestQuote');
  }

  public function getSelectCountSql()
{
    $countSelect = parent::getSelectCountSql();
    $countSelect->reset(\Zend_Db_Select::GROUP);
    return $countSelect;
}

protected function _toOptionArray($valueField = 'post_id', $labelField = 'post_id', $additional = [])
{
    return parent::_toOptionArray($valueField, $labelField, $additional);
}

public function addFieldToSearchFilter($field, $condition = null)
{
    $field = $this->_getMappedField($field);
    $this->_select->orWhere($this->_getConditionSql($field, $condition));
    return $this;
}
  }

you need to create this Collection.php in this location Helios\RequestQuote\Model\ResourceModel\RequestQuote\Grid

Collection.php

<?php

namespace Helios\RequestQuote\Model\ResourceModel\RequestQuote\Grid;

class Collection extends \Helios\RequestQuote\Model\ResourceModel\RequestQuote\Collection implements \Magento\Framework\Api\Search\SearchResultInterface
{
    protected $_aggregations;

    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
        $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    )
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }


    /**
     * @return \Magento\Framework\Search\AggregationInterface
     */
    public function getAggregations()
    {
        return $this->_aggregations;
    }

    /**
     * @param \Magento\Framework\Search\AggregationInterface $aggregations
     * @return $this
     */
    public function setAggregations($aggregations)
    {
        $this->_aggregations = $aggregations;
    }


    /**
     * Retrieve all ids for collection
     * Backward compatibility with EAV collection
     *
     * @param int $limit
     * @param int $offset
     * @return array
     */
    public function getAllIds($limit = null, $offset = null)
    {
        return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
    }

    /**
     * Get search criteria.
     *
     * @return \Magento\Framework\Api\SearchCriteriaInterface|null
     */
    public function getSearchCriteria()
    {
        return null;
    }

    /**
     * Set search criteria.
     *
     * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
    {
        return $this;
    }

    /**
     * Get total count.
     *
     * @return int
     */
    public function getTotalCount()
    {
        return $this->getSize();
    }

    /**
     * Set total count.
     *
     * @param int $totalCount
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setTotalCount($totalCount)
    {
        return $this;
    }

    /**
     * Set items list.
     *
     * @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setItems(array $items = null)
    {
        return $this;
    }
}

Finally, Run

php bin/magento setup:di:compile

php bin/magento cache:clean

Then, Check it, let me know if you have the issue

12
  • Nope.... Still having the same error. Commented Jul 6, 2017 at 11:50
  • Invalid method Magento\Framework\View\Element\UiComponent\DataProvider\Document::delete Commented Jul 6, 2017 at 11:51
  • @are you create Helios\RequestQuote\Model\ResourceModel\RequestQuote\Grid\Collection this collection file Commented Jul 6, 2017 at 11:54
  • I created that Grid folder in RequestQuote and created the collection.php file but at that time i had Grid class not found exeption. Commented Jul 6, 2017 at 11:58
  • I have updated the answer, change yourdi.xml file Commented Jul 6, 2017 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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