-
- 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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
03ae7e5 Added cookbook entry about turning a Doctrine repository into a service
bcc37c8 Fixed formating
725ff8c Update repository_service.rst
zebba 9ad68da Update repository_service.rst
zebba 33e264b Update repository_service.rst
zebba 75f8e57 Updated index
zebba 511792b Merge branch 'master' of https://github.com/zebba/symfony-docs
zebba f66b56a Update index.rst
zebba 6133d90 Update repository_service.rst
zebba 198e741 Update repository_service.rst
zebba 2a75b9d Update repository_service.rst
zebba dd7dd14 Update repository_service.rst
zebba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -15,3 +15,4 @@ Doctrine | |
| mapping_model_classes | ||
| registration_form | ||
| console | ||
| repository_service | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| .. index:: | ||
| single: Doctrine; Service | ||
| | ||
| How to turn your Doctrine repository into a service | ||
| =================================================== | ||
| | ||
| Working with repositories can be a pretty messy task. Sometimes it is easier to | ||
| perform certain things right in the repository instead of pulling | ||
| the data and handle it in other places. | ||
| Since this often requires to have other services at hand it is a good | ||
| 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. [...] at hand, it is a good [...] | ||
| idea to turn your standard Doctrine repository into a service. | ||
| | ||
| 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/PostRepository.php | ||
| namespace Acme\BlogBundle\Entity; | ||
| | ||
| use Doctrine\Common\Persistence\Mapping\ClassMetadata; | ||
| use Doctrine\ORM\EntityRepository; | ||
| use Doctrine\ORM\EntityManager; | ||
| | ||
| class PostRepository extends EntityRepository | ||
| { | ||
| public function __construct(EntityManager $em, ClassMetadata $class) | ||
| { | ||
| parent::__construct($em, $class); | ||
| | ||
| // your own logic... | ||
| } | ||
| | ||
| // ... | ||
| } | ||
| | ||
| 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 | ||
| services: | ||
| acme.blog.repository.event: | ||
| class: Acme\BlogBundle\Entity\PostRepository | ||
| factory_service: doctrine.orm.entity_manager | ||
| factory_method: getRepository | ||
| arguments: ['Acme\BlogBundle\Entity\Blog'] | ||
| | ||
| .. 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"> | ||
| <services> | ||
| <service id="acme.blog.repository.post" | ||
| class="Acme\BlogBundle\Entity\PostRepository" | ||
| factory-service="doctrine.orm.entity_manager" | ||
| factory-method="getRepository"> | ||
| <argument>Acme\BlogBundle\Entity\Blog</argument> | ||
| </service> | ||
| </services> | ||
| </container> | ||
| | ||
| .. code-block:: php | ||
| | ||
| // src/Acme/BlogBundle/Resources/config/config.php | ||
| use Symfony\Component\DependencyInjection\Definition; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| | ||
| $definition = new Definition('Acme\BlogBundle\Entity\PostRepository', array( | ||
| 'Acme\BlogBundle\Entity\Blog', | ||
| )); | ||
| $definition->setFactoryService('doctrine.orm.entity_manager'); | ||
| $definition->setFactoryMethod('getRepository'); | ||
| | ||
| $container->setDefinition('acme.blog.repository.post', $definition); | ||
| | ||
| | ||
| Finally, use your repository in your standard controller:: | ||
| | ||
| // src/Acme/BlogBundle/Controller/PostController.php | ||
| namespace Acme\BlogBundle\Controller; | ||
| | ||
| use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
| use Acme\BlogBundle\Entity\Post; | ||
| use Acme\BlogBundle\Entity\PostRepository; | ||
| | ||
| class PostController extends Controller | ||
| { | ||
| public function indexAction() | ||
| { | ||
| /* @var $repository PostRepository */ | ||
| $repository = $this->get('acme.blog.repository.post'); | ||
| $posts = $repository->findAll(); | ||
| | ||
| return $this->render('AcmeBlogBundle:Blog:index.html.twig', array( | ||
| 'posts' => $posts | ||
| )); | ||
| } | ||
| | ||
| // ... | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
How to Turn Your Doctrine Repository into a Service
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.
yourmust be lowercased to, mustn't it?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.
hmm, indeed. I think so
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.
Though, I'm not absolutely sure. Let's see, what @weaverryan thinks about it.
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.
Hm, according to http://www.audioenglish.org/dictionary/closed-class_word.htm (possessive) pronouns are closed-class words.