// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^4.5", // v4.5.0
"doctrine/dbal": "^3", // 3.9.4
"doctrine/doctrine-bundle": "^2.13", // 2.13.2
"doctrine/doctrine-migrations-bundle": "^3.3", // 3.4.0
"doctrine/orm": "^3.3", // 3.3.1
"knplabs/knp-time-bundle": "^2.2", // v2.4.0
"pagerfanta/doctrine-orm-adapter": "^4.7", // v4.7.1
"php-cs-fixer/shim": "^3.46", // v3.65.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.6.0
"phpstan/phpdoc-parser": "^1.25", // 1.33.0
"stof/doctrine-extensions-bundle": "^1.12", // v1.13.0
"symfony/asset": "7.1.*", // v7.1.6
"symfony/asset-mapper": "7.1.*", // v7.1.9
"symfony/console": "7.1.*", // v7.1.8
"symfony/dotenv": "7.1.*", // v7.1.9
"symfony/flex": "^2", // v2.4.7
"symfony/framework-bundle": "7.1.*", // v7.1.6
"symfony/http-client": "7.1.*", // v7.1.9
"symfony/monolog-bundle": "^3.0", // v3.10.0
"symfony/property-access": "7.1.*", // v7.1.6
"symfony/property-info": "7.1.*", // v7.1.9
"symfony/runtime": "7.1.*", // v7.1.7
"symfony/serializer": "7.1.*", // v7.1.9
"symfony/stimulus-bundle": "^2.13", // v2.22.0
"symfony/twig-bundle": "7.1.*", // v7.1.6
"symfony/ux-turbo": "^2.13", // v2.22.0
"symfony/yaml": "7.1.*", // v7.1.6
"symfonycasts/tailwind-bundle": "^0.7.1", // v0.7.1
"twig/extra-bundle": "^2.12|^3.0", // v3.16.0
"twig/twig": "^2.12|^3.0" // v3.16.0
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.6", // 3.7.1
"symfony/debug-bundle": "7.1.*", // v7.1.6
"symfony/maker-bundle": "^1.52", // v1.61.0
"symfony/stopwatch": "7.1.*", // v7.1.6
"symfony/web-profiler-bundle": "7.1.*", // v7.1.9
"zenstruck/foundry": "^2.2" // v2.3.1
}
}
11 Comments
Hi,
Let's say we have two tables such that User and Lablist. And we created two factory for these tables.
How will we send data to these tables by using AppFixtures without affection both of them? If I add LablistFactory under UserFactory in below code, and if we load, all tables will be refreshed?
Hey @Mahmut-A,
Factory relations is what you're looking for. There are a couple ways to achieve but using your code above, it could be something like this:
I hope this helps!
--Kevin
Hi Kevin,
Maybe I explained wrong. There are two tables. one is user, second is lablist. I want to load their data to tables. As far as I understand, I can use only AppFixtures to load data. However, if I use UserFactory and LablistFactory in AppFixtures, then if I load by using fixtures:load command, both tables will be refreshed at each run.
I want to only load lablist, since user is alread loaded , I do not want to refresh it.
I want to add something. I created two Fixtures. AppFixtures and LablistFixtures by using command "symfony console make:fixtures LablistFixtures."
then, I am applying "php bin/console doctrine:fixtures:load --group=LablistFixtures" command to load data to lablist table. however, when I apply this, user table is being deleted.
if I use "php bin/console doctrine:fixtures:load --group=LablistFixtures --group=AppFixtures, all tables are refreshed and IDs are regenerated.
I want to only load to spesific tables not all of them.
Ok, I understand. You can have multiple fixture files: perhaps in your case, a
UserFixturesandLablistFixtures. By default, both will by loaded, but you can group them so they can be loadedindependently. Check the "Splitting Fixtures into Separate Files" documentation to see your options.
Hi,
Actually,splitting also refreshing. But I fixed my problem as below:
php bin/console doctrine:fixtures:load --group=LablistFixtures --appendwith this command, User tables is not refreshing.
However, we should be careful to not duplicate data in Lablist since we used --append. To prevent this, I added below lines in LabList.php class.
so now everything is perfect. thank you
`use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[UniqueEntity('labname')]
`
Glad to hear you have it working!
I think adjusting the purging behaviour could be used as an alternative to
--append.Hello,
I want to ask that how can I add new mail address to existing user table. As you see, there are mahmut.aydin mail adress and random 10 mail address. I want to add new one withouth deleting old data.
thank you
Hey Mahmut,
Then you did it right :) Did you try to reload your fixtures? It will create a mahmut.aydin@netsys.com.tr user and 10 more random users.
Or are you trying to add only that mahmut.aydin@netsys.com.tr without resetting your DB? Well, if so, that's a bit trickier. First of all, in theory, you should not care too much about the data locally, so you can just reload your fixtures that will do the trick eventually, but yeah, it will reset the DB so for example all your additional data you add to the DB after loaded the fixtures will gone. Alternative solution in your specific case - just register a new user with that email on the registration page if you have it 🤷♂️ That's the easiest way when you need to add just one more specific user. And finally, if you still need to do it with fixtures - run the same
bin/console doctrine:fixtures:loadcommand but with--appendoption:It will try to append new data w/o clearing the DB before. But it may cause some duplicated constraints fails, depends on how good your randomness is :) I would recommend you to comment out everything you don't need before running that command, e.g. comment out that
// UserFactory::createMany(10);.I hope that helps :)
Cheers!
thank you Victor. Actually, randomness is not important. I only wanted to learn how to add new data to existing table. I see need to use --append parameter to command.
thank you
Hey Mahmut,
Great! About randomness - it depends on your unique constraints in the DB. When you append data but if somehow during it the same email that already exists in your DB will be generated - the whole data load command will fail, that's just something you should keep in mind.
Cheers!
"Houston: no signs of life"
Start the conversation!