Chapters
22 Chapters
|
2:28:56
|
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!
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!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.7", // v3.7.0
"doctrine/doctrine-bundle": "^2.7", // 2.7.0
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.12", // 2.12.3
"knplabs/knp-time-bundle": "^1.18", // v1.19.0
"pagerfanta/doctrine-orm-adapter": "^3.6", // v3.6.1
"pagerfanta/twig": "^3.6", // v3.6.1
"sensio/framework-extra-bundle": "^6.2", // v6.2.6
"stof/doctrine-extensions-bundle": "^1.7", // v1.7.0
"symfony/asset": "6.1.*", // v6.1.0
"symfony/console": "6.1.*", // v6.1.2
"symfony/dotenv": "6.1.*", // v6.1.0
"symfony/flex": "^2", // v2.4.5
"symfony/framework-bundle": "6.1.*", // v6.1.2
"symfony/http-client": "6.1.*", // v6.1.2
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/proxy-manager-bridge": "6.1.*", // v6.1.0
"symfony/runtime": "6.4.3", // v6.4.3
"symfony/twig-bundle": "6.1.*", // v6.1.1
"symfony/ux-turbo": "^2.0", // v2.3.0
"symfony/webpack-encore-bundle": "^1.13", // v1.15.1
"symfony/yaml": "6.1.*", // v6.1.2
"twig/extra-bundle": "^2.12|^3.0", // v3.4.0
"twig/twig": "^2.12|^3.0" // v3.4.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
"symfony/debug-bundle": "6.1.*", // v6.1.0
"symfony/maker-bundle": "^1.41", // v1.44.0
"symfony/stopwatch": "6.1.*", // v6.1.0
"symfony/web-profiler-bundle": "6.1.*", // v6.1.2
"zenstruck/foundry": "^1.21" // v1.21.0
}
}
19 Comments
Why are the non-nullable properties (e.g. title) type-hinted as possibly having a null value and initialised as null?
Hey Michail!
GREAT question. That was a design decision in MakerBundle and the goal was to be "friendly" over "strict" (definitely a subjective decision). So, if make bundle instead generate things like
private string $title, then if the user called$obj->getTitle()before setting it, they would get the "not initialized" error from PHP. So, to make the entities behave identically to the "pre property types world", we made them nullable and default to null. It's very possible that, as a community, we will eventually desire to be more strict... or we might even add an option in MakerBundle. But, that's the explanation :).Cheers!
Hello everybody!
Can you please help me to find out why my entities aren't type hinted when I run the command
bin/console make:entity?I got:
Instead of:
Maker Bundle version: "symfony/maker-bundle": "1.43"
Thanks in advance!
Hey @RafaelBraga
That's interesting. Perhaps it's due to your PHP or Symfony version. What versions do you have? Try upgrading them
Cheers!
Hi @MolloKhan
I'm using PHP 8.2.10 and Symfony 6.1.12. I will upgrade Symfony and try again.
Thank you!
Ok, typed properties are a feature of PHP 7.4, so that's not the problem. It could be related to your Symfony/MakerBundle version. Let me know if it works after upgrading
Cheers!
How to start over again when there's a mistake (e.g. wrong field name/type) in the created entity?
Hey Patrick,
If you made a typo - you need to do Ctrl + C (or Cmd + C on a Mac) to exit the command and re-run it again :) It might be so that command already wrote some code to your files, so you need to revert those changes and remove created fields if they contain your mistake and you don't want to fix it manually in those files. That's why it's a good idea to commit all the changes before running make commands, as you can easily revert uncommit changes with
git.Cheers!
Yeah I religiously use git and did the same thing actually. I was just wondering if what I'm doing is too hacky and if there's a "Symfony Way" of doing it. Thanks for confirming! Cheers~
Can you point me to tutorial about setup of symfony to use yaml format instead of php for the entity classes.
Hey BYT,
We always use PHP attributes (or annotations on old tutorials) for configuring entities, so I can't point you to any tutorial but you can see examples in the Symfony docs. For example, you can see how to configure an association here (just click on the YAML option) https://symfony.com/doc/current/doctrine/associations.html#mapping-the-manytoone-relationship
And you may want to read how to configure your entities when using YAML https://symfony.com/doc/current/reference/configuration/doctrine.html#custom-mapping-entities-in-a-bundle
Cheers!
Creating tables and columns one by one will take very long. I have hundreds of tables and thousands of columns listed in Excel. I can export table/columns/relationship info to a CSV or any format. Is there a way to generate all of the Entity classes from this existing file? I'm not talking about Down from an existing database because the exiting database has a different way. I can list schema in files, a file or any format.
Hey BYT,
I don't think so... unless you will find a bundle or a library on GitHub that may help you with this. Doctrine only allows you to import mapping information from an existing database - for this you can leverage
doctrine:mapping:importcommand.If it's not something you need - I think you can create your own Symfony command and read the CSV file to import all the data from that, i.e. create entity files based on the schema in that file.
Cheers!
Hi
I want to be sure that I understand this right. I want to start new project, and I already have the database, with about 200 tables in it.
If I run "symfony console doctrine:mapping:import" command, an entity for each table will be created for me? Is my assumption correct?
Thanks.
Hey @t5810 ,
Yes, that's how it suppose to be used, it creates entities with the corresponding mapping. But on practice, it depends on how well you have your legacy DB configured. But you can easily try it and see how it works for you ;)
Cheers!
Hello. I want to create a CRM system in which there will be users with their own sets of Task entities.
It is very convenient when the ID of each entity is represented by an integer and the numbers for each individual user go sequentially and inextricably.
In simple words, you need an ID with auto-increment, then separately for each user. But how to do it right? It should be fast and have good scalability.
Hey Dmitriy,
But Doctrine already does this for you. You just need to generate an entity via MakerBundle, or make sure your User entity has this mapping config:
Every time you will create a new entity - it will generate a sequential and inextricable ID for it. But if you delete a user from the DB - it won't be sequential anymore I suppose, and nothing much you can do with it.
I hope this helps!
Cheers!
Hi, is there a way to create the entity in a specific folder? (e.g src/CustomFolder/Entity/...)
Hey Wei,
Unfortunately, no. The Maker bundle follows Symfony's best practice when all your entities are in the
src/Entity/folder. This is also done for the simplicity of this command. The easiest way would be to generate an entity in that folder and then move it to another folder if needed.Well, technically, you can put your entity in a subfolder of the
src/Entity/with the following syntax:But if you're looking for a way to create it outside of the default
src/Entity/- nope, you would need to move it manually there.I hope this helps!
Cheers!
"Houston: no signs of life"
Start the conversation!