Magento CE 2.2.0 dev without sample data installed
Create Magento's module for admin using my tables has been successed
MassDelete action works
I have a problem here. My MassDelete doesn't work. In MassDelete action I am getting the collection but while deleting the item it give me above error. Here is my MassDelete.php code:
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 element(s) have been deleted.', $collectionSize));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Cms_General::city');
}
And this is my ui_component:
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">cms_cities_grid.cms_cities_grid.cms_cities_columns.ids</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
<item name="indexField" xsi:type="string">city_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="general/cities/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 to delete selected city(s)?</item>
</item>
</item>
</argument>
</action>
</massaction>
And I got this error:
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Magento\Framework\View\Element\UiComponent\DataProvider\Document::delete
I added $_idFieldName in collection.php and still got error too.
@dejan25 thank you for your report.
Please identify which version of Magento this issue occurs on.
Please format your issue description according to the Issue reporting guidelines.
@veloraven I use magento 2.2.0. Thanks
You should write a good configuration in file di.xml like this:
<virtualType name="CityGridFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
<arguments>
<argument name="appliers" xsi:type="array">
<item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
<item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
</argument>
</arguments>
</virtualType>
<virtualType name="CityGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
<arguments>
<argument name="collection" xsi:type="object" shared="false">Cms\General\Model\Resource\City\Collection</argument>
<argument name="filterPool" xsi:type="object" shared="false">CityGridFilterPool</argument>
</arguments>
</virtualType>
<virtualType name="Cms\General\Model\ResourceModel\City\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">cms_general_city</argument>
<argument name="resourceModel" xsi:type="string">Cms\General\Model\ResourceModel\City</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="cms_general_city_listing_data_source" xsi:type="string">Cms\General\Model\ResourceModel\City\Grid\Collection</item>
</argument>
</arguments>
</type>
and then, you should run setup:di:compile again.
I got a similar issue. here is my di.xml
`
<virtualType name="Training\ComputerGame\Model\ResourceModel\ComputerGame\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">computer_games</argument>
<argument name="resourceModel" xsi:type="string">Training\ComputerGame\Model\ResourceModel\ComputerGame</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<!--data provider name which used in grid ui component file -->
<item name="game_record_grid_list_data_source" xsi:type="string">Training\ComputerGame\Model\ResourceModel\ComputerGame\Collection</item>
</argument>
</arguments>
</type>
`
when I changed Training\ComputerGame\Model\ResourceModel\ComputerGame\Collection to Training\ComputerGame\Model\ResourceModel\ComputerGame\Grid\Collection, it's show class not found.
I have found the solution to solve the problem
in di.xml we must create a virtualType point to model
Here is my di.xml
`
<virtualType name="Training\ComputerGame\Model\ResourceModel\ComputerGame\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">computer_games</argument>
<argument name="resourceModel" xsi:type="string">Training\ComputerGame\Model\ResourceModel\ComputerGame</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="game_record_grid_list_data_source" xsi:type="string">Training\ComputerGame\Model\ResourceModel\ComputerGame\Grid\Collection</item>
</argument>
</arguments>
</type>
`
@dejan25, thank you for your report.
This seems to be correct Magento behavior. Please refer to the Community Forums or the Magento Stack Exchange site for advice or general discussion about this.
Otherwise you may submit Pull Request with the suggested changes.
Please refer to the Community Forums or the Magento Stack Exchange site for advice or general discussion about this issue. The GitHub issue tracker is intended for Magento Core technical issues only.
Most helpful comment
I have found the solution to solve the problem
in di.xml we must create a virtualType point to model
Here is my di.xml
`
`