R
R
Roman2021-02-26 13:24:51
symfony
Roman, 2021-02-26 13:24:51

Is there a fundamental difference between injecting a Repository into a function and getting it through the entityManager (see code)?

The data can be obtained like this:

public function list(ItemRepository $itemRepository)
    {
        $items = $itemRepository->findAll();


or so

public function list()
    {
        $entityManager = $this->getDoctrine()->getManager();
       $items = $entityManager->getRepository(Item::class)->findAll();


Is there any fundamental difference between these two approaches?

What exactly is the advantage of DI (the first option) in this situation? And does it exist at all?

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sl0, 2021-02-26
@procode

If you just need to get some data from the repository, then the first option is better.
But if you need to somehow change the received data, then the second option is better, because. you can immediately pull persist and flush from em.

M
montray, 2021-02-26
@quiex

Try writing tests for this class/method and see what the difference is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question