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!
05.
Foundry: Always Pass a Factory Instance to a Relation
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.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
}
}
12 Comments
Hello,
Since I went back to this course, I had a problem with the database and Docker.
After running: "docker-compose up -d" and running the first command line of the video "symfony console doctrine:fixtures:load",
I had this error:
Then I deleted the container and followed the procedure of the previous course to install a new container (...down -> ...up -d -> mysql -u root ...). But, in addition: I had something strange: when I asked: "docker-compose ps", I hadn't the same table!
When I do this course 2 weeks ago, and asked "docker-compose ps" I had the columns: Name | Command | State (set to "up") | Ports (set to "0.0.0.0:50153->3306/tcp, 33060/tcp")
Whereas now, I have : NAME | COMMAND | SERVICE (set to "database") | STATUS ( set to "running") | PORTS (set to "33060/tcp, 0.0.0.0:49363->3306/tcp)
Thus, I think the problem comes from Docker or a missing command for docker but I don't know what to do (I don't touch "env." or ".env.local" files & I already clear the cache)
Thanks
Hey Tristan N.!
Hmm. This might not be a big deal. With Mysql & Docker, iirc, I think Docker might not create the actual "database" for you. And so you can create it via
symfony console doctrine:database:create. The fact that you see "Unknown database" means that it is successfully connected to MySQL.Hmm. I think there are 2 things happening here:
1) The different "headers" on the table suggest that you may have just upgraded to a newer version of Doctrine with new headers. When I run
docker-compose ps, I see the same new columns that you see (e.g. NAME | COMMAND | SERVICE, etc).2) The exposed port was 50153 before and it's now 49363. That's expected: each time you restart Docker, it will bind port 3306 of the container to a different, random port on your host machine. But that's ok: the symfony binary automatically reads the random port and uses it when it adds the DATABASE_URL environment variable.
Let me know if this helps!
Cheers!
Yes, it works! Thank you, Ryan ^^
How do I create a relation where its the same class? For example I have a parent/child object where if it has an ID in a parent_id field then it needs to fetch the parent object as well - I want to be able to create random parents but not for all
Hi. Tell me please, Ho to do if we need some kind of "fixed" dictionary/table like Country, City or Statuses - data which never changes?
Hey Ruslan,
It depends on, if you store those data in a special table or use scalar values for them. You could create a special table where you will store the statuses for example, and then you will do exactly what we do in this screencast, i.e. make a DB relation between a specific status and e.g. an Order entity. But usually such things as statuses we save as scalar values, i.e. you create a new "varchar" column for Order entity called "status" and set a scalar value to it, e.g. "paid". And all possible statuses we write as constants on an entity. Then, you will be able to create a method that will return all possible statuses, e.g. getSupportedStatuses():
Then, in the factory, you can leverage faker to get a random element of the array e.g. " $faker->randomElement(Order:: getSupportedStatuses());"
Or you can just hardcode the specific status by default. Or create a special method on the factory that will help you to set a specific status on the entity that you will pass as an argument.
I hope this helps!
Cheers!
Thank you for clarification. For "scalar" is clear.
But for still unclear , if I need Country (table of all countries) - it's fixed dictionary.
How will you do in that situation? Will you fill data in migration or write Fixture ?
Hey Ruslan,
Ah, ok! Well, migrations and fixtures are totally different things :) Migrations are basically used to "migrate" DB changes on production without losing real data. If you want to pre-fill your local database with some dummy data so it don't be just an empty DB - fixtures are exactly what you need.
But regarding your question, in the AppFixtures where you create all your fixtures fetch a real Country entity object/objects you need from the DB via entity manager and set them, something like we do in this course:
Something like this. I hope this helps!
Cheers!
Let's say I have an Image entity related to Post entity. Is there a way to fill the db table image with fake file paths and generate images on my public/uploads or public/images?
Hey Abdeljabar T.
What if you upload a set of images into your public directory and then, you just have a list of their paths in your fixtures that you will assign randomly to your Image objects?
Cheers!
Missed global goal of this lesson - Foundry just help to set database with fake data?
Hey Mepcuk!
Ha, fair enough :). The goal of the previous chapter is probably "Having fake database data is great, and Foundry make it easy, and here is how you use Foundry with Doctrine relations". This chapter really just focuses on covering one edge-case when people use Foundry with relations (that's this part here: https://symfonycasts.com/sc... ).
Cheers!
"Houston: no signs of life"
Start the conversation!