Skip to content
Next Next commit
Added cookbook entry about turning a Doctrine repository into a service
  • Loading branch information
sf committed May 2, 2014
commit 03ae7e547d9ddc9e9c8fb416ad5cd847d988493e
96 changes: 96 additions & 0 deletions cookbook/doctrine/repository_service.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
How to turn your Doctrine repository into a service
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to Turn Your Doctrine Repository into a Service

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your must be lowercased to, mustn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, indeed. I think so

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, I'm not absolutely sure. Let's see, what @weaverryan thinks about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, according to http://www.audioenglish.org/dictionary/closed-class_word.htm (possessive) pronouns are closed-class words.

===================================================

Working with repositories can be a pretty messy task. Sometimes it is more
logic to perform certain things right in the repository instead of pulling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes it is easier to perform certain things right in the repository [...]

the data and handle it in other places.
Since this often requires to have other services at hand it is a good
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] at hand, it is a good [...]

idea to turn your standard Doctrine repository into a service.

Let's start with the repository itself. The process is pretty straight forward:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always try to avoid usage of the first person ("Let's"). Like: "First, you need to create a repository. The process [...]"

if you might need to log something, here is the how-to too:

<?php

namespace Acme\BlogBundle\Entity\Repository;

use Psr\Log\NullLogger;
use Psr\Log\LoggerInterface;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;

class PostRepository extends EntityRepository
{
private $Logger; /** @var LoggerInterface */

/**
*
* @param EntityManager $em
* @param ClassMetadata $class
*/
public function __construct(EntityManager $em, ClassMetadata $class)
{
parent::__construct($em, $class);

$this->Logger = new NullLogger();
}

/**
*
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->Logger = $logger;
}
}

// ...

Proceed with creating the service definition:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should add a little explanation about what you're doing. Something like:

Normally, you would retrieve the repository using the ``getRepository()`` method of the entity manager. You can simulate this behaviour by configuring a factory method for the repository service. See :doc:`/components/dependency_injection/factories` for more information about factories. To configure the PostRepository, use something like:

.. code-block:: xml
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this uses tabs, use spaces instead

<service id="acme.blog.repository.event"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were does event come from? I think post is better?

class="Acme\BlogBundle\Entity\Repository\PostRepository"
factory-service="doctrine.orm.entity_manager"
factory-method="getRepository">
<argument>Acme\BlogBundle\Entity\Blog</argument>

<call method="setLogger">
<argument type="service" id="logger" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabs here to (can you please check the complete file for tab usage and replace them with 4 spaces?)

</call>
</service>
</services>
</container>

Finally, use your repository in your standard controller:

<?php

namespace Acme\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\BlogBundle\Entity\Post;
use Acme\BlogBundle\Entity\Repository\PostRepository;

class PostController extends Controller
{
public function indexAction()
{
/* @var $repository PostRepository */
$repository = $this->get('acme.blog.repository.post');
$entities = $repository->findAll();

return $this->render('AcmeBlogBundle:Blog:index.html.twig', array(
'entities' => $entities
));
}

// ...
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line