The course is built on Symfony 4, but the principles still apply perfectly to Symfony 5 - not a lot has changed in the world of relations!
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.7.2
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.1.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.0.4
"symfony/console": "^4.0", // v4.0.14
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.0.14
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.4
"symfony/web-server-bundle": "^4.0", // v4.0.4
"symfony/yaml": "^4.0", // v4.0.14
"twig/extensions": "^1.5" // v1.5.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"fzaninotto/faker": "^1.7", // v1.7.1
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.4
"symfony/dotenv": "^4.0", // v4.0.14
"symfony/maker-bundle": "^1.0", // v1.4.0
"symfony/monolog-bundle": "^3.0", // v3.1.2
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.4
"symfony/stopwatch": "^3.3|^4.0", // v4.0.4
"symfony/var-dumper": "^3.3|^4.0", // v4.0.4
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.4
}
}
24 Comments
When I run "php bin/console doctrine:fixtures:load", I get:
In ReferenceRepository.php line 141:
Reference to: (App\DataFixtures\Article_0) does not exist
However, doing some tiny debugging, I found out that if I change this line:
$comment->setArticle($this->getReference(Article::class.'_0'));
to:
$comment->setArticle($this->getReference('App\Entity\Article_0'));
It totally works. Am I missing something?
Hey Daniel,
Yeah, you missed the namespace, add "use App\Entity\Article;" line in the beginning of the src/DataFixtures/CommentFixture.php file.
Cheers!
I added the namespace but still receive the same error as Daniel (also Daniels solution doesn't work for me).
However, I renamed my "Article" class to "Drops" (as "Drop" wasn't allowed with doctrine), so might this be an order-issue (CommentFixture being executed first while no related Drops exist yet)?
Update: I got the answer from the next tutorial and it was as I expected the order of the names :)
Though I managed, I cant see how to make the thing random, did anyone managed to randomize that _0 so it loops through the entity related, and get random ids?
got it. :D
Hey JLChafardet,
Haha, well done! :)
Cheers!
I'm facing a different hurdle though now,
I'm playing with an idea thats just for the heck of it but, nontheless, I'm the kind that likes to even make my time-spenders to work properly.
I do write (amateurish at best) fiction work, so I decided to write a small app, that will allow me to store fictions.
so I have for now 3 tables in my db, 3 entities, Author, Novel, Chapter.
One Author can have written many novels, One novel can have many chapters. Fairly straightforward.
the complication comes here in "Chapters", at least when it comes to fixtures. as every chapter has a "number" (Chapter 1, 2, 3, 4 etc), so 3 novels, can have a chapter 1, or a chapter 300.
that "number" has to be incremental but not automatically. every new record, it should "get the last inserted value for "number" and increment it by one.
do you got an idea on how to do this through fixtures? by form is somewhat simpler, as all I need is to bring that information when the form is rendered, but in fixtures (to test) is a whole different story.
Hey JLChafardet,
I think I got it. So, it should be pretty straightforward with fixtures I think. Just iterate over all Novels and in a loop create as many chapters as you want. Like for every new Novel you will have a new loop that will start from 1 so all chapters will start from 1 to some number, you can use faker to generate a random number that will be the last chapter. Does it sound good for you? :)
Cheers!
Ctrl + Opt + O
removes unused 'use' statements
but I prefer
Cmd + Opt + L
as it also auto-formats the code along the way
just FYI
Nice tip!
Hello,
I just dont understand how 'references' from the function 'create many' are stored. What is the entity which is actually owning them, i cant find any parameter call reference where they could be stored.
Hey Akavir S.!
Let me try to give some hints... but let me know if it still doesn't make sense ;)
Check out the code block here - https://symfonycasts.com/screencast/doctrine-relations/awesome-random-fixtures#codeblock-00ae1a5ad6 - you can click to expand the entire code block to see the entire file. The key is this line inside
createMany()``$this->addReference($className . '_' . $i, $entity);
Yeah it helped a lot !
Thanks you for taking the time to answer me !
You guys are doing amazing job
I wish you prosperity <3
Hello,
I've found this tutorial very useful, as always. However I have a question.
My entity
Commenthas several properties. TheParentTypeproperty could be aReviewor aFAQ. How would you handle the fixtures so you create 50Commentfixtures withParentTypeset toReviewand 50Commentfixtures withParentTypeset toFAQ? When I try to have 2 different Fixture Classes, likeCommentWithReviewParentFixturesandCommentWithFaqParentFixturesone overwrites the other, same if I try to modify your code to have 2 callable factories in it.Is the comment record being overwritten or what's been overwritten? If the problem is the reference, then you could move the logic for creating references into a protected method and in your Fixture class you just overwrite it
Cheers!
Getting a similar error below which carries over to the next chapter:
Reference to: (App\Entity\Post_0) does not exist
Note: I changed "Article" to "Post" in the very beginning of the first tutorial for reasons unrelated to this discussion.
Hey Aaron
Did you update the file name as well?
Cheers!
All files were created by hand while following along, I just always replace "Post" in place of "Article" to avoid a name mismatch. I don't think that's it. Just validated there were no name mismatches anywhere.
Great! I can see in your other comments that you could fix your problem
Cheers & happy learnings!
Hey Aaron!
Hmm. So, a few things to check. Even after you finish the next chapter, I would double check that your fixtures are being loaded in order. Specifically, we're looking to see if the PostFixture is loaded before you see this error (you should be able to determine this by the console output). That would be possible reason #1 for the error: that the CommentFixture is running before PostFixture has a chance to add in the fixture references.
Thing #2 to check is that the references are being added correctly in PostFixture. The fancy createMany() should handle this, but something might going wrong. To check, at the end of the PostFixture::loadData() method, try:
If everything is working properly, that should return something. If there is a big error on that line (you can see the exact line of an error by adding
-vvvat the end of the fixtures command) then we know the problem is somewhere in the PostFixture.Cheers!
Yes, this was the solution for anyone else that runs into a similar issue:
Change your BaseFixture declaration to this:
abstract class BaseFixture extends Fixture implements OrderedFixtureInterfaceThen add this function to each fixture class:
`public function getOrder()
Actually -- it looks like there's a better way to solve this in the next chapter, but you can use this in the interim if you're following along and just want to make this work for the moment.
Looks like maybe use "implements OrderedFixtureInterface" for BaseFixture and then some special sauce somewhere or another?
Definitely looks like Comment is loading before Post. I presume there's some fancy footwork to get Post to load before Comment.
> purging database
> loading App\DataFixtures\AppFixtures
> loading App\DataFixtures\CommentFixture
BTW -- these training courses are awesome and I REALLY wish I knew about them before I spent an embarrassing amount of time trying to figure out some very basic things.
"Houston: no signs of life"
Start the conversation!