I have a custom module with a custom table and I've created a ui component grid to show its data in Magento backend. This works fine.
Now I need a second admin grid were to show a collection of order items. The problem is that I'm not able to populate the grid with the records from the sales_order_item
table.
As a temporary workaround, I created a custom DataProvider to fetch the data from the order item collection interface but this way I had to rewrite all the filters and sorting methods. This way the second grid works as intended: it shows the filtered collection of order items correctly.
Still, I think it would be more efficient to just keep the original Magento functionality but I don't know how to bind the core sales order item table to my custom grid.
I simply tried to create a new Model/ResourceModel/Collection
that links to the core table but that doesn't work, I get the error: "class xx/Grid/Collection does not exists".
My code:
- Vendor/Module/Model/Name.php
namespace Vendor\Module\Model;
use Magento\Framework\Model\AbstractModel;
use Vendor\Module\Model\ResourceModel\Name as ResourceModel;
class Name extends AbstractModel
{
protected function _construct()
{
parent::_construct();
$this->_init(ResourceModel::class);
}
}
- Vendor/Module/Model/ResourceModel/Name.php
<?php
namespace Vendor\Module\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Name extends AbstractDb
{
const TABLENAME = "sales_order_item";
const KEY_ID = 'item_id';
protected function _construct()
{
$this->_init(self::TABLENAME, self::KEY_ID);
}
}
- Vendor/Module/Model/ResourceModel/Name/Collection.php
<?php
namespace Vendor\Module\Model\ResourceModel\Name;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Vendor\Module\Model\Name as Model;
use Vendor\Module\Model\ResourceModel\Name as ResourceModel;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(Model::class, ResourceModel::class);
}
}
- Vendor/Module/etc/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="Vendor\Module\Model\ResourceModel\Name\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">sales_order_item</argument>
<argument name="resourceModel" xsi:type="string">Vendor\Module\Model\ResourceModel\Name</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="vendor_module_name_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Name\Grid\Collection</item>
</argument>
</arguments>
</type>
</config>
As I said, this doesn't work, when I load the page I get the error "Class Vendor\Module\Model\ResourceModel\Name\Grid\Collection does not exist".
So how can I link my custom grid to the core sales_order_item
table?