I have checked the similar post: Magento2.2 getCollection from Factory deprecated

But I still don't find the answer of it. I am calling getCollection() function, and PHPStorm saying that this function is deprecated.


where $this->_myCollectionFactory is the instance of my collection factory which is \Vendor\Module\Model\ModuleFactory.

And this is my ModuleFactory class code


namespace Vendor\Module\Model;

class ModuleFactory
     * @var \Magento\Framework\ObjectManagerInterface
    protected $_objectManager;

     * @param \Magento\Framework\ObjectManagerInterface $objectManager
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
        $this->_objectManager = $objectManager;

     * Create new country model
     * @param array $arguments
     * @return \Magento\Directory\Model\Country
    public function create(array $arguments = [])
        return $this->_objectManager->create('Vendor\Module\Model\Module', $arguments, false);

My question is, what is the alternate of getCollection if it is deprecated?


Shoaib Munir ,you are wrong as $this->_myCollectionFactory instance of a collection faCTORY CLASS . If you have using Collection factory class then getting collection data, you don't need to call method getCollection.Also getCollection is not the method of collection class and it is the method of Model class Like Magento\Catalog\Model\Product.

If you have a collection class then you need to use the code like below

$collection = $this->_myCollectionFactory->create()

| improve this answer | |
  • Updated my answer, added more info. getCollection() code is working fine and it is getting my collection, but phpStorm giving warning that the function is deprecated. – Shoaib Munir Apr 13 '19 at 3:07
  • Please check your commit where $this->_myCollectionFactory is the instance of my collection factory which is \Vendor\Module\Model\ModuleFactory .How can a Modelfactory class is collection factory class???. Please understand Model,ResourceModel,Collection class and their factory class – Amit Bera Apr 13 '19 at 5:11
  • Is this solution is applicable for previous versions of Magento 2? – iqbal malik May 13 '19 at 10:15
  • Got your point now. Thanks mate :) – Shoaib Munir Jul 10 '19 at 18:01

In Magento2 it is always recommend using Search Criteria to get collections For example:

   $searchCriteriaBuilder->addFilter('zip', $saveSearchZip['zip'], 'eq')
        ->addFilter('country', $saveSearchZip['country'], 'eq')
        ->addFilter('latitude', $saveSearchZip['latitude'], 'eq')
        ->addFilter('longitude', $saveSearchZip['longitude'], 'eq');

   $collection = $myModuleRepositoryInterfaceObject->getList($searchCriteriaBuilder->create());

Please give a refer this for better understanding. Also find sample module

| improve this answer | |
  • what is the instance of $searchCriteriaBuilder? is it collection factory? – Shoaib Munir Apr 12 '19 at 12:03
  • @ShoaibMunir Magento\Framework\Api\SearchCriteriaBuilder::class , i have also included documentation for it please go through it it would be helpful – Vishwas Bhatnagar Apr 12 '19 at 12:05
  • what if my collection is related to a custom table? how can I get my collection through search criteria? – Shoaib Munir Apr 12 '19 at 12:05
  • 1
    I am assuming that that you have created a module for custom, So as per magento standards you have to create repository interface for it . so you can use that repo interface myModuleRepositoryInterfaceObject – Vishwas Bhatnagar Apr 12 '19 at 12:08
  • No Vishawas this is not related to customer, this is custom module of a custom table collection. – Shoaib Munir Apr 12 '19 at 12:10

Use a CollectionFactoryas follows. For example, for Magento\Catalog\Model\Product :

public function __construct(    
  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
  $this->productCollectionFactory = $productCollectionFactory; 

public function getProductCollection()
  return $this->productCollectionFactory->create();
| improve this answer | |
  • Thanks for the answer. Amit bera gave answer first. +1 for your efforts and help :) – Shoaib Munir Jul 10 '19 at 18:02

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.