Chapters
27 Chapters
|
2:45:18
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
11.
Collection Criteria for Custom Relation Queries
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.3", // v3.3.0
"composer/package-versions-deprecated": "^1.11", // 1.11.99.3
"doctrine/doctrine-bundle": "^2.1", // 2.4.2
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
"doctrine/orm": "^2.7", // 2.9.5
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.1
"pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
"pagerfanta/twig": "^3.3", // v3.3.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
"symfony/asset": "5.3.*", // v5.3.4
"symfony/console": "5.3.*", // v5.3.7
"symfony/dotenv": "5.3.*", // v5.3.7
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.3.*", // v5.3.7
"symfony/monolog-bundle": "^3.0", // v3.7.0
"symfony/runtime": "5.3.*", // v5.3.4
"symfony/stopwatch": "5.3.*", // v5.3.4
"symfony/twig-bundle": "5.3.*", // v5.3.4
"symfony/validator": "5.3.*", // v5.3.14
"symfony/webpack-encore-bundle": "^1.7", // v1.12.0
"symfony/yaml": "5.3.*", // v5.3.6
"twig/extra-bundle": "^2.12|^3.0", // v3.3.1
"twig/string-extra": "^3.3", // v3.3.1
"twig/twig": "^2.12|^3.0" // v3.3.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.3.*", // v5.3.4
"symfony/maker-bundle": "^1.15", // v1.33.0
"symfony/var-dumper": "5.3.*", // v5.3.7
"symfony/web-profiler-bundle": "5.3.*", // v5.3.5
"zenstruck/foundry": "^1.1" // v1.13.1
}
}
13 Comments
wow.. its been a while since I've deep dived into the latest symfony stuff. I'm starting a new role soon so I'm going through all your amazing tutorials on Symfony 5 and doctrine etc to get back up to date.
criteria is an amazing feature.. symfony always blows my mind.
thank you Ryan and team :)
Welcome back to the Symfony world, you'll be amazed with all the new features!
Wait, I'm actually more impressed with that
$this->answers->matching()magic! So, if the lazy relation was not fetched yet, this will automatically apply the criteria to the database query itself? This is actually amazing.For anyone else reading (and future myself): here's the lesson where Ryan talks about it: https://symfonycasts.com/screencast/doctrine-queries/criteria
Hey Andrey,
Yep, exactly! :) This feature might be really interesting and powerful in some cases.
Cheers!
Holly smoke. I'm so glad I decided to watch this "old" Symfony 5 serie. Either I forgot about this criteria thing or had never heard of it! But this is a game changer!
#awesomeness
Hello Ryan,
Thanks for showing us this feature, it's amazing to know that there is multiple ways to customize queries, but I am little bit confused : which one to choose to get Data from DB : Criteria Or QueryBuilder ? Is one option better than the other?
I noticed that it wasn't possible to use Repository in our Entity like we did in the controller, but I want to know if there are other things that will let us to choose one of the both options.
Thanks again
Hey Hanane,
Definitely the QuestionBuilder, it's more flexible and I'd recommend you to always start with it, and in some cases when it doesn't work for you - you may fallback to Criteria. So, when use one or another - it depends on specific case, but as you already guessed yourself - Criteria mostly used in entities to customize (filter) the collection, because as you said you can't use QueryBuilder in entities - it's not recommended as entities are simple data and injecting any services into it is a bad practice.
I hope this helps!
Cheers!
In what classes also we should not inject services?
Honestly, I was a little bit confused when Ryan said that we can't use repository in an entity.
And I was going to ask you why, because I thought we can autowire services in ANY class inside src folder.
But then I saw your comment where you say that it's just a bad practice.
Hey!
Well, it depends. on your application, but mostly you have controllers, entities and... services in your app. Actually, almost everything in your app is a service, and you can inject services into other services. Unfortunately, data objects like your entities are not services, and so it's not correct to inject other services into entities. The idea is that you operate data in your controllers/services. So, if you think about it - I bet you will realise that you don't need to inject anything in entities, because most probably you're using those data in controllers, where you can inject a repository class and execute any query you want to fetch the data you need. So, controllers is the place where you get some data and pass them to template where those data will be rendered. I.e. in your project you operate data using controllers and services, something like this.
I hope this helps!
Cheers!
I kind of understand you, but I have a question.
As Ryan always say WORK = SERVICE.
What if we need some work, when we try to get some entity field?
For example, in Question entity we have getVotesString() method, which contains logic for processing votes field (adds + or - sign).
But what if I need more complex logic to process that field and return in some other format. Could it require some WORK?
Hey Phy,
Great!
If you need to do some work - create a service for this. If we're talking about entities - they already have repositories, so you can pass some simple logic there. Otherwise, create a standalone service and use, i.e. pass an entity to that service that need to do some work with their data. The service will do the complex work, e.g. any heavy calculations you need, and then set the final value to the entity.
But if we're talking about some simple actions, like incrementing the value that does not need any third-party services - you can do this in the entity, i.e. create a special method for this, e.g.:
So, it depends on the complexity of the business logic you need. I hope this clarify things for you!
Cheers!
Cool, thank you, Victor!
"Houston: no signs of life"
Start the conversation!