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!
09.
Fetching Data & The Repository
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
}
}
22 Comments
For anyone interested in getting a custom 404 or other error page.... I wrote a blog post about it:
https://blog.syntaxseed.com...
Hey Sherri,
Thank you for sharing your knowledge with others :)
Cheers!
I follow the steps in this video. But it didn't auto complete methods in my code (01:45). What is the possibly reason for this?
Hey Ngan-Cat,
It depends if you have Symfony Plugin installed or no. In this tutorial Ryan has it installed and enabled, that's why he get that autocomplete. You can take a look at this video to know how to install and enable it: https://symfonycasts.com/screencast/phpstorm/setup#plugins
Otherwise, you can hint your IDE about what class is that object by using
@varannotation above the var, e.g.:I hope this helps!
Cheers!
How to know which library should I use? Like you used your "\App\Repository\QuestionRepository $repository" seems like out of nowhere..
Hey fUb,
Well, it depends on what repository you need. If you call "getRepository(Question::class)", then I know that the repository behind of that Question entity is QuestionRepository. If you're not sure - you can try to dump the $repository var, e.g. with "dd($repository)" - it will give you a hint about instance of what class is that var.
Cheers!
Hello everybody,
a happy new year to everybody. Especially the ones in the far east of europe.
Could anyone please tell me why this statements throws an error when executed by doctrine but not when directly executed by PHPMyAdmin - on the same server and the same database!
I also tried to insert a chr(10) OR \r\n right before the second Update. No chance either 8-(.
Thx
Oliver
Hey Oliver-W!
A very warm happy new year to you too - and I echo your sentiments ❤️.
Hmm. What method are you using to execute these 2 queries? This isn't something I've done before, but I'm sure it's "allowed", but it's possible that you need to use the DBAL directly (i.e. a lower level) to allow this.
Cheers!
Hey Ryan,
I am using
To make it work, I execute the query for every set of data. Before I concatenated the statements and made one query.
Cheers!
Hey Oliver-W!
Hmm. That's what I would have used too. I'm not sure why concatenating doesn't work. This looks technically possible, though "iffy" - like it's not something that you're really "supposed" to do. So, I would keep your solution of executing 1 query at a time.
Cheers!
Hey Ryan,
or I could use ...where id=1 OR id=2 and so on.
Sometimes live is really hard! Thx for your time and help
Oliver
Hey team SymfonyCasts, you guys are awesome and I have learned a lot from you guys and btw, happy new year.
Now I am trying to build some sort of form generator adhering to the Open/Closed principle. The form will definitely have new fields in the future. So with all these things in mind here is the path that I have taken:
Ok, now the problem I am facing at step 3 is that I came down to this line:
The error I get is: Class 'App\Entity'.$ffn.' does not exist.
Of course hard coding the above variable with entity name works. For example:
$entities[] = $this->em->getRepository('App\Entity\Continent'::class)->findBy(['isActive' => true);But I need to fetch all entities agnostically, meaning the code should not or cannot be aware of the name of the entities. Oh, and btw, if I use getRepositpry(Continent::class), not only I am hard coding, but also I would need to add a use statement on top of the page to inject this entity which I can simply avoid by using the exact path instead of the entity name.
There seems to be an scaping mechanism of some sort in the background which I do not know how to bypass. I tried different concatenations, none worked. Any suggestion or advice in general is very appreciated. Thank you!
Hey Ali_M!
A very happy new year to you too :). Now, let's see about your question!
Ok, I'm going to assume that the
$ffnvariable is set to the "short" class name of your entity - like the stringProduct. If that's the case, the code you need is:That should do it :). The
::classthing is something that you can call on a class to turn it into the full class name of that class. For example:So,
::classis just a convenience thing when you have ausestatement. But it's not required. For example, just using the full string is identical:So, you've basically got it right - there's just not need to add the
::class. Actually, the fact that'App\Entity\Continent'::classworks surprised me - but I guess PHP is super friendly. And so, in theory, your other solution may have worked with some tweak, but the::classsimply isn't needed.I hope this helps! Also, OCP is great, but, if you want some advice that you didn't ask for, be careful not to over-complicate things too early in a project ;). I typically put things into the database and make them dynamic only if I (or an admin) need the ability to change those values at runtime via an admin interface. For something like
Language, it depends on the purpose of your app, but in many cases, if I want to add aLanguage, if that required a code change, I'd be ok with that.Cheers!
Cheers!
Wonderful... It works just fine now, thanks to you... though I had my 20-minutes of head-scratching trying to get it to work. It was the backslash scaping effect during concatenation causing issues.. I invite you to look at my attempts leading to victory :)
`$c = "Continent"; //refers to an Entity class name
As to your valuable advices, you are correct. I do want to create an admin dashboard allowing to turn some of the form fields on & off. So that's why I considered adding them to the DB.
And that "Language" thing, it's not related to translation or locale or things like that. It's just another entity just like Continent, Country, etc. A mere list.
I am aware and actually used the Symfony form type country , however, in this project I need 100% control over my code and to allow the admin to tweak the form render to the fullest. i.e. Should the form show the Country field? Which countries should be hidden in the list? All of these to be controlled by an admin.
As to the OCP, It's in my todo list to watch your tutorial on SOLID soon enough ;)
Appreciate your help.
Hey Ali_M!
Nice job experimenting! Yes, the backslash makes perfect sense - that's an annoying character sometimes :). Good luck on the project - it sounds cool!
Cheers!
Hello,
I am trying to fetching data from repository and I found more solutions.
In this course, you are using
entityManagerInterfaceIn official symfony docummentation,
https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database is
managerRegistryusedAnd I found another one
objectManager...Could you explain me the differrence between each one? Are there any other options I haven't mentioned?
Cheers, Thomas
Hey Tomas,
Good qustion! Well, ManagerRegistry is something more global in Doctrine, it will allow you to get instance to the entity manager iteself, to other Doctrine features. You can look at that service to see what you can do / get with it. With the EntityManagerInterface you only get the part of Doctrine features, i.e. the actual entity manager only. So, if you don't need the whole Doctrine features and only need the entity manager to be able to persist() and flush() data for exmaple - better to inject EntityManagerInterface, as it
s more precise dependency. If you even don't need those persit() and flush() methods - perfect, you probably even don't need an entity manager at all. Maybe you just need an entity repository? Inject the repo you need, e.g. UserRepository in case you need to get access to User entity data. The more specific services you inject - the better. So, it really depends on your specific case.
About ObjectManager - that's too low-level thing usually, mostly used in case to make unit tests easier to set up because it's easier to mock up it.
In short, depending on what you really need to get in your methods - inject the specific services that give you specific features you need, don't need to inject the global objects that gives you literally everything while you're using only a tiny part of it. This will help you when you will be testing your code as well, it would be easier to mock up :)
I hope it clarifies thing for you a bit!
Cheers!
Hi.
How can I use the
`findBy([
]);`
to get the not nullable values.
isPaid is a DateTime field, is not boolean.
thank you.
Hey m3tal
You can pass a DateTime object as a parameter if the field is configured as a Datime or Date. In your case it may be better to write a custom query instead of using the generic `findBy()` method
Cheers!
Hi MolloKhan,
thanks for taking your time.
Finally I have opted for this road of creating a custom building because find method doesnt satisfy my needs.
thank you very much.
error message pleaaase =>Environment variable not found: "SENTRY_DSN".
Hey there,
It seems to me you forgot to define that environment variable, just add it to your
.envor.env.localfileCheers!
"Houston: no signs of life"
Start the conversation!