-
- Notifications
You must be signed in to change notification settings - Fork 5.3k
New cookbook recipe stub: Turn Doctrine repository into service #3825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
03ae7e5 bcc37c8 725ff8c 9ad68da 33e264b 75f8e57 511792b f66b56a 6133d90 198e741 2a75b9d dd7dd14 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -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: | ||
| 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:: | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| | @@ -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 */ | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| | ||
| /** | ||
| * | ||
| * @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; | ||
| | @@ -48,7 +41,16 @@ if you might need to log something, here is the how-to too: | |
| | ||
| Proceed with creating the service definition: | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"> | ||
| | @@ -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 | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| | ||
There was a problem hiding this comment.
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 [...]"