0

I am not understanding the concept difference between collection and repository in magento2 and which is better to use.

your help will be able to appreciate.

1 Answer 1

0

Sort answer : They are the same.

Long answer : Repositories are in fact more abstract than collection. The same way as collection are more abstract than ResourceModel.

Collection used to be the magento 1 way. But they are still very used in magento 2, although we should all be using Repositories.

But at the end of the day, what you need to remind is that repositories are just an abstraction of collection, so when you call repository they will end up using collection at some point.

Repositories tend to be closer from the object side. When Collection tends to be closer from the sql / database side.

2
  • Although Repositories are recommended there can be a huge performance hit for using them. One of our clients had a customer with a gift registry containing over 100 items, using a repository the page took 6 seconds to load, after switching to a collection it only took 3.5 seconds and then we got it down to 2 seconds by adding block caching. Commented Jun 5 at 8:01
  • Yes that explains why everyone still use collection...but sometimes we don't get proper results if we don't use repositories, especially with products
    – Claims
    Commented Jun 6 at 9:49

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.