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.
Leave a Comment
(0 Comments)
Useful Magento 2 Articles
Author Info
Chirag
Connect With Me