Skip to content

Commit 03224d5

Browse files
meel-ioxabbuh
authored andcommitted
Updating Doctrine syntax for getRepository method
1 parent 06ff68e commit 03224d5

File tree

11 files changed

+32
-24
lines changed

11 files changed

+32
-24
lines changed

best_practices/controllers.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ for the homepage of our app:
9595
9696
namespace AppBundle\Controller;
9797
98+
use AppBundle\Entity\Post;
9899
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
99100
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
100101
@@ -106,7 +107,7 @@ for the homepage of our app:
106107
public function indexAction()
107108
{
108109
$posts = $this->getDoctrine()
109-
->getRepository('AppBundle:Post')
110+
->getRepository(Post::class)
110111
->findLatest();
111112
112113
return $this->render('default/index.html.twig', array(
@@ -170,7 +171,7 @@ manually. In our application, we have this situation in ``CommentController``:
170171
public function newAction(Request $request, $postSlug)
171172
{
172173
$post = $this->getDoctrine()
173-
->getRepository('AppBundle:Post')
174+
->getRepository(Post::class)
174175
->findOneBy(array('slug' => $postSlug));
175176
176177
if (!$post) {

best_practices/security.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ more advanced use-case, you can always do the same security check in PHP:
226226
*/
227227
public function editAction($id)
228228
{
229-
$post = $this->getDoctrine()->getRepository('AppBundle:Post')
229+
$post = $this->getDoctrine()->getRepository(Post::class)
230230
->find($id);
231231
232232
if (!$post) {

doctrine.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ on its ``id`` value::
541541
public function showAction($productId)
542542
{
543543
$product = $this->getDoctrine()
544-
->getRepository('AppBundle:Product')
544+
->getRepository(Product::class)
545545
->find($productId);
546546

547547
if (!$product) {
@@ -565,18 +565,18 @@ job is to help you fetch entities of a certain class. You can access the
565565
repository object for an entity class via::
566566

567567
$repository = $this->getDoctrine()
568-
->getRepository('AppBundle:Product');
568+
->getRepository(Product::class);
569569

570570
.. note::
571571

572-
The ``AppBundle:Product`` string is a shortcut you can use anywhere
572+
You can also use ``AppBundle:Product`` syntax. This string is a shortcut you can use anywhere
573573
in Doctrine instead of the full class name of the entity (i.e. ``AppBundle\Entity\Product``).
574574
As long as your entity lives under the ``Entity`` namespace of your bundle,
575575
this will work.
576576

577577
Once you have a repository object, you can access all sorts of helpful methods::
578578

579-
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
579+
$repository = $this->getDoctrine()->getRepository(Product::class);
580580

581581
// query for a single product by its primary key (usually "id")
582582
$product = $repository->find($productId);
@@ -599,7 +599,7 @@ Once you have a repository object, you can access all sorts of helpful methods::
599599
You can also take advantage of the useful ``findBy()`` and ``findOneBy()`` methods
600600
to easily fetch objects based on multiple conditions::
601601

602-
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
602+
$repository = $this->getDoctrine()->getRepository(Product::class);
603603

604604
// query for a single product matching the given name and price
605605
$product = $repository->findOneBy(
@@ -635,7 +635,7 @@ you have a route that maps a product id to an update action in a controller::
635635
public function updateAction($productId)
636636
{
637637
$em = $this->getDoctrine()->getManager();
638-
$product = $em->getRepository('AppBundle:Product')->find($productId);
638+
$product = $em->getRepository(Product::class)->find($productId);
639639

640640
if (!$product) {
641641
throw $this->createNotFoundException(
@@ -681,7 +681,7 @@ Querying for Objects
681681
You've already seen how the repository object allows you to run basic queries
682682
without any work::
683683

684-
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
684+
$repository = $this->getDoctrine()->getRepository(Product::class);
685685

686686
$product = $repository->find($productId);
687687
$product = $repository->findOneByName('Keyboard');
@@ -742,7 +742,7 @@ depends on dynamic conditions, as your code soon becomes hard to read with
742742
DQL as you start to concatenate strings::
743743

744744
$repository = $this->getDoctrine()
745-
->getRepository('AppBundle:Product');
745+
->getRepository(Product::class);
746746

747747
// createQueryBuilder() automatically selects FROM AppBundle:Product
748748
// and aliases it to "p"

doctrine/associations.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ did before. First, fetch a ``$product`` object and then access its related
276276
public function showAction($productId)
277277
{
278278
$product = $this->getDoctrine()
279-
->getRepository('AppBundle:Product')
279+
->getRepository(Product::class)
280280
->find($productId);
281281

282282
$categoryName = $product->getCategory()->getName();
@@ -303,7 +303,7 @@ You can also query in the other direction::
303303
public function showProductsAction($categoryId)
304304
{
305305
$category = $this->getDoctrine()
306-
->getRepository('AppBundle:Category')
306+
->getRepository(Category::class)
307307
->find($categoryId);
308308

309309
$products = $category->getProducts();
@@ -324,7 +324,7 @@ to the given ``Category`` object via their ``category_id`` value.
324324
example::
325325

326326
$product = $this->getDoctrine()
327-
->getRepository('AppBundle:Product')
327+
->getRepository(Product::class)
328328
->find($productId);
329329

330330
$category = $product->getCategory();
@@ -385,7 +385,7 @@ object and its related ``Category`` with just one query::
385385
public function showAction($productId)
386386
{
387387
$product = $this->getDoctrine()
388-
->getRepository('AppBundle:Product')
388+
->getRepository(Product::class)
389389
->findOneByIdJoinedToCategory($productId);
390390

391391
$category = $product->getCategory();

doctrine/multiple_entity_managers.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,28 @@ entity manager to persist and fetch its entities.
206206

207207
The same applies to repository calls::
208208

209+
use AcmeStoreBundle\Entity\Customer;
210+
use AcmeStoreBundle\Entity\Product;
211+
209212
class UserController extends Controller
210213
{
211214
public function indexAction()
212215
{
213216
// Retrieves a repository managed by the "default" em
214217
$products = $this->get('doctrine')
215-
->getRepository('AcmeStoreBundle:Product')
218+
->getRepository(Product::class)
216219
->findAll()
217220
;
218221

219222
// Explicit way to deal with the "default" em
220223
$products = $this->get('doctrine')
221-
->getRepository('AcmeStoreBundle:Product', 'default')
224+
->getRepository(Product::class, 'default')
222225
->findAll()
223226
;
224227

225228
// Retrieves a repository managed by the "customer" em
226229
$customers = $this->get('doctrine')
227-
->getRepository('AcmeCustomerBundle:Customer', 'customer')
230+
->getRepository(Customer::class, 'customer')
228231
->findAll()
229232
;
230233
}

doctrine/repository.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ entities, ordered alphabetically by name.
8787
You can use this new method just like the default finder methods of the repository::
8888

8989
$em = $this->getDoctrine()->getManager();
90-
$products = $em->getRepository('AppBundle:Product')
90+
$products = $em->getRepository(Product::class)
9191
->findAllOrderedByName();
9292

9393
.. note::

form/data_transformers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ to and from the issue number and the ``Issue`` object::
202202
}
203203

204204
$issue = $this->manager
205-
->getRepository('AppBundle:Issue')
205+
->getRepository(Issue::class)
206206
// query for the issue with this id
207207
->find($issueNumber)
208208
;

form/form_collections.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,14 @@ the relationship between the removed ``Tag`` and ``Task`` object.
685685

686686
// src/AppBundle/Controller/TaskController.php
687687

688+
use AppBundle\Entity\Task;
688689
use Doctrine\Common\Collections\ArrayCollection;
689690

690691
// ...
691692
public function editAction($id, Request $request)
692693
{
693694
$em = $this->getDoctrine()->getManager();
694-
$task = $em->getRepository('AppBundle:Task')->find($id);
695+
$task = $em->getRepository(Task::class)->find($id);
695696

696697
if (!$task) {
697698
throw $this->createNotFoundException('No task found for id '.$id);

introduction/from_flat_php_to_symfony2.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ them for you. Here's the same sample application, now built in Symfony::
544544
// src/AppBundle/Controller/BlogController.php
545545
namespace AppBundle\Controller;
546546

547+
use AppBundle\Entity\Post;
547548
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
548549

549550
class BlogController extends Controller
@@ -562,7 +563,7 @@ them for you. Here's the same sample application, now built in Symfony::
562563
{
563564
$post = $this->get('doctrine')
564565
->getManager()
565-
->getRepository('AppBundle:Post')
566+
->getRepository(Post::class)
566567
->find($id);
567568

568569
if (!$post) {

testing/database.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Suppose the class you want to test looks like this::
3535

3636
namespace AppBundle\Salary;
3737

38+
use AppBundle\Entity\Employee;
3839
use Doctrine\Common\Persistence\ObjectManager;
3940

4041
class SalaryCalculator
@@ -49,7 +50,7 @@ Suppose the class you want to test looks like this::
4950
public function calculateTotalSalary($id)
5051
{
5152
$employeeRepository = $this->entityManager
52-
->getRepository('AppBundle:Employee');
53+
->getRepository(Employee::class);
5354
$employee = $employeeRepository->find($id);
5455

5556
return $employee->getSalary() + $employee->getBonus();

testing/doctrine.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ which makes all of this quite easy::
2121
// src/AppBundle/Tests/Repository/ProductRepositoryFunctionalTest.php
2222
namespace AppBundle\Tests\Repository;
2323

24+
use AppBundle\Entity\Product;
2425
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2526

2627
class ProductRepositoryFunctionalTest extends KernelTestCase
@@ -45,7 +46,7 @@ which makes all of this quite easy::
4546
public function testSearchByCategoryName()
4647
{
4748
$products = $this->em
48-
->getRepository('AppBundle:Product')
49+
->getRepository(Product::class)
4950
->searchByCategoryName('foo')
5051
;
5152

0 commit comments

Comments
 (0)