Alternative to Magento 2 Deprecated getCollection() method.

Many of us have been experienced that the use of the getCollection() method has been deprecated. The getCollection() method is available in your Model class. You might have faced that many times while calling product, quote, or order collection. Therefore, If you are using PhpStorm it’s showing a deprecated method like this screenshot.

Alternative to Magento 2 Deprecated getCollection() method

There are two alternatives. We will see both alternatives by example. However, Magento 2 always recommends using the Search Criteria to get collections.

Use Service Contract (SearchCriteriaBuilder class)

For example, If you want to fetch the customers with filters, you can use the below code.

<?php

namespace Codextblog\Demo\Helper;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Customer\Api\CustomerRepositoryInterface;

class Customer extends AbstractHelper
{
    /**
     * @var CustomerRepositoryInterface
     */
    protected $customerRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var FilterGroupBuilder
     */
    protected $filterGroupBuilder;

    /**
     * @var FilterBuilder
     */
    protected $filterBuilder;


    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        CustomerRepositoryInterface $customerRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        FilterGroupBuilder $filterGroupBuilder,
        FilterBuilder $filterBuilder,
    ) {
    	parent::__construct($context);
    	$this->customerRepository = $customerRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterGroupBuilder = $filterGroupBuilder;
        $this->filterBuilder = $filterBuilder;
    }

    public function getCustomers() {
  		$filter1 = $this->filterBuilder->setField('member')
              ->setValue(1)
              ->setConditionType('neq')
              ->create();
          $filter2 = $this->filterBuilder->setField('member')
              ->setValue('')
              ->setConditionType('eq')
              ->create();

          $filterOr = $this->filterGroupBuilder
              ->addFilter($filter1)
              ->addFilter($filter2)
              ->create();
          $this->searchCriteriaBuilder->setFilterGroups([$filterOr]);

          $customers = $this->customerRepository->getList($this->searchCriteriaBuilder->create())->getItems();

          if ($customers) {
              foreach ($customers as $customer) {
                  //rest of the code
              }
          }
    }    
}

Use Resource Model’s Collection Factory Class

For example, If you want to fetch the category collection with filters, you can use the below code.

<?php

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

class Index extends \Magento\Framework\View\Element\Template
{
   /**
     * @var CollectionFactory
     */
   protected $_categoryCollection;

   public function __construct(
     Context $context,
     CollectionFactory $categoryCollection,
     array $data = []
   ) {
     $this->_categoryCollection = $categoryCollection;
     parent::__construct($context, $data);
   }

   public function getCategoryList()
    {
        return $this->_categoryCollection->create()->addAttributeToSelect('*')
            ->addAttributeToFilter('is_active', 1)
            ->addAttributeToFilter('include_in_menu', 1)
            ->addAttributeToFilter('parent_id', 4)
            ->setOrder('position', 'ASC');
    }

}

In conclusion, using a service contract to fetch any collection is the best way. Hope this code snippet helps you. Feel free to add a comment if you still have any doubts.

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

Was this post helpful? Please support Us!

Follow us on twitter or Like us on facebook.

 


To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close

Increase Your
Magento 2
Knowledge

Get Weekly Tutorial
to your Inbox
Subscribe Me
close-link
Subscribe Here