@@ -445,56 +445,8 @@ Even though Doctrine now knows how to persist a ``Product`` object to the
445
445
database, the class itself isn't really useful yet. Since ``Product `` is just
446
446
a regular PHP class with ``private `` properties, you need to create ``public ``
447
447
getter and setter methods (e.g. ``getName() ``, ``setName($name) ``) in order
448
- to access its properties in the rest of your application's code. Fortunately,
449
- the following command can generate these boilerplate methods automatically:
450
-
451
- .. code-block :: terminal
452
-
453
- $ php bin/console doctrine:generate:entities AppBundle/Entity/Product
454
-
455
- This command makes sure that all the getters and setters are generated
456
- for the ``Product `` class. This is a safe command - you can run it over and
457
- over again: it only generates getters and setters that don't exist (i.e. it
458
- doesn't replace your existing methods).
459
-
460
- .. caution ::
461
-
462
- Keep in mind that Doctrine's entity generator produces simple getters/setters.
463
- You should review the generated methods and add any logic, if necessary,
464
- to suit the needs of your application.
465
-
466
- .. sidebar :: More about ``doctrine:generate:entities``
467
-
468
- With the ``doctrine:generate:entities `` command you can:
469
-
470
- * generate getter and setter methods in entity classes;
471
-
472
- * generate repository classes on behalf of entities configured with the
473
- ``@ORM\Entity(repositoryClass="...") `` annotation;
474
-
475
- * generate the appropriate constructor for 1:n and n: m relations.
476
-
477
- The ``doctrine:generate:entities `` command saves a backup of the original
478
- ``Product.php `` named ``Product.php~ ``. In some cases, the presence of
479
- this file can cause a "Cannot redeclare class" error. It can be safely
480
- removed. You can also use the ``--no-backup `` option to prevent generating
481
- these backup files.
482
-
483
- Note that you don't *need * to use this command. You could also write the
484
- necessary getters and setters by hand. This option simply exists to save
485
- you time, since creating these methods is often a common task during
486
- development.
487
-
488
- You can also generate all known entities (i.e. any PHP class with Doctrine
489
- mapping information) of a bundle or an entire namespace:
490
-
491
- .. code-block :: terminal
492
-
493
- # generates all entities in the AppBundle
494
- $ php bin/console doctrine:generate:entities AppBundle
495
-
496
- # generates all entities of bundles in the Acme namespace
497
- $ php bin/console doctrine:generate:entities Acme
448
+ to access its properties in the rest of your application's code. Add these
449
+ methods manually or with your own IDE.
498
450
499
451
.. _doctrine-creating-the-database-tables-schema :
500
452
@@ -637,7 +589,7 @@ on its ``id`` value::
637
589
public function showAction($productId)
638
590
{
639
591
$product = $this->getDoctrine()
640
- ->getRepository('AppBundle: Product' )
592
+ ->getRepository(Product::class )
641
593
->find($productId);
642
594
643
595
if (!$product) {
@@ -660,18 +612,19 @@ as its "repository". You can think of a repository as a PHP class whose only
660
612
job is to help you fetch entities of a certain class. You can access the
661
613
repository object for an entity class via::
662
614
663
- $repository = $em->getRepository('AppBundle:Product');
615
+ $repository = $this->getDoctrine()
616
+ ->getRepository(Product::class);
664
617
665
618
.. note ::
666
619
667
- The ``AppBundle:Product `` string is a shortcut you can use anywhere
620
+ You can also use ``AppBundle:Product `` syntax. This string is a shortcut you can use anywhere
668
621
in Doctrine instead of the full class name of the entity (i.e. ``AppBundle\Entity\Product ``).
669
622
As long as your entity lives under the ``Entity `` namespace of your bundle,
670
623
this will work.
671
624
672
625
Once you have a repository object, you can access all sorts of helpful methods::
673
626
674
- $repository = $em-> getRepository('AppBundle: Product' );
627
+ $repository = $this->getDoctrine()-> getRepository(Product::class );
675
628
676
629
// query for a single product by its primary key (usually "id")
677
630
$product = $repository->find($productId);
@@ -694,7 +647,7 @@ Once you have a repository object, you can access all sorts of helpful methods::
694
647
You can also take advantage of the useful ``findBy() `` and ``findOneBy() `` methods
695
648
to easily fetch objects based on multiple conditions::
696
649
697
- $repository = $em-> getRepository('AppBundle: Product' );
650
+ $repository = $this->getDoctrine()-> getRepository(Product::class );
698
651
699
652
// query for a single product matching the given name and price
700
653
$product = $repository->findOneBy(
@@ -727,11 +680,13 @@ Updating an Object
727
680
Once you've fetched an object from Doctrine, updating it is easy. Suppose
728
681
you have a route that maps a product id to an update action in a controller::
729
682
683
+ use AppBundle\Entity\Post;
684
+ // ...
685
+
730
686
public function updateAction($productId)
731
687
{
732
- $product = $this->getDoctrine()
733
- ->getRepository('AppBundle:Product')
734
- ->find($productId);
688
+ $em = $this->getDoctrine()->getManager();
689
+ $product = $em->getRepository(Product::class)->find($productId);
735
690
736
691
if (!$product) {
737
692
throw $this->createNotFoundException(
@@ -777,7 +732,7 @@ Querying for Objects
777
732
You've already seen how the repository object allows you to run basic queries
778
733
without any work::
779
734
780
- $repository = $em-> getRepository('AppBundle: Product' );
735
+ $repository = $this->getDoctrine()-> getRepository(Product::class );
781
736
782
737
$product = $repository->find($productId);
783
738
$product = $repository->findOneByName('Keyboard');
@@ -836,7 +791,8 @@ Instead of writing a DQL string, you can use a helpful object called the
836
791
depends on dynamic conditions, as your code soon becomes hard to read with
837
792
DQL as you start to concatenate strings::
838
793
839
- $repository = $em->getRepository('AppBundle:Product');
794
+ $repository = $this->getDoctrine()
795
+ ->getRepository(Product::class);
840
796
841
797
// createQueryBuilder() automatically selects FROM AppBundle:Product
842
798
// and aliases it to "p"
0 commit comments