@@ -701,28 +701,30 @@ a new method for this to your repository::
701701 }
702702
703703 /**
704- * @param $price
705704 * @return Product[]
706705 */
707706 public function findAllGreaterThanPrice($price): array
708707 {
709- // automatically knows to select Products
710- // the "p" is an alias you'll use in the rest of the query
711- $qb = $this->createQueryBuilder('p')
712- ->andWhere('p.price > :price')
713- ->setParameter('price', $price)
714- ->orderBy('p.price', 'ASC')
715- ->getQuery();
716-
717- return $qb->execute();
718-
719- // to get just one result:
720- // $product = $qb->setMaxResults(1)->getOneOrNullResult();
708+ $entityManager = $this->getEntityManager();
709+
710+ $query = $entityManager->createQuery(
711+ 'SELECT p
712+ FROM App\Entity\Product p
713+ WHERE p.price > :price
714+ ORDER BY p.price ASC'
715+ )->setParameter('price', $price);
716+
717+ // returns an array of Product objects
718+ return $query->getResult();
721719 }
722720 }
723721
724- This uses Doctrine's `Query Builder `_: a very powerful and user-friendly way to
725- write custom queries. Now, you can call this method on the repository::
722+ The string passed to ``createQuery() `` might look like SQL, but it is
723+ `Doctrine Query Language `_. This allows you to type queries using commonly
724+ known query language, but referencing PHP objects instead (i.e. in the ``FROM ``
725+ statement).
726+
727+ Now, you can call this method on the repository::
726728
727729 // from inside a controller
728730 $minPrice = 1000;
@@ -736,36 +738,45 @@ write custom queries. Now, you can call this method on the repository::
736738If you're in a :ref: `services-constructor-injection `, you can type-hint the
737739``ProductRepository `` class and inject it like normal.
738740
739- For more details, see the `Query Builder `_ Documentation from Doctrine.
741+ Querying with the Query Builder
742+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
740743
741- Querying with DQL or SQL
742- ------------------------
743-
744- In addition to the query builder, you can also query with `Doctrine Query Language `_::
744+ Doctrine also provides a `Query Builder `_, an object-oriented way to write
745+ queries. It is recommended to use this when queries and build dynamically (i.e.
746+ based on PHP conditions)::
745747
746748 // src/Repository/ProductRepository.php
747- // ...
748749
749- public function findAllGreaterThanPrice($price): array
750+ // ...
751+ public function findAllGreaterThanPrice($price, $includeUnavailableProducts = false): array
750752 {
751- $entityManager = $this->getEntityManager();
753+ // automatically knows to select Products
754+ // the "p" is an alias you'll use in the rest of the query
755+ $qb = $this->createQueryBuilder('p')
756+ ->where('p.price > :price')
757+ ->setParameter('price', $price)
758+ ->orderBy('p.price', 'ASC')
759+
760+ if (!$includeUnavailableProducts) {
761+ $qb->andWhere('p.available = TRUE')
762+ }
752763
753- $query = $entityManager->createQuery(
754- 'SELECT p
755- FROM App\Entity\Product p
756- WHERE p.price > :price
757- ORDER BY p.price ASC'
758- )->setParameter('price', $price);
764+ $query = $qb->getQuery();
759765
760- // returns an array of Product objects
761766 return $query->execute();
767+
768+ // to get just one result:
769+ // $product = $query->setMaxResults(1)->getOneOrNullResult();
762770 }
763771
764- Or directly with SQL if you need to::
772+ Querying with SQL
773+ ~~~~~~~~~~~~~~~~~
774+
775+ In addition, you can query directly with SQL if you need to::
765776
766777 // src/Repository/ProductRepository.php
767- // ...
768778
779+ // ...
769780 public function findAllGreaterThanPrice($price): array
770781 {
771782 $conn = $this->getEntityManager()->getConnection();
0 commit comments