5

When should we use a ProductRepository, ProductFactory, Product model?

vendor/magento/module-catalog/Model/Product.php

vendor/magento/module-catalog/Model/ProductRepository.php

generated/code/Magento/Catalog/Model/ProductFactory.php

Example would be more descriptive.

2 Answers 2

5

First, check below links for understanding Repository pattern

ProductRepository.php is the main class to get product data. The repository is a design pattern which allows code separation between data retrieval and business logic and Magento 2 use this design pattern.

You can use Repository also use for Magento REST AND SOAP API and that main advance of the repository. Same class use for both API, frontend, admin area for getting a particular entity data like category, Product, etc.

A repository is a part of the service contracts. We use service contacts to Implement API point and Service contacts are related with RepositoryInterface and Data interface

Here Product.php is Data model class which provide

and ProductFactory.php is the Factory class which uses initiate the data class.

Also, check this MSE answer for understanding when you will use Factory and Repository When Should We Use a Repository and Factory in Magento 2?

From Magento 2.2 version, Magento tried to avoid data using load() function of Model class and this load() method is a deprecated method.

4
  • But if you see in repository there is also retrieve data by using load method. like if am taking getById method example there is also data retrieve using load Commented Jun 14, 2019 at 8:44
  • @RutveeSojitra A repository is a new mechanism used by Magento. The main advantage of the repository is API which can be used in any area(admin, frontend, API), while with a model we only use getter and setter. Yes, Magento not updated the direct load model in many files due to backward compatibility. Commented Jun 14, 2019 at 9:09
  • @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link
    – Dj3
    Commented Jun 17, 2019 at 15:21
  • 1
    Load() of model class is deprecated.Resource class load() is NOT deprecated.
    – Amit Bera
    Commented Jun 17, 2019 at 15:25
2

When should USE

ProductRepository

  • Product repository is generally used to call data collection of product which is basically call ResourceModel at the end.
  • When you call Product Repository it will call resource model and cache that product in repository object so when you call it another time. It will retrieve product data from that cache.
  • You can find vendor/magento/module-catalog/Model/ProductRepository.php public function get($sku, $editMode = false, $storeId = null, $forceReload = false)
  • You can use it to show product information which doesn't need every time to refresh product data like mobile API, webpage front.

ProductFactory

  • It's a factory class of Product Model, which is called product model class but on time of ProductFactory->create() called.
  • It's a factory object you can use it when you need a new object of product every time.

Product Model

  • Product model is used to interact with the database using its resource object. so for retrieving data purpose use Product Repository cause it will give performance enhancement by it's caching mechanism by the repository.

  • And If you need to do any database operation related to save, update then you can use ProductResourceModel class which is direct interact to product resource model class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.