Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Go Pro with Doctrine Queries

1:26:33

What you'll be learning

// 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!


Your Guides

Victor Bocharsky Ryan Weaver

Buy Access

Join the Conversation?

6
Login or Register to join the conversation
Lionel-F Avatar
Lionel-F Avatar Lionel-F | posted 2 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
S-H Avatar

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
S-H Avatar

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
Cat in space

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

userVoice