1

I'm trying to follow Magento's tutorial for adding an extension attribute. I want to add an extension attribute called "exportedFlag" to the Invoice class, and have it returned through the V1/invoices/ API method.

Here's my API call

http://magento.test/rest/V1/invoices/?
searchCriteria[filter_groups][0][filters][0][field]=store_id&
searchCriteria[filter_groups][0][filters][0][value]=1&
searchCriteria[filter_groups][0][filters][0][condition_type]=eq

Here's the error I'm getting:

Fatal Error: 'Uncaught TypeError: Argument 2 passed to 
MyCompany\\Invoice\\Model\\Plugin\\Invoice\\Repository::afterGetList() 
must be an instance of Magento\\Framework\\Api\\SearchResults, 
instance of Magento\\Sales\\Model\\ResourceModel\\Order\\Invoice\\Collection given, 
called in /var/www/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php 
on line 146 and defined in 
/var/www/magento2/app/code/MyCompany/Invoice/Model/Plugin/Invoice/Repository.php:53
Stack trace: 
#0 /var/www/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(146): 
MyCompany\\Invoice\\Model\\Plugin\\Invoice\\Repository->afterGetList(Object(Magento\\Sales\\Model\\Order\\InvoiceRepository\\Interceptor), 
Object(Magento\\Sales\\Model\\ResourceModel\\Order\\Invoice\\Collection), 
Object(Magento\\Framework\\Api\\SearchCriteria))
#1 /var/www/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(153): 
Magento\\Sales\\Model\\Order\\InvoiceRepository\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\Api\\SearchCriteria))
#2 /var/www/mag' in '/var/www/magento2/app/code/MyCompany/Invoice/Model/Plugin/Invoice/Repository.php' on line 53

Here's the relevant code

/**
 * Class Repository
 * @package MyCompany\Invoice\Model\Plugin\Invoice
 */
class Repository
{
    /**
     * Adds the "exported flag" to invoice extension attributes.
     *
     * @param \Magento\Sales\Api\InvoiceRepositoryInterface $subject
     * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
     * @return \Magento\Framework\Api\SearchResults
     */
    public function afterGetList
    (
        \Magento\Sales\Api\InvoiceRepositoryInterface $subject,
        \Magento\Framework\Api\SearchResults $searchResult
    ) {
        /** @var \Magento\Catalog\Api\Data\InvoiceInterface $invoice */
        foreach ($searchResult->getItems() as $invoice) {
            $this->addExportedFlagToInvoice($invoice);
        }
        return $searchResult;
    }  
}  

For reference, here's the comparable bit of code from Magento's Github example

/**
 * Class Repository
 * @package MyCompany\Invoice\Model\Plugin\Invoice
 */
class Repository
{
    /**
     * Add Social Links to product extension attributes
     *
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $subject
     * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
     * @return \Magento\Framework\Api\SearchResults
     */
    public function afterGetList
    (
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Framework\Api\SearchResults $searchResult
    ) {
        /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
        foreach ($searchResult->getItems() as $product) {
            $this->addExternalLinksToProduct($product);
        }
        return $searchResult;
    }
}

Why is Magento passing an instance of Magento\\Sales\\Model\\ResourceModel\\Order\\Invoice\\Collection as my second parameter to afterGetList, when Magento's example shows that the second parameter should be \Magento\Framework\Api\SearchResults?

  • did yun run upgrade and di compile after applying your configuration? – fmsthird Apr 8 at 21:36
  • run upgrade then compile then indexer – fmsthird Apr 8 at 21:37
  • I did php bin/magento setup:upgrade, php bin/magento setup:di:compile, and php bin/magento indexer:reindex and I'm still getting the same error. – Ben Rubin Apr 9 at 13:28
  • I dug into the code, and apparently InvoiceRepository return a Collection object, unlike ProductRepository that returns a SearchResults object. – Ben Rubin Apr 9 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.