Symfony Doctrine entity manager and repository
Asked Answered
M

1

8

I understand the benefit or repository pattern but I just can't understand in Symfony3 Doctrine there are Doctrine\ORM\EntityManager and \Doctrine\ORM\EntityRepository

  1. What are the difference between the two?
  2. Is repository should be injected to controller or entity manager?

    Edit The correct question should be: What's the proper way to access a repository from a controller?

    • Should a repository be injected to a controller as a service?
    • Should a repository be injected to another service as a service?
  3. Should entity manager contain any query at all?
    Edit The correct question should be: should a service contain a query at all? Which @MateuszSip already explained, it could be done by injecting Entity Manager

  4. Should a custom function like getAvailableManagers be put in repository or services? (Where manager is a repository and there are some logic in determining available manager)

  5. How about a more generic function like findAllManager, should it be in repository or entity manager?

Currently I'm using Symfony3. Thank you very much

Cheers,

Edit Talking to @MateuszSip (thanks mate), I decided to make my question clearer with an example below. Please note that below code are not representing real problem

controller

Class ManagementController
{
    public function assignManager($projectType)
    {
        // Grabbing a service
        $s = $this->get('mycompany_management_management_service')

        $managers = $s->findAvailableManagers();
        $managers = $s->checkCapability($managers, $projectType);

        return $managers
    }
}

repository

class ManagerRepository extends \Doctrine\ORM\EntityRepository
{
    public function findAvailableManagers() 
    {
        ...
        return $managers
    }

    public function checkCapability($managers, $type)
    {
        ...
        return $capableManagers
    }
}

services

class ManagementService 
{
   ... I am not sure what should be here.
}
Mailable answered 30/5, 2016 at 23:58 Comment(0)
B
6
  1. EntityManager is used to manage doctrine-related objects, so:
    • you can persist an entity object (it's now managed by doctrine, and ready to save)
    • you can remove an entity object (so it'll be deleted later)
    • you can flush, and it'll trigger pending operations
    • you can get a repository (to get objects you'll need) or use a generic api to get an object by a primary key etc.

It's a class that manages a state of objects and their relation to the database.

Repository is a pattern that standarizes an access to the entites.

  1. If your app is complex, you should inject a separate service(s) to your controller. So there's a UserSaver service (as an example) that use entityManager to create/update a user and UserFinder (or something well-named) using UserRepository which's responsible of fetching user by defined criterias.

  2. You can create a query using entity manager, but em itself cannot contain queries.

  3. In my opinion, define a method inside a service, and a corresponding method in your UserRepository. At this moment, all of what you want should be fetched by a database, but it can change later.

  4. In repository. Methods like: findByRole(role=manager), findIsActive, findOneBySecurityNumber relies to a repository.

Bobbery answered 31/5, 2016 at 0:55 Comment(5)
Thanks for the answer. For your point 4. How can the service access repository? Is it through injection? or is it through ->entityManager->getRepository ?Mailable
You can define your repository as a service and inject it into your service.Bobbery
If I define my repository as a service, why do I need of injecting it into another service? Shouldn't I just inject the repository in the controller as a service instead? Also if I'm using injection, what's the point of ->entityManager->getRepository then? Thanks and sorry if it's too many questionsMailable
You call a service that know how to get data, but controller shouldn't be aware of implementation. It's all about separating business logic from data source.Bobbery
I agree that the controller shouldn't be aware of the implementation. That's why we have service. The one that I don't understand is ... why can't we use repository as a service that is used by controller directly ? Why there should be another service in between? Thanks again. You've been really helpfulMailable

© 2022 - 2024 — McMap. All rights reserved.