Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Doctrine entity manager and repository

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.
}
like image 900
Don Djoe Avatar asked May 30 '16 23:05

Don Djoe


People also ask

What is repository in Symfony?

A repository is a way to retrieve entities, so put on repositories any method you need to get them, such as getUserByEmail or whatever.

What is doctrine repository?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.

What is a entity in Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.


1 Answers

  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.

like image 181
Mateusz Sip Avatar answered Nov 22 '22 23:11

Mateusz Sip