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!
18.
Update Query & Rich vs Anemic Models
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
}
}
14 Comments
Hey Ryan!
How do you decide how much logic to place into an entity method vs making a new service, i.e. QuestionService and placing all of your voting etc into there?
I ask because I have worked on past projects where almost all the logic related to an entity was placed into the entity which eventually led to so much tight coupling it was hell to work with. As a result, I've always been a bit too scared to touch the Entity class with anything more than the basic getter and setter.
Hey Rob T.!
That's a really good question... and kind of tough to answer - it's a judgement call. But, at least in our company, we tend to put logic into a service more often than we tweak the entity. We would usually change the entity when we have some pure method whose only job is to manipulate the data on that entity (e.g. upVote() or getFullName() which concatenates first and last name). Normally it's kind of hard (at least in my experience) to put too much logic into you entity because your entity does not have access to any services.
For example, suppose that each time a Question gets an up vote, you need to send an email to someone. That logic cannot go into the entity... because the entity doesn't have access to the mailer service. You could pass it into a method - e.g.
public function upVote(MailerInterface $mailer)then pass the mailer in whenever you call that. DON'T do that :). You probably already know, but services should never go into your entities.Anyways, if you don't pass services into your entity, then your entity is pretty limited. In the above example, I would need to have some sort of QuestionVoteHelper with an
upVote(Question $question)method. That method would be responsible for incrementing the vote on the Question object AND sending the email. You could still choose to have anupVote()method on your entity. The QuestionVoteHelper would call it... and its job would simply be to increment the vote count (the helper service would still send the email).That answer got... lengthy 😆. Let me know if it helped!
Cheers!
Hi Ryan,
Why is it considered bad practice to add services to entities? Is it because methods of an entity should strictly be used/created to modify its own data and nothing more?
Hey Ccc123,
Yes, using services inside entities is a bad practice. And yes, that's because entities are just simple data objects.
If you need to do some heavy calculations inside and its logic is moved to a separate service - rewrite that service so that you pass the entity object to the service and it do all the necessary heavy calculation and set the final simple value on that entity instead of passing the service to the entity.
Cheers!
You say don't pass service vars into the entity, but thats exactly what some past devs had done to the code base I mentioned... *shudders*!!!
That's brilliant, thanks very much for taking the time to reply. Makes a lot of sense and has definitely helped :)
Ha! I was afraid of that! 😆I've seen it too
Anyways, I'm glad this was helpful!
Cheers!
Hello everyone,
Thank you very much for this tutorial.
I'm concerned about using docker in a Symfony 6.4 project. Is it possible to configure and manage two or more databases with docker? If so, how? How do you manage entities and queries? How do you make migrations work for each database?
Please help.
Hey @Diarill
I suppose you can have multiple databases using Docker. I unknown the details of how to properly do it but in this link explains how to set up multiple databases in the traditional way. I suppose you can start that way, and then try to replace the local databases with Docker. I think it's a matter of setting up the env vars properly
https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
Cheers!
The title of this video is "Rich vs Anemic Models", yet these terms aren't mentioned even once in the video. I know it can be Googled, but perhaps the video could be updated with a short description.
Hey Niclas H.!
Ah, good catch! Yes, we'll add a note to the video to help - thanks!
Cheers!
Hello, and thank you for this awesome tutorial,
I was wondering for this questionVote() controller method you present here : does flush send a db request only if the object changed ? It seems so.
But does flush take time to compare every objects in order to know if it needs to send request or not ? Does this flush() method consume some resources ? I'm thinking about what Ryan said about "someone who is messing with this route".. Is it better to put the flush() in the if and else statements ? Or to guard pattern the whole at beginning ?
Not criticizing the video, it's perfect; I was just wondering how to to this "the best way possible" during it :)
cheers !
Hey Nayte,
Yes, you'er right, the flush() will send the query to your DB only when the object is changed - that part is completely handled by Doctrine ORM, Doctrine track all the entities and if any have changes - the corresponding queries will be sent on flush() and only on flush() :)
And yes, it consumes some resources for this, but that's the price of this developer experience you have with Doctrine ORM. And it's not that much resources when you do thing right, so don't think too much about this. But if you're really curious, you can always use a profiler tool like Blackfire.io (we even have a course about it here: https://symfonycasts.com/sc... ) and check how heavy that part of code is in your application ;)
Cheers!
Do you guys have any tips of how to structure the directories for big projects? A website with many sub parts. For example: Games, Videos, Blog, Articles.
I want to keep everything organized. So I was thinking to put every Sub Category Controller in a different folder. For example:
src / Controller / Games / GamesController.php
src / Controller / Videos / VideosController.php
But if Symfony is automatically making files in src / Entities and src / Repository directories, then those 2 directories will be filled up with 50 or more .php files and will become messy. How do I keep everything in those directories organised?
Hey Farry7,
Yeah, you can orginize it as you want. Good idea about orginizing controllers, if you have several controllers related to "Games" - it makes sense to put them into src/Controller/Games/ folder. And same for other services, you may group them with separate folders.
About entities, take a look at "config/packages/doctrine.yaml" config file, in particular, "doctrine.orm.mappings". In theory you can put your entities in different folders, but you would need to configure paths to them.
About repositories - you don't care about them, you can put them in whatever folder you want too, but you will need to specify correct FQCNs in your entities.
I hope this helps!
Cheers!
"Houston: no signs of life"
Start the conversation!