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