Magento 2 – Extending cataloginventory_stock_item table

I want to extend the cataloginventory_stock_item with one more column, so that I would be able to show this data in a Catalog Products grid.

I’ve read that in order to properly extend the core Magento tables we need to create a new table and join the data.

I’ve added new table but am not exactly sure how to properly extend the vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock/Item/Collection.php. collection with a field from the new table?

I am able to solve my main task (adding grid column) with different approach consisting of the following steps:

  1. Adding a new Field Strategy for MagentoCatalogUiDataProviderProductProductDataProvider via etc/adminhtml/di.xml :
    <?xml version="1.0" encoding="utf-8" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCatalogUiDataProviderProductProductDataProvider">
            <arguments>
                <argument name="addFieldStrategies" xsi:type="array">
                    <item name="simple_products_qty" xsi:type="object">[VendorName][ModuleName]UiDataProviderProductAddSimpleProductsQtyFieldToCollection</item>
                </argument>
            </arguments>
        </type>
    </config>
  1. Adding a strategy class file:
<?php

namespace [VendorName][ModuleName]UiDataProviderProduct;

use MagentoFrameworkDataCollection;
use MagentoUiDataProviderAddFieldToCollectionInterface;

/**
 * Class AddQuantityFieldToCollection
 */
class AddSimpleProductsQtyFieldToCollection implements AddFieldToCollectionInterface
{
    /**
     * {@inheritdoc}
     */
    public function addField(Collection $collection, $field, $alias = null)
    {
        $collection->joinField(
            'simple_products_qty',
            '[vendor_name]_[module_name]_cataloginventory_stock_item',
            'simple_products_qty',
            'item_id=entity_id',
            null,
            'left'
        );
    }
}
  1. Adding new column to product_listing grid via view/adminhtml/ui_component/product_listing.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">
    <columns name="product_columns">
        <column name="simple_products_qty" sortOrder="500">
            <settings>
                <addField>true</addField>
                <filter>textRange</filter>
                <label translate="true">Simple Products Quantity</label>
            </settings>
        </column>
    </columns>
</listing>

But what I don’t like in this approach is that the main collection query …

SELECT `e`.*, `at_simple_products_qty`.`simple_products_qty` FROM `catalog_product_entity` AS `e`
LEFT JOIN `cataloginventory_stock_item` AS `at_qty` ON (at_qty.`product_id`=e.entity_id) AND (at_qty.stock_id=1)

… directly joins catalog_product_entity with my new table [vendor_name]_[module_name]_cataloginventory_stock_item.

LEFT JOIN `[vendor_name]_[module_name]_cataloginventory_stock_item` AS `at_simple_products_qty` ON (at_simple_products_qty.`item_id`=e.entity_id)

It is able to do so, and does work, because item_id and entity_id have the same value, but I think the more proper join would be through cataloginventory_stock_item instead.

Can you please share the advice which will point me in the right direction?

How can module-catalog-inventory/Model/ResourceModel/Stock/Item/Collection.php be joined with a field from new table?

Thank you.