@@ -400,56 +400,8 @@ Even though Doctrine now knows how to persist a ``Product`` object to the
400
400
database, the class itself isn't really useful yet. Since ``Product `` is just
401
401
a regular PHP class with ``private `` properties, you need to create ``public ``
402
402
getter and setter methods (e.g. ``getName() ``, ``setName($name) ``) in order
403
- to access its properties in the rest of your application's code. Fortunately,
404
- the following command can generate these boilerplate methods automatically:
405
-
406
- .. code-block :: terminal
407
-
408
- $ php app/console doctrine:generate:entities AppBundle/Entity/Product
409
-
410
- This command makes sure that all the getters and setters are generated
411
- for the ``Product `` class. This is a safe command - you can run it over and
412
- over again: it only generates getters and setters that don't exist (i.e. it
413
- doesn't replace your existing methods).
414
-
415
- .. caution ::
416
-
417
- Keep in mind that Doctrine's entity generator produces simple getters/setters.
418
- You should review the generated methods and add any logic, if necessary,
419
- to suit the needs of your application.
420
-
421
- .. sidebar :: More about ``doctrine:generate:entities``
422
-
423
- With the ``doctrine:generate:entities `` command you can:
424
-
425
- * generate getter and setter methods in entity classes;
426
-
427
- * generate repository classes on behalf of entities configured with the
428
- ``@ORM\Entity(repositoryClass="...") `` annotation;
429
-
430
- * generate the appropriate constructor for 1:n and n: m relations.
431
-
432
- The ``doctrine:generate:entities `` command saves a backup of the original
433
- ``Product.php `` named ``Product.php~ ``. In some cases, the presence of
434
- this file can cause a "Cannot redeclare class" error. It can be safely
435
- removed. You can also use the ``--no-backup `` option to prevent generating
436
- these backup files.
437
-
438
- Note that you don't *need * to use this command. You could also write the
439
- necessary getters and setters by hand. This option simply exists to save
440
- you time, since creating these methods is often a common task during
441
- development.
442
-
443
- You can also generate all known entities (i.e. any PHP class with Doctrine
444
- mapping information) of a bundle or an entire namespace:
445
-
446
- .. code-block :: terminal
447
-
448
- # generates all entities in the AppBundle
449
- $ php app/console doctrine:generate:entities AppBundle
450
-
451
- # generates all entities of bundles in the Acme namespace
452
- $ php app/console doctrine:generate:entities Acme
403
+ to access its properties in the rest of your application's code. Add these
404
+ methods manually or with your own IDE.
453
405
454
406
.. _doctrine-creating-the-database-tables-schema :
455
407
@@ -589,7 +541,7 @@ on its ``id`` value::
589
541
public function showAction($productId)
590
542
{
591
543
$product = $this->getDoctrine()
592
- ->getRepository('AppBundle: Product' )
544
+ ->getRepository(Product::class )
593
545
->find($productId);
594
546
595
547
if (!$product) {
@@ -613,18 +565,18 @@ job is to help you fetch entities of a certain class. You can access the
613
565
repository object for an entity class via::
614
566
615
567
$repository = $this->getDoctrine()
616
- ->getRepository('AppBundle: Product' );
568
+ ->getRepository(Product::class );
617
569
618
570
.. note ::
619
571
620
- 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
621
573
in Doctrine instead of the full class name of the entity (i.e. ``AppBundle\Entity\Product ``).
622
574
As long as your entity lives under the ``Entity `` namespace of your bundle,
623
575
this will work.
624
576
625
577
Once you have a repository object, you can access all sorts of helpful methods::
626
578
627
- $repository = $this->getDoctrine()->getRepository('AppBundle: Product' );
579
+ $repository = $this->getDoctrine()->getRepository(Product::class );
628
580
629
581
// query for a single product by its primary key (usually "id")
630
582
$product = $repository->find($productId);
@@ -647,7 +599,7 @@ Once you have a repository object, you can access all sorts of helpful methods::
647
599
You can also take advantage of the useful ``findBy() `` and ``findOneBy() `` methods
648
600
to easily fetch objects based on multiple conditions::
649
601
650
- $repository = $this->getDoctrine()->getRepository('AppBundle: Product' );
602
+ $repository = $this->getDoctrine()->getRepository(Product::class );
651
603
652
604
// query for a single product matching the given name and price
653
605
$product = $repository->findOneBy(
@@ -680,10 +632,13 @@ Updating an Object
680
632
Once you've fetched an object from Doctrine, updating it is easy. Suppose
681
633
you have a route that maps a product id to an update action in a controller::
682
634
635
+ use AppBundle\Entity\Post;
636
+ // ...
637
+
683
638
public function updateAction($productId)
684
639
{
685
640
$em = $this->getDoctrine()->getManager();
686
- $product = $em->getRepository('AppBundle: Product' )->find($productId);
641
+ $product = $em->getRepository(Product::class )->find($productId);
687
642
688
643
if (!$product) {
689
644
throw $this->createNotFoundException(
@@ -729,7 +684,7 @@ Querying for Objects
729
684
You've already seen how the repository object allows you to run basic queries
730
685
without any work::
731
686
732
- $repository = $this->getDoctrine()->getRepository('AppBundle: Product' );
687
+ $repository = $this->getDoctrine()->getRepository(Product::class );
733
688
734
689
$product = $repository->find($productId);
735
690
$product = $repository->findOneByName('Keyboard');
@@ -790,7 +745,7 @@ depends on dynamic conditions, as your code soon becomes hard to read with
790
745
DQL as you start to concatenate strings::
791
746
792
747
$repository = $this->getDoctrine()
793
- ->getRepository('AppBundle: Product' );
748
+ ->getRepository(Product::class );
794
749
795
750
// createQueryBuilder() automatically selects FROM AppBundle:Product
796
751
// and aliases it to "p"
0 commit comments