1

I have a custom table which contains product id, I want to add an extra attribute column. so how can I get dropdown attribute value of the product in my custom table? if the dropdown attribute is not selected the data will be display as blank.

namespace Demo\Test\Model\ResourceModel\Grid;

class Collection extends SearchResult
{
     /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'custom_table',
        $resourceModel = \Demo\Test\Model\ResourceModel\Load::class
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }
    protected function _initSelect()
    {
        $this->getSelect()->from(['main_table' => $this->getMainTable()]);
        return $this;
    }
}
0

You could use joinLeft to get the value of attribute dropdown column which stored in catalog_product_entity_int

<?php
namespace Demo\Test\Model\ResourceModel\Grid;

use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Eav\Model\ResourceModel\Entity\Attribute as EntityAttribute;
use Psr\Log\LoggerInterface as Logger;

class Collection extends SearchResult
{
    protected $_eavAttribute;

    /**
     * Initialize dependencies.
     *
     * @param EntityAttribute $eavAttribute
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityAttribute $eavAttribute,
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'custom_table',
        $resourceModel = \Demo\Test\Model\ResourceModel\Load::class
    ) {
        $this->_eavAttribute = $eavAttribute;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }

    /**
     * Init Select
     *
     * @return $this
     */
    protected function _initSelect()
    {
        parent::_initSelect();
        $this->_joinFields();
        return $this;
    }

    /**
     * Join customers
     *
     * @return $this
     */


     protected function _joinFields()
        {
            $attributeId = $this->_eavAttribute->getIdByCode('catalog_product', 'your_attribute_code');

            if($attributeId) {
                $this->getSelect()->joinLeft(
                    ['attribute' => $this->getTable('catalog_product_entity_int')],
                    'attribute.entity_id = main_table.product_id AND attribute.attribute_id = ' . $attributeId,
                    []
                )->joinLeft(
                    ['attributevalue' => $this->getTable('eav_attribute_option_value')],
                     'attribute.value = attributevalue.option_id',
                    [
                        'attribute_value' => 'attributevalue.value'
                    ]
                );
            }

            return $this;
        }

}

Hope this help.

  • i can get only numeric value, how can i get actual value of product attribute? – user75757 May 2 at 9:57
  • 1
    You could continue join with table eav_attribute_option_value – Tuyen Nguyen May 2 at 10:07
  • @Neeta the answer has been updated – Tuyen Nguyen May 2 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy