Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to specify attributes to load on ProductRepository::getList() #1341

Closed
Vinai opened this issue Jun 5, 2015 · 7 comments
Closed
Labels
Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Progress: needs update

Comments

@Vinai
Copy link
Contributor

Vinai commented Jun 5, 2015

Using Magento version 0.74.0-beta12, I'm trying to load a list of products, some of which belong to the Bag attribute set of the sample data.

Roughly this is the code I'm playing with:

        $this->searchCriteriaBuilder->addFilter($filters);
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $productList = $this->productRepository->getList($searchCriteria);

As far as I can tell the ProductRepository::getList() method only adds the default attribute set attributes to the underlying collection.

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
    $collection = $this->collectionFactory->create();
    $defaultAttributeSetId = $this->eavConfig
        ->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
        ->getDefaultAttributeSetId();
    $extendedSearchCriteria = $this->searchCriteriaBuilder->addFilter(
        [
            $this->filterBuilder
                ->setField('attribute_set_id')
                ->setValue($defaultAttributeSetId)
                ->create(),
        ]
    );
    foreach ($this->metadataService->getList($extendedSearchCriteria->create())->getItems() as $metadata) {
        $collection->addAttributeToSelect($metadata->getAttributeCode());
    }

How can I add additional attributes to the loaded products? As far as I can tell it is only possible to specify attribute filters using the SearchCriteria.
Or is it right to use \Magento\Catalog\Model\Resource\Product\Collection directly?

Thanks for clarifying.

@kokoc kokoc added MX labels Jun 8, 2015
@antonkril
Copy link
Contributor

It's not right to use product collection directly, but this implementation of repository obviously does not support your scenario. So it should be fixed.

@Vinai
Copy link
Contributor Author

Vinai commented Jun 9, 2015

Thanks for the reply Anton.
Would the following (pseudo code) logic make sense in your opinion?

if (searchCriteriaHasAttributeSetIdFilter()) {
   attributeSetIds = getAttributeSetIdsFromSearchCriteriaFilters();
} else {
   attributeSetIds = [defaultAttributeSetId];
}
addAttributeSetAttributesToCollection(attributeSetIds);

Of course it should also check if it is an inclusive filter. I would expect the result to be pretty much what developers want.
I realize this is far from a real solution which would allow the product attributes to load to be specified on the SearchCriteria.
however, because this requirement is EAV specific and I think the Repository is supposed to encapsulate any knowledge about the ORM I have no idea what would be a better approach.

Is there something planed?

@Vinai
Copy link
Contributor Author

Vinai commented Jun 18, 2015

Is there an internal ticket for this issue?

@antonkril
Copy link
Contributor

Yes. MAGETWO-38948. I don't think smth. is planned here. In my opinion attributeSetId and list of attributes to load do not belong to search criteria. These are operation modifiers and should be passed as an additional argument.

@vpelipenko vpelipenko added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Jun 18, 2015
@Vinai
Copy link
Contributor Author

Vinai commented Jun 22, 2015

From a clients point of view I would expect that each product received from the repository would have all values loaded for that product's attribute set, even if the SearchResult would contain products from different attribute sets.
This would also help decouple the internal storage logic further from any clients using the repository.
I'm aware implementing this without adding a couple more db roundtrips is not trivial.

@vpelipenko
Copy link
Contributor

@Vinai, internal ticket is closed and fix is available in 1.0.0-beta. Please, check it on this version.

@Vinai
Copy link
Contributor Author

Vinai commented Jul 16, 2015

From what I can see now all attributes that are assigned to at least one attribtue set are always added to be selected, This works, thanks!

@Vinai Vinai closed this as completed Jul 16, 2015
magento-team pushed a commit that referenced this issue Jul 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Progress: needs update
Projects
None yet
Development

No branches or pull requests

4 participants