0

i have created a custom attribute user_id of [type => int] for catalog_products_entity in my Install Data script

$productSetup = $this->eavSetupFactory->create(['setup' => $setup]); $productSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'user_id', ['type' => 'int']);

now I am trying to make a listing of products on the admin panel by using the catalog-product collection model. I have duplicated the ProductDataProvider for product_listing as the data provider for my listing UI component and created my own product collection class extending \Magento\Catalog\Model\ResourceModel\Product\Collection

here is my custom data provider that i am using instead of ProductDataProvider:

<?php
namespace Company\Module\Ui\DataProvider\Product;

use Company\Module\Model\ResourceModel\Product\CollectionFactory;

class ProductDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    /**
     * Product collection
     *
     * @var \Company\Module\Model\ResourceModel\Product\Collection
     */
    protected $collection;

    /**
     * @var \Magento\Ui\DataProvider\AddFieldToCollectionInterface[]
     */
    protected $addFieldStrategies;

    /**
     * @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
     */
    protected $addFilterStrategies;

    /**
     * Construct
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
     * @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $addFieldStrategies = [],
        array $addFilterStrategies = [],
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->collection = $collectionFactory->create();
        $this->addFieldStrategies = $addFieldStrategies;
        $this->addFilterStrategies = $addFilterStrategies;
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (!$this->getCollection()->isLoaded()) {
            $this->getCollection()->load();
        }
        $items = $this->getCollection()->toArray();

        return [
            'totalRecords' => $this->getCollection()->getSize(),
            'items' => array_values($items),
        ];
    }

    /**
     * Add field to select
     *
     * @param string|array $field
     * @param string|null $alias
     * @return void
     */
    public function addField($field, $alias = null)
    {
        if (isset($this->addFieldStrategies[$field])) {
            $this->addFieldStrategies[$field]->addField($this->getCollection(), $field, $alias);
        } else {
            parent::addField($field, $alias);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function addFilter(\Magento\Framework\Api\Filter $filter)
    {
        if (isset($this->addFilterStrategies[$filter->getField()])) {
            $this->addFilterStrategies[$filter->getField()]
                ->addFilter(
                    $this->getCollection(),
                    $filter->getField(),
                    [$filter->getConditionType() => $filter->getValue()]
                );
        } else {
            parent::addFilter($filter);
        }
    }
}

here is my custom collection class:
namespace Company\Module\Model\ResourceModel\Product;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{
  const USER_ID = 'user_id';
  protected function _initSelect()
  {
     parent::_initSelect();
     $field = self::USER_ID;
     $this->addAttributeToSelect('*');
     $this->getSelect()->where("$field IS NOT NULL");
     return $this;
  }
}

but i am getting the following error enter image description here

I am new to Magento so I may be doing something incorrectly. Can anyone please tell me how I can get the data from all other product value tables like catalog_product_entity_int and catalog_product_entity_decimal etc. I want to get all the product entities with all their attributes and attribute values and then to be able to filter them using the custom attribute user_id.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.