Question

In Magento2, I understood we have to use repositories instead of collections. What I do not understand is how to add custom attributes to repository.

If I do something like this:

    $searchCriteria = $this->searchCriteriaBuilder
        ->addFilter(ProductInterface::STATUS, Status::STATUS_ENABLED)
        ->addFilter(ProductInterface::VISIBILITY, array(Visibility::VISIBILITY_IN_SEARCH, Visibility::VISIBILITY_BOTH), 'in')
        ->create();

    $products = $this->productRepositoryInterface->getList($searchCriteria);

I have a list of products, but I do not have all the attributes I need. How can achieve this? Something like the addAttributeToSelect in Magento 1.

Was it helpful?

Solution

I understood we have to use repositories instead of collections

not necessarily;

AFAIK, when you use repositories you adhere to Magento 2 service contracts, which means that service interfaces and data interfaces are defined (but extensible by third-party modules).

Making it possible at run-time to change the result of the call to the getList() method of a repository would violate this contract.

To retrieve the attributes you need at run-time you can use collections. Magento 2 framework itself makes an heavy use of collections.

Otherwise you should implement a module which defines a new data interface (that is, the new set of attributes) for that entity.

Hope it sheds some light.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top