1

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?

3
  • please check this link magento.stackexchange.com/questions/91807/… . It might be helpful in your case. Commented Feb 18, 2020 at 14:14
  • Thank you for the link. If I understood the post correctly, those examples allow you to join a core table to your custom table. But my second grid has no custom table, it only needs the data in the core table. So I need to be able to call the sales order item collection to populate my custom grid
    – Lou Nik
    Commented Feb 18, 2020 at 16:15
  • I am also stuck in the same scenario. I want to achieve the same result. How to make a Custom Model, Resource Model and a Collection for the "sales_order_items" table. Which is Magento Core Database Table. No Help i found anywhere. Commented Oct 12, 2020 at 10:32

0

Your Answer

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

Browse other questions tagged or ask your own question.