0

Im creating a custom product Api with searchCriteria from \Magento\Catalog\Model\ResourceModel\Product\Collection

And I joined other table to this collection;

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $this->collectionFactory->create();
    $collection->getSelect()->joinLeft(array("vendorProduct"=>'ced_csmarketplace_vendor_products'), 'e.entity_id = vendorProduct.product_id',array('vendor_id' => "vendor_id"));
    
    $this->extensionAttributesJoinProcessor->process($collection);

    $collection->addAttributeToSelect('*');
    $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
    $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');

    $this->collectionProcessor->process($searchCriteria, $collection);

    $collection->load();

    $collection->addCategoryIds();
    $this->addExtensionAttributes($collection);
    $searchResult = $this->searchResultsFactory->create();
    
    $searchResult->setSearchCriteria($searchCriteria);
    $searchResult->setItems($collection->getItems());
    $searchResult->setTotalCount($collection->getSize());

   
    return $searchResult;
}

The joined data not show when I call this Api.

But if I "return $collection->getData();" it will show the joined data.

enter image description here

  • in both screenshot, I see the identical data, am I missing something? – Herve Tribouilloy Jun 26 at 16:48
  • Sorry, I uploaded a wrong image. – โอ ศราวุธ Jun 27 at 13:33
  • you may add your webapi.xml file and your repository function at least for us to know where you get it wrong.. Of course we can do it all from scratch but who will? – Herve Tribouilloy Jun 27 at 13:37
0

The searchResult is the result from the repository getList function and you may see this function (the one that appears in your post) mentions attributeJoinProcessor and so on.. these lines aggregate to the collection various additional values - amongst them are extension_attributes - but more simply the product data that are setup to be part of this repository getList call.

Instead, the collection is just the product collection that you can create independently to the repository. Using the collection can be useful in many circumstances (particularly to write complex custom query). Now, of course all the extension_attributes won't appear in the collection unless you make some complex joins yourself.

Hope this gives you enough background to see why your result do differ

| improve this answer | |

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.