3

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.

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

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

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.