@@ -400,56 +400,8 @@ Even though Doctrine now knows how to persist a ``Product`` object to the
400400database, the class itself isn't really useful yet. Since ``Product `` is just
401401a regular PHP class with ``private `` properties, you need to create ``public ``
402402getter 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.
453405
454406.. _doctrine-creating-the-database-tables-schema :
455407
@@ -589,7 +541,7 @@ on its ``id`` value::
589541 public function showAction($productId)
590542 {
591543 $product = $this->getDoctrine()
592- ->getRepository('AppBundle: Product' )
544+ ->getRepository(Product::class )
593545 ->find($productId);
594546
595547 if (!$product) {
@@ -613,18 +565,18 @@ job is to help you fetch entities of a certain class. You can access the
613565repository object for an entity class via::
614566
615567 $repository = $this->getDoctrine()
616- ->getRepository('AppBundle: Product' );
568+ ->getRepository(Product::class );
617569
618570.. note ::
619571
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
621573 in Doctrine instead of the full class name of the entity (i.e. ``AppBundle\Entity\Product ``).
622574 As long as your entity lives under the ``Entity `` namespace of your bundle,
623575 this will work.
624576
625577Once you have a repository object, you can access all sorts of helpful methods::
626578
627- $repository = $this->getDoctrine()->getRepository('AppBundle: Product' );
579+ $repository = $this->getDoctrine()->getRepository(Product::class );
628580
629581 // query for a single product by its primary key (usually "id")
630582 $product = $repository->find($productId);
@@ -647,7 +599,7 @@ Once you have a repository object, you can access all sorts of helpful methods::
647599You can also take advantage of the useful ``findBy() `` and ``findOneBy() `` methods
648600to easily fetch objects based on multiple conditions::
649601
650- $repository = $this->getDoctrine()->getRepository('AppBundle: Product' );
602+ $repository = $this->getDoctrine()->getRepository(Product::class );
651603
652604 // query for a single product matching the given name and price
653605 $product = $repository->findOneBy(
@@ -680,10 +632,13 @@ Updating an Object
680632Once you've fetched an object from Doctrine, updating it is easy. Suppose
681633you have a route that maps a product id to an update action in a controller::
682634
635+ use AppBundle\Entity\Post;
636+ // ...
637+
683638 public function updateAction($productId)
684639 {
685640 $em = $this->getDoctrine()->getManager();
686- $product = $em->getRepository('AppBundle: Product' )->find($productId);
641+ $product = $em->getRepository(Product::class )->find($productId);
687642
688643 if (!$product) {
689644 throw $this->createNotFoundException(
@@ -729,7 +684,7 @@ Querying for Objects
729684You've already seen how the repository object allows you to run basic queries
730685without any work::
731686
732- $repository = $this->getDoctrine()->getRepository('AppBundle: Product' );
687+ $repository = $this->getDoctrine()->getRepository(Product::class );
733688
734689 $product = $repository->find($productId);
735690 $product = $repository->findOneByName('Keyboard');
@@ -790,7 +745,7 @@ depends on dynamic conditions, as your code soon becomes hard to read with
790745DQL as you start to concatenate strings::
791746
792747 $repository = $this->getDoctrine()
793- ->getRepository('AppBundle: Product' );
748+ ->getRepository(Product::class );
794749
795750 // createQueryBuilder() automatically selects FROM AppBundle:Product
796751 // and aliases it to "p"
0 commit comments