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
Using Entity Repository in a doctrine event subscriber leads to INSERT on edit form #5606
Comments
So just by calling |
It produces another entity on edit form submit if I get the same repository as the entity being edited (in https://github.com/pdesgarets/sonata-bug-reproducer/blob/psql/src/EventListener/StudentListener.php#L24, calling But, yes, just getting the repository seems to confuse the entity fetching in CRUDController... |
Can you verify if the entity managers are the same object or if they are a different instance? |
Looking at https://github.com/doctrine/orm/blob/3cda3f1ae30657aceb3a2ec028ba8612863e2c70/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php#L29 if you have 2 entity managers, both would persist the same entity making it duplicate. |
Reason I mention it: symfony/symfony#30091 |
Yup, instances are different (but only one EM configured) |
Thanks @linaori and @kunicmarko20 , I guess I can close this issue |
Environment
Sonata packages
Symfony packages
PHP version
Subject
If I call
$entityManager->getRepository(MyEntity::class)
in a doctrine event subscriber constructor, submitting the "edit" form leads to a new entity being created instead of the existing one being updated.Please see this project reproducing the issue (in a virgin sonata project) :
https://github.com/pdesgarets/sonata-bug-reproducer/blob/psql/tests/EditUserTest.php
https://github.com/pdesgarets/sonata-bug-reproducer/blob/psql/src/EventListener/StudentListener.php
https://travis-ci.org/pdesgarets/sonata-bug-reproducer
Commenting https://github.com/pdesgarets/sonata-bug-reproducer/blob/psql/src/EventListener/StudentListener.php#L24 solves the issue.
The same issue occurs with MySQL instead of PostgreSQL.
The issue doesn't occur using the standard symfony CRUD (generated with
bin/console make:crud
).Steps to reproduce
Create a Sonata Project with at least one entity and one admin class attached to this entity.
Add a Doctrine Event Subscriber, even without any events attached. In its constructor, inject the EntityManagerInterface and call
getRepository
of the entity you're trying to edit.Go to the edit form, submit.
Expected results
The edited entity is updated, no new entity is created.
Actual results
A new entity is created, the edited entity remains untouched.
The text was updated successfully, but these errors were encountered: