Chapters
23 Chapters
|
2:22:24
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.4.1
Subscribe to download the code!Compatible PHP versions: ^7.4.1
-
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!
13.
DQL & The Query Builder
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.
This tutorial also works great for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.4.1",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^2.1", // 2.1.1
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.7", // 2.8.2
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"sentry/sentry-symfony": "^4.0", // 4.0.3
"stof/doctrine-extensions-bundle": "^1.4", // v1.5.0
"symfony/asset": "5.1.*", // v5.1.2
"symfony/console": "5.1.*", // v5.1.2
"symfony/dotenv": "5.1.*", // v5.1.2
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.1.*", // v5.1.2
"symfony/monolog-bundle": "^3.0", // v3.5.0
"symfony/stopwatch": "5.1.*", // v5.1.2
"symfony/twig-bundle": "5.1.*", // v5.1.2
"symfony/webpack-encore-bundle": "^1.7", // v1.8.0
"symfony/yaml": "5.1.*", // v5.1.2
"twig/extra-bundle": "^2.12|^3.0", // v3.0.4
"twig/twig": "^2.12|^3.0" // v3.0.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.1.*", // v5.1.2
"symfony/maker-bundle": "^1.15", // v1.23.0
"symfony/var-dumper": "5.1.*", // v5.1.2
"symfony/web-profiler-bundle": "5.1.*", // v5.1.2
"zenstruck/foundry": "^1.1" // v1.5.0
}
}
12 Comments
I have real problem with autocompleting syntaxes like here. It doesnt show me ->orderBy or anything else. Am i missing an option or something? I think I installed wverything right in previous courses but I may be wrong.
Another thing. Project doesn't see QuestionRepository.php as a part of project.
Hey HudyWeas,
> Project doesn't see QuestionRepository.php as a part of project
Hm, what do you mean? Please, make sure you have the correct namespace for that class first.
About advanced autcompletion - make sure you installed and enabled Symfony Plugin in PhpStorm for your project. Most of the cool autocompletion comes from there. If still not work - try to restart PhpStorm. Also, you can easily teach your PhpStorm what is object exactly is set to a variable with inlined "/** @var ClassName $varName" above the variable on which you don't have autorompleteion.
I hope this helps!
Cheers!
thats what php storm said to me when i wanted to edit this file. There opened a window asking me if i want to edit file that isnt a part of a project
Hey HudyWeas,
Oh, wait, you're probably talking about editing vendor files, you should not do this 😅 Better, create a new variable in *your* repository and add that var annotation - this should help :)
Cheers!
im not talking about files in vendor directory. Im talking about files created like Question.php and QuestionRepository. There is set namespace but still its not seen. When i set use statement manually in QuestionController.php
use App\Repository\QuestionRepository;it's yellow and says "Undefinied namespace Repository"
QuestionRepository.php
namespace App\Repository;Hey HudyWeas,
Ah, ok... Hm, something is not right probably. Fairly speaking, I've never seen this error myself. I'd recommend you to close your PhpStorm and then remove the project completely with running "rm -rf .idea/" command in the project folder. It will remove the PhpStorm's project config files. Then run the PhpStorm again and try to open a directory with your project, i.e. click on "File" -> "Open" -> and choose the folder with your project if you have Mac. This will open your Symfony project and creates a new PhpStorm project for it. If you have troubles with using PhpStorm - I'd recommend you to watch this free course about it: https://symfonycasts.com/sc...
I hope this helps!
Cheers!
I'm getting this error with the custom query. There is no parameter defined in the function. I've cleared the cache. Is this new behavior?
You need to pass a parameter to 'findByAllAskedOrderedByNewest'Hey @Aaron!
Ah, I think it's just a tiny typo in your method name :). You have an extra "By" in there - it should just be
findAllAskedOrderedByNewest. You're getting this weird error because the entity repository class has some "magic" in it: if you call an undefined method on it that starts with "findBy" it tries to parse it and create a query from it. So Doctrine basically tries to make a query to find by "orderedByNewest = the argument", which makes no sense of course :p. Without that magic, you would just get a "method not found error" and it would be easier to debug.Cheers!
LOL the number of times a typo has caused me grief is insane. Symfony is like 20 - 0 in the battle of who's the problem with me. Not enough coffee I guess.
haha, when I close my eyes I only see typos everywhere! :p
Hello!
First off all, your tutorials really helped me understand symfony and its surroundings.
But I have 1 simple problem I couldn't get fixed.
You are saying that I can ether use the Querybuilder to build my SQL statements or raw SQL statements and I would like to just write raw SQL statements. After looking at the linked tutorial regarding Raw SQL Queries Link I saw that he's not getting an Object returned from the Raw SQL. It just returns him an Array If I am correct.
So he's still using the QueryBuilder + raw SQL.
Is it possible to just use Raw SQL? if so could you give me the function with only raw SQL as an example? Thanks!
Keep doing tutorials, I hope there's a tutorial for Login etc. ;)
Hi Janik S.!
Yes, that is correct. You can absolutely write raw SQL queries. But what is returned from the database is a "raw array of data". And that's usually fine - because usually if you are choosing to write very complex queries, then returning an entity object doesn't make sense. For example, you might have a raw query that selects a AVG or SUM of some fields. It only makes sense to return that as an array (your return set doesn't have the same data as your entity object).
However, there are 2 ways to use raw SQL and get an objec:
1) Do it yourself! Suppose you're writing some complex SQL query that returns 3 columns - a SUM, AVG and count of something. If you would like to put this into an object, you could create a class (in any directory, this is not managed by Doctrine) that looks like this:
I'm using public properties for simplicity - you could also make them private with getters/setters. The point is, after making your SQL query (and getting an array back), you would create this object manually, set the data on it (from the array) and then return this object. This is a really nice solution.
2) Doctrine has a way to do a raw SQL query but then have it convert that to an object. I've not used it much, but it's called result set mapping https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/native-sql.html#examples
Let me know if that helps :).
Cheers!
"Houston: no signs of life"
Start the conversation!