> Symfony 6 >

Course Overview

Go Pro with Doctrine Queries

  • 1259 students
  • EN/ES Captions
  • EN/ES Script
  • Certificate of Completion

Your Guides

About this course

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "beberlei/doctrineextensions": "^1.3", // v1.3.0
        "doctrine/doctrine-bundle": "^2.7", // 2.9.1
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
        "doctrine/orm": "^2.13", // 2.15.1
        "symfony/asset": "6.2.*", // v6.2.7
        "symfony/console": "6.2.*", // v6.2.10
        "symfony/dotenv": "6.2.*", // v6.2.8
        "symfony/flex": "^2", // v2.2.5
        "symfony/framework-bundle": "6.2.*", // v6.2.10
        "symfony/proxy-manager-bridge": "6.2.*", // v6.2.7
        "symfony/runtime": "6.2.*", // v6.2.8
        "symfony/twig-bundle": "6.2.*", // v6.2.7
        "symfony/webpack-encore-bundle": "^1.16", // v1.16.1
        "symfony/yaml": "6.2.*" // v6.2.10
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.4
        "symfony/maker-bundle": "^1.47", // v1.48.0
        "symfony/stopwatch": "6.2.*", // v6.2.7
        "symfony/web-profiler-bundle": "6.2.*", // v6.2.10
        "zenstruck/foundry": "^1.22" // v1.32.0
    }
}

You already know how to query with SQL, so learning how to do complex queries with Doctrine should not be a pain! In this tutorial, we'll learn about:

  • the language Doctrine speaks (DQL)
  • the QueryBuilder object
  • JOINs! And using them to reduce queries
  • SELECTing specific fields instead of entire objects
  • If all else fails, run raw SQL queries!
  • Using Criteria to efficiently filter relation collections

And more! Let's see what kind of crazy queries we can create!

Next courses in the Symfony 6: Tools, Tools, Tools! section of the Symfony 6 Track!

8 Comments

Sort By
Login or Register to join the conversation
Eric-N avatar Eric-N 6 months ago

Hi,

I´m interested in this course but I´m using Mezzio instead of Symfony as PHP-Framework, so I would like to know if the things you learn in this course about Doctrine are framework independent ?

| Reply |

Hey @Eric-N!

Good question! This tutorial is all about Doctrine, but we are absolutely working inside of Symfony. So the directory structure, bin/console commands (e.g. to generate migrations), and more will not translate. So, while I think you can still learn a lot about Doctrine in this course, you'll definitely need to do some translating to how that looks and works outside of Symfony. If you have any questions along the way, we'd be happy to do our best to answer them :).

Cheers!

| Reply |
Lionel-F avatar Lionel-F 9 months ago

Hi guys will you talk about adding a subquery?

| Reply |

Hey Lionel,

Unfortunately, no. But it should be the same DQL query in another DQL, this should work. But subqueries are mostly bad idea, sometimes it might be better (much better performance) to execute 2 separate queries. If you use subqueries - I would recommend you to profile those queries to see how long they are executing, then rewrite logic with 2 queries and compare. I bet in most cases 2 separate queries will be faster.

Cheers!

| Reply |

Great news! I would like to take this opportunity to share this article with you: https://soyuka.me/esql-alternative-to-doctrine-query-language-why/

Maybe it could give you some ideas 😇

| Reply |

The article is quite interesting. But try https://api-platform.com/docs/core/serialization/#embedding-relations . Probably you will achieve the same. If not, you would probably have to look at Doctrine and the Eager Loading. But I have to admit that in my first project with API Platform I encountered an unfavorable combination, combined with quite interesting bugs that e.g. made a difference on which platform the project was built... So I am not completly sure...

| Reply |

Thank you for your answer, do you have an example, or a link that shows the use of Eager Loading?

| Reply |

In combination with API-Platform it is explained here: https://api-platform.com/docs/core/performance/#eager-loading. There are multiple ways.

But I don't see an example for fetching an embedded relation one time with, and another without an eager collection, depending on the route.

For example you have customers with invoices, and each invoice has many products on it.
If you want to show an overview of the invoices you want to show all invoices and a number of positions, but not the product Detail-Data.
But if you want to show an invoice you want to show the invoices with the product data.
I think in this case, you can create a second getter, giving the exact same relation with another name and wihtout eager loading. In combination with https://api-platform.com/docs/core/serialization/#embedding-relations > Normalization Groups ( #[Groups('book')] ) even this should be possbile.

1 | Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!