Magento 2 – Alternate Method for Deprecated getCollection()

collection;deprecatedfactorymagento2

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.

$this->_myCollectionFactory->create()->getCollection()

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

And this is my ModuleFactory class code

<?php

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?

Best Answer

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()

Related Topic