4

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.

4
  • addAttributeToSelect() also exit in magento2.x ,You can use like ` $products->addAttributeToSelect('name)`
    – Amit Bera
    Commented Mar 23, 2016 at 16:51
  • Does it work with repositories too? Commented Mar 23, 2016 at 16:52
  • yes.i should works as per as my concept
    – Amit Bera
    Commented Mar 23, 2016 at 16:59
  • Just tried, it does not seem to work on repos, it only works on collections as far as I can see. Commented Mar 23, 2016 at 17:03

1 Answer 1

2

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.

1
  • Hi Alessandro, this is a n old question I forgot here :) . I came at your same conclusion. So thank you . Commented Jun 18, 2016 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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