Skip to content
Prev Previous commit
Next Next commit
Update repository_service.rst
  • Loading branch information
zebba committed May 2, 2014
commit 9ad68da33224fbb90dc41d80fd983afb6753e000
34 changes: 22 additions & 12 deletions cookbook/doctrine/repository_service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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,24 +22,15 @@ 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;
Expand All @@ -48,7 +41,16 @@ if you might need to log something, here is the how-to too:

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 +68,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