-
- 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 1 commit
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
Fixed formating
- Loading branch information
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 |
|---|---|---|
| | @@ -10,41 +10,39 @@ 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: | ||
| | ||
| <?php | ||
| namespace Acme\BlogBundle\Entity\Repository; | ||
| | ||
| 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; | ||
| | ||
| 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 */ | ||
| | ||
| 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); | ||
| | ||
| /** | ||
| * | ||
| * @param EntityManager $em | ||
| * @param ClassMetadata $class | ||
| */ | ||
| public function __construct(EntityManager $em, ClassMetadata $class) | ||
| { | ||
| parent::__construct($em, $class); | ||
| $this->Logger = new NullLogger(); | ||
| } | ||
| | ||
| $this->Logger = new NullLogger(); | ||
| } | ||
| | ||
| /** | ||
| * | ||
| * @param LoggerInterface $logger | ||
| */ | ||
| public function setLogger(LoggerInterface $logger) | ||
| { | ||
| $this->Logger = $logger; | ||
| } | ||
| } | ||
| /** | ||
| * | ||
| * @param LoggerInterface $logger | ||
| */ | ||
| public function setLogger(LoggerInterface $logger) | ||
| { | ||
| $this->Logger = $logger; | ||
| } | ||
| } | ||
| | ||
| // ... | ||
| | ||
| | @@ -70,27 +68,25 @@ Proceed with creating the service definition: | |
| | ||
| Finally, use your repository in your standard controller: | ||
| | ||
| <?php | ||
| | ||
| namespace Acme\BlogBundle\Controller; | ||
| namespace Acme\BlogBundle\Controller; | ||
| | ||
| use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
| use Acme\BlogBundle\Entity\Post; | ||
| use Acme\BlogBundle\Entity\Repository\PostRepository; | ||
| 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(); | ||
| 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 | ||
| )); | ||
| } | ||
| 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. I would prefer: '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.
We always try to avoid usage of the first person ("Let's"). Like: "First, you need to create a repository. The process [...]"