0

Which is the most efficient way to load desired model data if you have the flexibility to use any of the available methods?

My guess is via factory and load due to my assumption that the repositories are state-less and a single selection would be faster than filtering a collection down. However, with all of the caching and other optimizations I would like a confirmation.

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
LM_Fielding
  • 1,533
  • 4
  • 29
  • 51
  • may be your will get answer from here https://magento.stackexchange.com/questions/158081/when-should-we-use-a-repository-and-factory-in-magento-2 – Amit Bera Apr 27 '17 at 18:01

1 Answers1

1

You should use the repository Interface defined for the particular model.

for example if you want to load a product use productRepositoryInterface methods.

you should pass the productRepositoryInterface in your construct.

and get the model using.

$product = $this->productRepository->getById($productId);

Now, This will return you the product if it already exist in cache directly from cache Or it will load from database.

Pankaj Bhope
  • 1,546
  • 13
  • 26