Magento 2: When should we use a ProductRepository, ProductFactory, Product model?When Should We Use a Repository and Factory in Magento 2?Warning: Illegal offset type in vendor/magento/module-catalog/Model/CategoryRepository.php on line 133Having trouble exporting products from Magento 2.x. Fatal error: Uncaught Error: Call to a member function getName()getList() must be an instance of MagentoFrameworkApiSearchCriteriaInterfaceMagento 2: product image upload errorOverride MagentoCatalogModelProductCall to a member function getId() on null - module-catalog/Helper/Product.phpWhy does magento 2 use hard coded “Factory” class?How to Create Custom Helper Class in Magento2 then Call ProductRepository to get Product details

Should I pay closing cost and replace HVAC for buyer

Possible ambiguities when using "home" as an adverb? (study home, write home...)

How can I tell if I have simplified my talk too much?

The falling broom handle

Internals of backup compression with TDE (SQL Server)

Are there any real life instances of aircraft aborting a landing to avoid a vehicle?

Is 2FA via mobile phone still a good idea when phones are the most exposed device?

Best way to get my money back from a friend having family problems

How does text classification reduce manpower costs?

What are the applications of the Mean Value Theorem?

I can be found near gentle green hills and stony mountains

What is this hole on the left wing of the Eurofighter Typhoon?

Are ^ and _ the only commands in LaTeX not preceded by a backslash?

What helped Einstein to provide a more accurate description of gravity than Newton?

Can a Horizon Walker ranger use the Distant Strike feature to teleport out of the Forcecage spell?

What to expect when pursuing a second doctorate in an unrelated field

ASCII Expansion

Can the target of Feign Death speak, move, and dodge a fireball

Why is English not a regular language?

Can a stolen Android phone with USB debugging enabled have screen lock bypassed?

How offensive is Fachidiot?

Simple n-body class in C++

Finding big cacti between Phoenix, Las Vegas, and Los Angeles

Print your char count in words, in many languages



Magento 2: When should we use a ProductRepository, ProductFactory, Product model?


When Should We Use a Repository and Factory in Magento 2?Warning: Illegal offset type in vendor/magento/module-catalog/Model/CategoryRepository.php on line 133Having trouble exporting products from Magento 2.x. Fatal error: Uncaught Error: Call to a member function getName()getList() must be an instance of MagentoFrameworkApiSearchCriteriaInterfaceMagento 2: product image upload errorOverride MagentoCatalogModelProductCall to a member function getId() on null - module-catalog/Helper/Product.phpWhy does magento 2 use hard coded “Factory” class?How to Create Custom Helper Class in Magento2 then Call ProductRepository to get Product details






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









4

















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.










share|improve this question
































    4

















    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.










    share|improve this question




























      4












      4








      4


      1






      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.










      share|improve this question















      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.







      magento2 model magento2.3.1 factory repository






      share|improve this question














      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 14 at 6:38









      Dj3Dj3

      315 bronze badges




      315 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          3


















          First, check below links for understanding Repository pattern



          • https://inchoo.net/magento-2/repository-pattern-in-magento-2/


          • https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-contracts/design-patterns.html


          • https://alanstorm.com/magento_2_understanding_object_repositories/


          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.






          share|improve this answer




























          • 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

            – Rutvee Sojitra
            Jun 14 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.

            – Prince Patel
            Jun 14 at 9:09











          • @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

            – Dj3
            Jun 17 at 15:21






          • 1





            Load() of model class is deprecated.Resource class load() is NOT deprecated.

            – Amit Bera
            Jun 17 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.






          share|improve this answer



























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f278331%2fmagento-2-when-should-we-use-a-productrepository-productfactory-product-model%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown


























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3


















            First, check below links for understanding Repository pattern



            • https://inchoo.net/magento-2/repository-pattern-in-magento-2/


            • https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-contracts/design-patterns.html


            • https://alanstorm.com/magento_2_understanding_object_repositories/


            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.






            share|improve this answer




























            • 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

              – Rutvee Sojitra
              Jun 14 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.

              – Prince Patel
              Jun 14 at 9:09











            • @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

              – Dj3
              Jun 17 at 15:21






            • 1





              Load() of model class is deprecated.Resource class load() is NOT deprecated.

              – Amit Bera
              Jun 17 at 15:25















            3


















            First, check below links for understanding Repository pattern



            • https://inchoo.net/magento-2/repository-pattern-in-magento-2/


            • https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-contracts/design-patterns.html


            • https://alanstorm.com/magento_2_understanding_object_repositories/


            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.






            share|improve this answer




























            • 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

              – Rutvee Sojitra
              Jun 14 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.

              – Prince Patel
              Jun 14 at 9:09











            • @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

              – Dj3
              Jun 17 at 15:21






            • 1





              Load() of model class is deprecated.Resource class load() is NOT deprecated.

              – Amit Bera
              Jun 17 at 15:25













            3














            3










            3









            First, check below links for understanding Repository pattern



            • https://inchoo.net/magento-2/repository-pattern-in-magento-2/


            • https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-contracts/design-patterns.html


            • https://alanstorm.com/magento_2_understanding_object_repositories/


            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.






            share|improve this answer
















            First, check below links for understanding Repository pattern



            • https://inchoo.net/magento-2/repository-pattern-in-magento-2/


            • https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-contracts/design-patterns.html


            • https://alanstorm.com/magento_2_understanding_object_repositories/


            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.







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Jun 14 at 13:32

























            answered Jun 14 at 8:06









            Amit BeraAmit Bera

            64.6k17 gold badges90 silver badges187 bronze badges




            64.6k17 gold badges90 silver badges187 bronze badges















            • 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

              – Rutvee Sojitra
              Jun 14 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.

              – Prince Patel
              Jun 14 at 9:09











            • @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

              – Dj3
              Jun 17 at 15:21






            • 1





              Load() of model class is deprecated.Resource class load() is NOT deprecated.

              – Amit Bera
              Jun 17 at 15:25

















            • 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

              – Rutvee Sojitra
              Jun 14 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.

              – Prince Patel
              Jun 14 at 9:09











            • @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

              – Dj3
              Jun 17 at 15:21






            • 1





              Load() of model class is deprecated.Resource class load() is NOT deprecated.

              – Amit Bera
              Jun 17 at 15:25
















            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

            – Rutvee Sojitra
            Jun 14 at 8:44





            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

            – Rutvee Sojitra
            Jun 14 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.

            – Prince Patel
            Jun 14 at 9:09





            @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.

            – Prince Patel
            Jun 14 at 9:09













            @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

            – Dj3
            Jun 17 at 15:21





            @amit-bera could you please let me know where you have seen load() function is deprecated? Reference Link

            – Dj3
            Jun 17 at 15:21




            1




            1





            Load() of model class is deprecated.Resource class load() is NOT deprecated.

            – Amit Bera
            Jun 17 at 15:25





            Load() of model class is deprecated.Resource class load() is NOT deprecated.

            – Amit Bera
            Jun 17 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.






            share|improve this answer






























              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.






              share|improve this answer




























                2














                2










                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.






                share|improve this answer














                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.







                share|improve this answer













                share|improve this answer




                share|improve this answer










                answered Jun 14 at 10:03









                HimanshuHimanshu

                1,2988 silver badges23 bronze badges




                1,2988 silver badges23 bronze badges































                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f278331%2fmagento-2-when-should-we-use-a-productrepository-productfactory-product-model%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown









                    Popular posts from this blog

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?