Skip to content
41 changes: 27 additions & 14 deletions cookbook/doctrine/repository_service.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.. index::
single: Doctrine; Service

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.

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

Expand All @@ -8,8 +11,10 @@ Since this often requires to have other services 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:
if you might need to log something, here is the how-to too::
Copy link
Member

Choose a reason for hiding this comment

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

You should remove the colon from the previous sentence. Something like:

First, you need to create a repository. The process is pretty straight forward. Assume you need to log something, your repository will look like::

// src/Acme/BlogBundle/Entity/Repository.php;
Copy link
Member

Choose a reason for hiding this comment

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

src/Acme/BlogBundle/Entity/Repository/PostRepository.php


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 empty line

namespace Acme\BlogBundle\Entity\Repository;

use Psr\Log\NullLogger;
Expand All @@ -20,35 +25,35 @@ if you might need to log something, here is the how-to too:

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

Choose a reason for hiding this comment

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

$logger instead on $Logger


/**
*
* @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:

.. configuration-block::

.. code-block:: yaml

# src/Acme/BlogBundle/Resources/config/config.yml
# todo
Copy link
Member

Choose a reason for hiding this comment

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

services: acme.blog.repository.event: class: Acme\BlogBundle\Entity\Repository\PostRepository factory_service: doctrine.orm.entity_manager factory_method: getRepository arguments: ['Acme\BlogBundle\Entity\Blog'] calls: - [setLogger, ["@logger"]]

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/config.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">
Expand All @@ -66,8 +71,16 @@ Proceed with creating the service definition:
</services>
</container>

Finally, use your repository in your standard controller:
.. code-block:: php

// src/Acme/BlogBundle/Resources/config/config.php
// @todo
Copy link
Member

Choose a reason for hiding this comment

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

use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; $definition = new Definition('Acme\BlogBundle\Entity\Repository\PostRepository', array( 'Acme\BlogBundle\Entity\Blog', )); $definition->setFactoryService('doctrine.orm.entity_manager'); $definition->setFactoryMethod('getRepository'); $definition->addMethodCall('setLogger', array(new Reference('logger'))); $container->setDefinition('acme.blog.repository.event', $definition);


Finally, use your repository in your standard controller::

// src/Acme/BlogBundle/Controller/PostController.php

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

namespace Acme\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
Expand Down