K
K
Kerm2020-07-17 11:15:35
symfony
Kerm, 2020-07-17 11:15:35

Symfony 3, one service, multiple repositories, how to write in services.yml?

Please help, there is a service, this service has several tables in db, more specifically 7.

Each of these tables has its own entity and its own repository.

In the service, there are simple requests in the functions that I would like to organize through the repository, such as findAll()

But, if I access the repository in the service like this (for example):

$a = $this->em->getRepository('AppBundle :RepoOne')->findAll();
$b = $this->em->getRepository('AppBundle:RepoTwo')->findAll();
$c = $this->em->getRepository('AppBundle:RepoThree')->findAll();

It swears that the repository is not registered for the service in services.yml

It should be written something like this:

myservice:
    class: AppBundle\Services\Myservice
    arguments: [ "@doctrine", "@configure", "@kernel"]
  myservice.repository:
    class: AppBundle\Repository\MyServiceRepository
    arguments: [ "@doctrine.dbal.default_connection"]


And how can I register not one, but several repositories for one service?

This is how it is written in my repository after auto generation:

namespace AppBundle\Repository;

use AppBundle\Entity\RepoOne;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-07-17
@gebrak

Make sure the method getRepository()specifies the Entity and not the name of the repository. For example,:

$a = $this->em->getRepository('AppBundle:Entity1')->findAll();
# или
$a = $this->em->getRepository(Entity1::class)->findAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question