> Symfony 6 >

Course Overview

Hazte profesional con las consultas de Doctrine

Dive into Doctrine queries with this tutorial. Learn DQL, efficient use of JOINs, and specific field selection in Symfony!

  • 1348 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

Ya sabes hacer consultas con SQL, ¡así que aprender a hacer consultas complejas con Doctrine no debería ser un suplicio! En este tutorial, aprenderemos sobre:

  • El lenguaje que habla Doctrine (DQL)
  • El objeto QueryBuilder
  • Los JOIN Y cómo utilizarlos para reducir las consultas
  • Seleccionar campos específicos en lugar de objetos enteros
  • Si todo lo demás falla, ¡ejecuta consultas directas de SQL!
  • Utilizando Criteria para filtrar eficazmente colecciones de relaciones

¡Y mucho más! ¡Veamos qué tipo de consultas locas podemos crear!

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


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


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 :).


| Reply |
Lionel-F avatar Lionel-F 11 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.


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