cancel
Showing results for 
Search instead for 
Did you mean: 

How do I add an attribute to a custom EAV model collection?

How do I add an attribute to a custom EAV model collection?

I've created a custom module with an EAV model, and am now trying to render it into an admin grid. I've defined the attributes and they're being saved properly, but when I try to run `addAttributeToSelect('*')` to the model collection (`\Namespace\Module\Model\Obj\CollectionFactory`), `addAttributeToSelect` is not defined. My obj collection class extends `

\Magento\Eav\Model\Entity\Collection\AbstractCollection`

 

 

 

3 REPLIES 3

Re: How do I add an attribute to a custom EAV model collection?

Hi @nbgrommet,

 

Can you share more code?

 

From provided information I can assume that you are calling addAttributeToSelect on CollectionFactory... Didn't you forget to create the collection ($collectionFactory->create()->addAttributeToSelect('*')) 

Re: How do I add an attribute to a custom EAV model collection?

Hi - here's the rest of the code:

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 collection factory extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

 

Thanks

Re: How do I add an attribute to a custom EAV model collection?

`addAttributeToSelect()` only exists for EAV collections (inheriting `Magento\Eav\Model\Entity\Collection\AbstractCollection`)

 

For regular collections you have to use `addFieldToSelect()`