6

I built Magento 2 Module which has a model and Service contracts/API.

During this implementation, I have followed vinai Kopp article and also magento2 module Magento_Quote.

On this Magento\Quote\Model\QuoteRepository, i have found two classes

Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;

Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;

And found that during the implementation of getList(), these two classes used

  $this->collectionProcessor->process($searchCriteria, $this->quoteCollection);
  $this->extensionAttributesJoinProcessor->process($this->quoteCollection)

MY repository class

<?php
namespace Devamitbera\ProductSuggestion\Model;

use Devamitbera\ProductSuggestion\Api\Data\ProductSuggestionInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Devamitbera\ProductSuggestion\Api\ProductSuggestionRepositoryInterface;
use Devamitbera\ProductSuggestion\Model\ProductSuggestionFactory;
use Devamitbera\ProductSuggestion\Model\ResourceModel\ProductSuggestion as ProductSuggestionResource;
use Devamitbera\ProductSuggestion\Model\ResourceModel\ProductSuggestion\CollectionFactory as ProductSuggestionCollectionFactory ;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\CouldNotSaveException;
use Devamitbera\ProductSuggestion\Api\Data\ProductSuggestionSearchResultInterfaceFactory;


/**
 * Repository class 
 */

class ProductSuggestionRepository  implements ProductSuggestionRepositoryInterface 
{

    /**
     * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
     */
    protected $extensionAttributesJoinProcessor;

    /**
     * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
     */
    protected $collectionProcessor;

    /**
     * @var \Devamitbera\ProductSuggestion\Api\Data\ProductSuggestionSearchResultInterfaceFactory
     */
    protected $productSuggestionSearchResultFactory;

    /**
     * @var \Devamitbera\ProductSuggestion\Model\ProductSuggestionFactory
     */
    protected $productSuggestionFactory;

    /**
     * @var \Devamitbera\ProductSuggestion\Model\ResourceModel\ProductSuggestion
     */
    protected $productSuggestionResource;

    /**
     * @var Devamitbera\ProductSuggestion\Model\ResourceModel\ProductSuggestion\CollectionFactory
     */
    protected $collectionFactory;

    public function __construct(
        ProductSuggestionFactory $productSuggestionFactory,
        ProductSuggestionResource $productSuggestionResource,
        ProductSuggestionCollectionFactory $CollectionFactory,
        ProductSuggestionSearchResultInterfaceFactory $productSuggestionSearchResultFactory,
        CollectionProcessorInterface  $collectionProcessor,
        JoinProcessorInterface  $extensionAttributesJoinProcessor    
    ) 
    {

        $this->collectionFactory = $CollectionFactory;
        $this->productSuggestionResource = $productSuggestionResource;
        $this->productSuggestionFactory = $productSuggestionFactory;

        $this->productSuggestionSearchResultFactory = $productSuggestionSearchResultFactory;
        $this->collectionProcessor = $collectionProcessor ?: \Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessor::class);;
        $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
    }
     /**
     * {@inheritdoc}
     */
    public function delete(ProductSuggestionInterface $productSuggestion){
        $this->productSuggestionResource->delete($productSuggestion);
    }
    /**
     * {@inheritdoc}
     */
    public function getById($id){

        $productSuggestion = $this->productSuggestionFactory->create();
        $this->productSuggestionResource->load($productSuggestion, $id);
        if(!$productSuggestion->getId()){
            throw new NotFoundException(__('unable to find the record'));
        }
        return $productSuggestion;
    }
    /**
     * {@inheritdoc}
     */
    public function getList(SearchCriteriaInterface $searchCriteria){

        $collection = $this->collectionFactory->create();

        $searchData = $this->productSuggestionSearchResultFactory->create();
        $searchData->setSearchCriteria($searchCriteria);

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

        $searchData->setItems($this->quoteCollection->getItems());
        $searchData->setTotalCount($this->quoteCollection->getSize());
        return $searchData;        
    }
    /**
     * {@inheritdoc}
     */
    public function save(ProductSuggestionInterface $productSuggestion){
        try{
          $this->productSuggestionResource->save($productSuggestion);  
        } catch (Exception $exception) {
            throw  new CouldNotSaveException(__($exception->getMessage()));
        }

        return $productSuggestion;
    }

}

Now, I want to know,

  • why do these two classes used?
  • Do I need these classes when I will implement Service contracts/API ?
1

As I know Yes, for Service contracts/API you have to use this both classes.

why do this two classes use?

It will useful in api for retrieve data, CollectionProcessorInterface class is used for retrieving data with where condition $searchCriteria and JoinProcessorInterface is used for retrieving custom_attribute_fields on api call time.

Do I need this classes when I will implement Service contracts/API ?

Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
This class is used for get listing with seach criteria in your api(like where condition).

Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
This class is retrieve custom_checkout_field on api select, which is defined in extension_attributes.xml

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.