Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManyToMany-relation is only saved "one way" #4955

Closed
knallcharge opened this issue Jan 10, 2022 · 2 comments
Closed

ManyToMany-relation is only saved "one way" #4955

knallcharge opened this issue Jan 10, 2022 · 2 comments

Comments

@knallcharge
Copy link
Contributor

Describe the bug
I created two entities using a ManyToMany-relation and want to edit the relation from both of the entites using an AssocationField.
(e.g. User and Group, I want to be able to edit the User and assign it to many groups as well as edit the Group and assign many users to it)
What happens is that the relation is only saved "one way". I added the corresponding field to both CrudControllers, the field is shown on both edit-pages but will only save the relation when the entity is saved that has the "inversedBy"-part. The "mappedBy"-entity will not save the data.
I have tried to add "ORM\JoinTable" but that either result in an "table exists"-error or in two different relation-tables being created (user_group and group_user) which will save the relation but not making it visible to the other entity (so I end up having two ManyToMany-relations).

To Reproduce
Version used is 4.0.2, to reproduce create to entities:

#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'groups')]
private Collection $users;

#[ORM\ManyToMany(targetEntity: Group::class, mappedBy: 'users')]
private Collection $groups;

Then add the relation as an AssociationField in the CrudControllers:
yield AssociationField::new('users'); in the GroupCrudController and yield AssociationField::new('groups'); in the UserCrudController.

@trikess
Copy link
Contributor

trikess commented Jan 10, 2022

This more likely relates to Doctrine not EA, check this tutorial: https://symfonycasts.com/screencast/collections/saving-inverse-side-collection

@knallcharge
Copy link
Contributor Author

You are absolutely right, thanks a lot for pointing me in the right direction!

With symfony 5 the additions described in the symfonycast are already included by default, so the solution is to just add ->setFormTypeOption('by_reference', false) to the "mappedBy"-part of the relation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants