@@ -51,7 +51,7 @@ The database connection information is stored as an environment variable called
51
51
52
52
# to use sqlite:
53
53
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
54
-
54
+
55
55
# to use postgresql:
56
56
# DATABASE_URL="postgresql://db_user:[email protected] :5432/db_name?serverVersion=11&charset=utf8"
57
57
@@ -502,46 +502,60 @@ Fetching an object back out of the database is even easier. Suppose you want to
502
502
be able to go to ``/product/1 `` to see your new product::
503
503
504
504
// src/Controller/ProductController.php
505
+ namespace App\Controller;
506
+
507
+ use App\Entity\Product;
508
+ use Symfony\Component\HttpFoundation\Response;
505
509
// ...
506
510
507
- /**
508
- * @Route("/product/{id}", name="product_show")
509
- */
510
- public function show($id)
511
+ class ProductController extends AbstractController
511
512
{
512
- $product = $this->getDoctrine()
513
- ->getRepository(Product::class)
514
- ->find($id);
515
-
516
- if (!$product) {
517
- throw $this->createNotFoundException(
518
- 'No product found for id '.$id
519
- );
520
- }
513
+ /**
514
+ * @Route("/product/{id}", name="product_show")
515
+ */
516
+ public function show(int $id): Response
517
+ {
518
+ $product = $this->getDoctrine()
519
+ ->getRepository(Product::class)
520
+ ->find($id);
521
+
522
+ if (!$product) {
523
+ throw $this->createNotFoundException(
524
+ 'No product found for id '.$id
525
+ );
526
+ }
521
527
522
- return new Response('Check out this great product: '.$product->getName());
528
+ return new Response('Check out this great product: '.$product->getName());
523
529
524
- // or render a template
525
- // in the template, print things with {{ product.name }}
526
- // return $this->render('product/show.html.twig', ['product' => $product]);
530
+ // or render a template
531
+ // in the template, print things with {{ product.name }}
532
+ // return $this->render('product/show.html.twig', ['product' => $product]);
533
+ }
527
534
}
528
535
529
536
Another possibility is to use the ``ProductRepository `` using Symfony's autowiring
530
537
and injected by the dependency injection container::
531
538
532
539
// src/Controller/ProductController.php
533
- // ...
540
+ namespace App\Controller;
541
+
542
+ use App\Entity\Product;
534
543
use App\Repository\ProductRepository;
544
+ use Symfony\Component\HttpFoundation\Response;
545
+ // ...
535
546
536
- /**
537
- * @Route("/product/{id}", name="product_show")
538
- */
539
- public function show($id, ProductRepository $productRepository)
547
+ class ProductController extends AbstractController
540
548
{
541
- $product = $productRepository
542
- ->find($id);
549
+ /**
550
+ * @Route("/product/{id}", name="product_show")
551
+ */
552
+ public function show(int $id, ProductRepository $productRepository): Response
553
+ {
554
+ $product = $productRepository
555
+ ->find($id);
543
556
544
- // ...
557
+ // ...
558
+ }
545
559
}
546
560
547
561
Try it out!
@@ -607,15 +621,23 @@ for you automatically! First, install the bundle in case you don't have it:
607
621
Now, simplify your controller::
608
622
609
623
// src/Controller/ProductController.php
624
+ namespace App\Controller;
625
+
610
626
use App\Entity\Product;
627
+ use App\Repository\ProductRepository;
628
+ use Symfony\Component\HttpFoundation\Response;
629
+ // ...
611
630
612
- /**
613
- * @Route("/product/{id}", name="product_show")
614
- */
615
- public function show(Product $product)
631
+ class ProductController extends AbstractController
616
632
{
617
- // use the Product!
618
- // ...
633
+ /**
634
+ * @Route("/product/{id}", name="product_show")
635
+ */
636
+ public function show(Product $product): Response
637
+ {
638
+ // use the Product!
639
+ // ...
640
+ }
619
641
}
620
642
621
643
That's it! The bundle uses the ``{id} `` from the route to query for the ``Product ``
@@ -629,26 +651,37 @@ Updating an Object
629
651
Once you've fetched an object from Doctrine, you interact with it the same as
630
652
with any PHP model::
631
653
632
- /**
633
- * @Route("/product/edit/{id}")
634
- */
635
- public function update($id)
654
+ // src/Controller/ProductController.php
655
+ namespace App\Controller;
656
+
657
+ use App\Entity\Product;
658
+ use App\Repository\ProductRepository;
659
+ use Symfony\Component\HttpFoundation\Response;
660
+ // ...
661
+
662
+ class ProductController extends AbstractController
636
663
{
637
- $entityManager = $this->getDoctrine()->getManager();
638
- $product = $entityManager->getRepository(Product::class)->find($id);
664
+ /**
665
+ * @Route("/product/edit/{id}")
666
+ */
667
+ public function update(int $id): Response
668
+ {
669
+ $entityManager = $this->getDoctrine()->getManager();
670
+ $product = $entityManager->getRepository(Product::class)->find($id);
639
671
640
- if (!$product) {
641
- throw $this->createNotFoundException(
642
- 'No product found for id '.$id
643
- );
644
- }
672
+ if (!$product) {
673
+ throw $this->createNotFoundException(
674
+ 'No product found for id '.$id
675
+ );
676
+ }
645
677
646
- $product->setName('New product name!');
647
- $entityManager->flush();
678
+ $product->setName('New product name!');
679
+ $entityManager->flush();
648
680
649
- return $this->redirectToRoute('product_show', [
650
- 'id' => $product->getId()
651
- ]);
681
+ return $this->redirectToRoute('product_show', [
682
+ 'id' => $product->getId()
683
+ ]);
684
+ }
652
685
}
653
686
654
687
Using Doctrine to edit an existing product consists of three steps:
@@ -724,7 +757,7 @@ a new method for this to your repository::
724
757
/**
725
758
* @return Product[]
726
759
*/
727
- public function findAllGreaterThanPrice($price): array
760
+ public function findAllGreaterThanPrice(int $price): array
728
761
{
729
762
$entityManager = $this->getEntityManager();
730
763
@@ -769,25 +802,28 @@ based on PHP conditions)::
769
802
// src/Repository/ProductRepository.php
770
803
771
804
// ...
772
- public function findAllGreaterThanPrice($price, $includeUnavailableProducts = false): array
805
+ class ProductRepository extends ServiceEntityRepository
773
806
{
774
- // automatically knows to select Products
775
- // the "p" is an alias you'll use in the rest of the query
776
- $qb = $this->createQueryBuilder('p')
777
- ->where('p.price > :price')
778
- ->setParameter('price', $price)
779
- ->orderBy('p.price', 'ASC');
780
-
781
- if (!$includeUnavailableProducts) {
782
- $qb->andWhere('p.available = TRUE');
783
- }
807
+ public function findAllGreaterThanPrice(int $price, bool $includeUnavailableProducts = false): array
808
+ {
809
+ // automatically knows to select Products
810
+ // the "p" is an alias you'll use in the rest of the query
811
+ $qb = $this->createQueryBuilder('p')
812
+ ->where('p.price > :price')
813
+ ->setParameter('price', $price)
814
+ ->orderBy('p.price', 'ASC');
815
+
816
+ if (!$includeUnavailableProducts) {
817
+ $qb->andWhere('p.available = TRUE');
818
+ }
784
819
785
- $query = $qb->getQuery();
820
+ $query = $qb->getQuery();
786
821
787
- return $query->execute();
822
+ return $query->execute();
788
823
789
- // to get just one result:
790
- // $product = $query->setMaxResults(1)->getOneOrNullResult();
824
+ // to get just one result:
825
+ // $product = $query->setMaxResults(1)->getOneOrNullResult();
826
+ }
791
827
}
792
828
793
829
Querying with SQL
@@ -798,20 +834,23 @@ In addition, you can query directly with SQL if you need to::
798
834
// src/Repository/ProductRepository.php
799
835
800
836
// ...
801
- public function findAllGreaterThanPrice($price): array
837
+ class ProductRepository extends ServiceEntityRepository
802
838
{
803
- $conn = $this->getEntityManager()->getConnection();
804
-
805
- $sql = '
806
- SELECT * FROM product p
807
- WHERE p.price > :price
808
- ORDER BY p.price ASC
809
- ';
810
- $stmt = $conn->prepare($sql);
811
- $stmt->execute(['price' => $price]);
812
-
813
- // returns an array of arrays (i.e. a raw data set)
814
- return $stmt->fetchAllAssociative();
839
+ public function findAllGreaterThanPrice(int $price): array
840
+ {
841
+ $conn = $this->getEntityManager()->getConnection();
842
+
843
+ $sql = '
844
+ SELECT * FROM product p
845
+ WHERE p.price > :price
846
+ ORDER BY p.price ASC
847
+ ';
848
+ $stmt = $conn->prepare($sql);
849
+ $stmt->execute(['price' => $price]);
850
+
851
+ // returns an array of arrays (i.e. a raw data set)
852
+ return $stmt->fetchAllAssociative();
853
+ }
815
854
}
816
855
817
856
With SQL, you will get back raw data, not objects (unless you use the `NativeQuery `_
0 commit comments