0

Using Magento 2.1.3: I want to display custom EAV attributes in a admin grid, but can't figure out how to properly add them to the data provider collection. I would assume this would work, but it's throwing an undefined method error on addAttributeToSelect:

namespace Vendor\Namespace\Ui\Component\Listing\DataProviders\Namespace\Obj;

class Grid extends \Magento\Ui\DataProvider\AbstractDataProvider
{    
public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    \Vendor\Namespace\Model\ResourceModel\Obj\CollectionFactory $collectionFactory,
    array $meta = [],
    array $data = []
) {
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    $this->collection = $collectionFactory->create();
    $this->collection->addAttributeToSelect('*');
    $this->collection->load();
}
}

My \Vendor\Namespace\Model\ResourceModel\Obj\Collection class extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

I have also now tried loading the product model, then getting the collection, and still no luck, however loading a obj directly contains the attributes:

....
_construct(
    ....
    \Vendor\Namespace\Model\ObjFactory $objFactory,
     ....
) {
   $obj = $objFactory->create()->load([id]);
   var_dump($obj->getData()); // has attributes
   $objCollection = $obj->getCollection();
   foreach ($objCollection as $cObj) {
        var_dump($cObj->getData()); // no attributes
   }
}
  • It's custom EAV grid? Or which grid? – Sohel Rana Feb 3 '17 at 16:30
  • Hi @SohelRana - it's a custom grid, defined in a ui_component xml file. – dbcn Feb 3 '17 at 16:50
0

The resource model you're extending does not have an addAttributeToSelect method and is not intended to be used for EAV entities.

Your collection should extend \Magento\Eav\Model\Entity\Collection\AbstractCollection instead.

  • Thanks @Andrew - that did it - I'm seeing a different issue now with the grid, but changing the parent collection class allowed me to run addAttributeToSelect('*') to the collection and I am seeing the attribute values in the loaded objects. Incidentally, changing the class also changed the main table name from 'main_table' to 'e' – dbcn Feb 10 '17 at 19:08
  • When loading EAV entities Magento tends to give the main table an alias of e (entity) instead of main_table – Andrew Noble Feb 10 '17 at 20:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.