What is the difference between this below code?

Class : \Magento\Catalog\Model\ProductFactory

Code : $this->productFactory->create()->getCollection();

Class : \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory

Code : $this->collectionFactory->create();


Result is the same in this case.

$this->productFactory->create()->getCollection(); will use the basic method getCollection() of class \Magento\Framework\Model\AbstractModel, resulting in creating collection by object manager.

Magento\Catalog\Model\ResourceModel\Product\CollectionFactory is directly responsible of creating product collection. Extra logic can be added in this class.

You should use CollectionFactory as much as you can.

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.