Are Doctrine2 repositories a good place to save my entities?

Trent picture Trent · Dec 3, 2011 · Viewed 18.5k times · Source

When I read docs about repositories, it is often to work with entities & collection but in a "read-only" manner.

There are never examples where repositories have methods like insertUser(User $user) or updateUser(User $user).

However, when using SOA, Service should not be working with Entity Manager (that's right, isn't it?) so:

  1. Should my service be aware of the global EntityManager?
  2. Should my service know only about the used Repositories (let's say, UserRepository & ArticleRepository)

From that both questions, another one, should my service ever explicitly persist() & flush() my entities ?

Answer

Elnur Abdurrakhimov picture Elnur Abdurrakhimov · Dec 3, 2011

Yes, repositories are generally used for queries only.

Here is how I do it. The service layer manages the persistence. The controller layer knows of the service layer, but knows nothing of how the model objects are persisted nor where do they come from. For what the controller layer cares is asking the service layer to persist and return objects — it doesn't care how it's actually done.

The service layer itself is perfectly suitable to know about the the persistence layer: entity or document managers, repositories, etc.

Here's some code to make it clearer:

class UserController
{
    public function indexAction()
    {
        $users = $this->get('user.service')->findAll();
        // ...
    }

    public function createAction()
    {
        // ...
        $user = new User();
        // fill the user object here
        $this->get('user.service')->create($user);
        // ...
    }
}

class UserService
{
    const ENTITY_NAME = 'UserBundle:User';

    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function findAll()
    {
        return $this->em->getRepository(self::ENTITY_NAME)->findAll();
    }

    public function create(User $user)
    {
        // possibly validation here

        $this->em->persist($user);
        $this->em->flush($user);
    }
}