-
- 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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| How to turn your Doctrine repository into a service | ||
| =================================================== | ||
| | ||
| 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 | ||
| 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. 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 | ||
| 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. | ||
| | ||
| Let's start with the repository itself. The process is pretty straight forward: | ||
| 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. 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: | ||
| 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: | ||
| | ||
| .. 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> | ||
| 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. this uses tabs, use spaces instead | ||
| <service id="acme.blog.repository.event" | ||
| 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. were does | ||
| 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" /> | ||
| 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. 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 | ||
| )); | ||
| } | ||
| | ||
| // ... | ||
| } | ||
| | ||
| 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 | ||
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.