Hi i'm relatively new to Magento2 development and it has really been difficult to find straight answers to programmatical problems.

For instance; a simple: How to i retrieve an sales_order record from the database so that i can read and update all of it's fields in the database.

  • Some people say use the ObjectManager directly.
  • Some people say use the ObjectManager but through DI
  • Some people say don't do that, use a OrderRepository
  • Some people say yes, do that last one but do it with a OrderRepositoryInterface and do it directly
  • Some people say yes, do that last one but do it with a OrderRepositoryInterface and use DI
  • Some people give examples what to do not, but fail to mention what to do.
  • Some people give examples that don't work.
  • etc etc etc

So, if i want to retrieve an Order in the Sales module and i want to be able to update and read all fields in the databases table what is the correct official and preferred way to do that in Magento2?

Where can i find examples, and where can i find the logic behind this. I just bought 4 magento2 developer books, and all are quite useless in this aspect.


As in Magento1, there are several ways to do the same things in Magento2

Not sure if, with time, newer versions will directly invalidate some ways to do some things (I think yes, but that's my bet)

As far as I have read until now, best practices approach to do what you ask would be:

  • use DI, and use Factory classes to get your objects

A good reason to use factories...

Factories are smart enough to resolve dependencies and allow you to get the correct instance of an interface as defined in your module’s di.xml.


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.