doctrine / data-fixtures

Doctrine2 ORM Data Fixtures Extensions
http://www.doctrine-project.org
MIT License
2.76k stars 224 forks source link

Separate Fixtures Class Throws many types of errors #394

Closed dougie84mo closed 2 years ago

dougie84mo commented 2 years ago

Versions: PHP 8.1 Worked in Symfony 5.3 finding behavior in Symfony 5.4. "doctrine/doctrine-fixtures-bundle": "^3.4" "doctrine/doctrine-bundle": "^2.2" "doctrine/orm": "^2.8"

General Problem: Multiple Fixture classes causes errors with references from other fixture classes

Expectations from old 5.3 Project: On the old project I am able to make tons of separate Fixtures classes and run them all with DependentFixturesInterface and use the references already created to then create the relations needed for the other fixtures. Example: I create Users first and then Teams, for each Team these is a ManyToOne $createdUser column that relates to a User that creates it. But I can make a UserFixtures class and safe the references (as seen in symfony casts etc.) then runs the TeamFixtures class to use the references in UserFixtures for TeamFixtures (all standard understanding)

Behavior in new 5.4 project: In the new project I am no way able to create multiple fixture classes for organization. In the same exact example above when I try to create the same relationship I get the follow error A new entity was found through the relationship 'App\Entity\Team#createdUser' that was not configured to cascade persist operations for entity: App\Entity\User@whateveruser. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\User#__toString()' to get a clue.

So then I listen to the exception and add cascade={"persist"} to the entity relation, run the fixtures again and I get the following error: Duplicate entry *** for key ****

The minute I put it in the original fixture I do not get this error. SAME exact functions persisting the objects, SAME exact references, SAME everything just a different outcome besides (see below):

ONLY DIFFERENCE between 5.3 project and 5.4 project: The only major difference I can see that is causing this problem is that in my old (5.3) project I had ALL Bidirectional variables built into the entity. In 5.4 I removed ALL/MOST of the Bidirectional relationships for my entities in theory to generate less queries.

Team Entity image

This is happening with every relation in every Fixture Class. I basically will only have one Fixture class that does everything at this point

TomHAnderson commented 2 years ago

Your error does not relate to the fact you're using fixtures. Instead, it is a problem that you have created an entity (User) then assigned it to Team through createdUser then tried to persist and flush. However, you never persisted the User entity.

greg0ire commented 2 years ago

Hi, we try to keep Github issues for bug reports and feature requests only. If you have a question, please ask it on Stack Overflow or in one of the channels mentioned at https://www.doctrine-project.org/community/index.html

You're welcome to open a bug report once you have clearly identified the offending project, and isolated the bug. Ideally, there should be a test case reproducing the issue, and it should pass on old versions of the package you think is buggy, but not on recent ones.

dougie84mo commented 2 years ago

I am persisting the user in both cases. Thanks I will take it to stack