2
votes

New to symfony. I am following this documentation. I have multiple entities mapped from tables imported from another application which did not create repositories automatically and there are a lot of entites to do it by hand. However I want create a single repository class that interfaces multiple entities.

My current Repository according to the documention is

Product repository
class ProductRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
    parent::__construct($registry, Product::class);
}

I will be using it in my controller like,

$respository = $this->getDoctrine()->getRepository(Product::class);

However I have other entity classes as well,

Customer, User

to name a few.

How or is it possible to use just Product Respository to interface with these other entities like

 $respositoryUser = $this->getDoctrine()->getRepository('User');

or is it possible to auto generate Repositories from an already existing entities.

2
You should probably use Doctrine for a bit and learn some more about the relationship between entity managers, repositories and entities. After that you might understand how your question does not make much sense.Cerad
One of long-term better solutions are using repositories as services: tomasvotruba.cz/blog/2017/10/16/…Tomas Votruba
@Cerad I agree with you, but for now I was looking a quick fix.Sanjok Gurung

2 Answers

0
votes

You can create repositories for any entity as you have with products. This will work and provide you with a standard entity repository for users:

$userRepository = $this->getDoctrine()->getRepository(User::class);

If you wanted to find something specific, which required more computation, you would create a dedicated repository. You will still get the repository in the same way unless you want to inject it into your controller or service directly. Just specify the repository in your entity if that is the case.

0
votes

Look like you are looking for equivalent facilities you used in another environment like Java/Persistence ... I think that what you are looking at, is a DaoService for a group of related entities, instead of a shared Repository. Custom Repository class is tied by definition to a class entity, and trying to share his functions will end in mapping confusions.

By organizing your code around a DaoService class, you will have the opportunity to regroup all the data access concerning those entities in one place.

How to organize then your DaoService code ?

  1. You generally work with default entity manager ... Better group entities in same database space. But multiples entities is also manageable with care.
  2. Use the generic repository to for each entity in the group

    $userRepo = $this->em->getRepository(User::class)

    $prodRepo = $this->em->getRepository(Product::class)

as you see feat your needs...

  1. Remember that, the main focus here is modularity by code organization, then make sure you centralize in this class and for each entity:
    • crud operations and specific search functions
    • business logic related to those group of entities
    • Module based service for some business features
    • You can also define an interface that this service class will implement, making your model more flexible, and easy to use by that interface class. If your service is well defined, it will make client Controllers more light and clean and be the Model side of your MVC solution.