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

Using Entity Repository in a doctrine event subscriber leads to INSERT on edit form #5606

Closed
pdesgarets opened this issue Jun 25, 2019 · 7 comments

Comments

@pdesgarets
Copy link
Contributor

pdesgarets commented Jun 25, 2019

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.50.0 3.50.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0 3.15.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.17.0 3.17.0 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0  1.3.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0  3.9.0  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library

Symfony packages

$ composer show --latest 'symfony/*'
symfony/asset                      v4.3.1  v4.3.1  Symfony Asset Component
symfony/browser-kit                v4.3.1  v4.3.1  Symfony BrowserKit Component
symfony/cache                      v4.3.1  v4.3.1  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v1.1.5  v1.1.5  Generic abstractions related to caching
symfony/config                     v4.3.1  v4.3.1  Symfony Config Component
symfony/console                    v4.3.1  v4.3.1  Symfony Console Component
symfony/css-selector               v4.3.1  v4.3.1  Symfony CssSelector Component
symfony/debug                      v4.3.1  v4.3.1  Symfony Debug Component
symfony/debug-bundle               v4.3.1  v4.3.1  Symfony DebugBundle
symfony/debug-pack                 v1.0.7  v1.0.7  A debug pack for Symfony projects
symfony/dependency-injection       v4.3.1  v4.3.1  Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.3.1  v4.3.1  Symfony Doctrine Bridge
symfony/dom-crawler                v4.3.1  v4.3.1  Symfony DomCrawler Component
symfony/dotenv                     v4.3.1  v4.3.1  Registers environment variables from a .env file
symfony/event-dispatcher           v4.3.1  v4.3.1  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.5  v1.1.5  Generic abstractions related to dispatching event
symfony/expression-language        v4.3.1  v4.3.1  Symfony ExpressionLanguage Component
symfony/filesystem                 v4.3.1  v4.3.1  Symfony Filesystem Component
symfony/finder                     v4.3.1  v4.3.1  Symfony Finder Component
symfony/flex                       v1.2.7  v1.2.7  Composer plugin for Symfony
symfony/form                       v4.3.1  v4.3.1  Symfony Form Component
symfony/framework-bundle           v4.3.1  v4.3.1  Symfony FrameworkBundle
symfony/http-client                v4.3.1  v4.3.1  Symfony HttpClient component
symfony/http-client-contracts      v1.1.5  v1.1.5  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.3.1  v4.3.1  Symfony HttpFoundation Component
symfony/http-kernel                v4.3.1  v4.3.1  Symfony HttpKernel Component
symfony/inflector                  v4.3.1  v4.3.1  Symfony Inflector Component
symfony/intl                       v4.3.1  v4.3.1  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle               v1.11.6 v1.11.6 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can for...
symfony/mime                       v4.3.1  v4.3.1  A library to manipulate MIME messages
symfony/monolog-bridge             v4.3.1  v4.3.1  Symfony Monolog Bridge
symfony/monolog-bundle             v3.4.0  v3.4.0  Symfony MonologBundle
symfony/options-resolver           v4.3.1  v4.3.1  Symfony OptionsResolver Component
symfony/orm-pack                   v1.0.6  v1.0.6  A pack for the Doctrine ORM
symfony/phpunit-bridge             v4.3.1  v4.3.1  Symfony PHPUnit Bridge
symfony/polyfill-intl-icu          v1.11.0 v1.11.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.11.0 v1.11.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring          v1.11.0 v1.11.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/process                    v4.3.1  v4.3.1  Symfony Process Component
symfony/profiler-pack              v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access            v4.3.1  v4.3.1  Symfony PropertyAccess Component
symfony/property-info              v4.3.1  v4.3.1  Symfony Property Info Component
symfony/routing                    v4.3.1  v4.3.1  Symfony Routing Component
symfony/security-acl               v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.3.1  v4.3.1  Symfony SecurityBundle
symfony/security-core              v4.3.1  v4.3.1  Symfony Security Component - Core Library
symfony/security-csrf              v4.3.1  v4.3.1  Symfony Security Component - CSRF Library
symfony/security-guard             v4.3.1  v4.3.1  Symfony Security Component - Guard
symfony/security-http              v4.3.1  v4.3.1  Symfony Security Component - HTTP Integration
symfony/serializer                 v4.3.1  v4.3.1  Symfony Serializer Component
symfony/serializer-pack            v1.0.2  v1.0.2  A pack for the Symfony serializer
symfony/service-contracts          v1.1.5  v1.1.5  Generic abstractions related to writing services
symfony/stopwatch                  v4.3.1  v4.3.1  Symfony Stopwatch Component
symfony/swiftmailer-bundle         v3.2.8  v3.2.8  Symfony SwiftmailerBundle
symfony/templating                 v4.3.1  v4.3.1  Symfony Templating Component
symfony/test-pack                  v1.0.6  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.3.1  v4.3.1  Symfony Translation Component
symfony/translation-contracts      v1.1.5  v1.1.5  Generic abstractions related to translation
symfony/twig-bridge                v4.3.1  v4.3.1  Symfony Twig Bridge
symfony/twig-bundle                v4.3.1  v4.3.1  Symfony TwigBundle
symfony/validator                  v4.3.1  v4.3.1  Symfony Validator Component
symfony/var-dumper                 v4.3.1  v4.3.1  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.3.1  v4.3.1  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link                   v4.3.1  v4.3.1  Symfony WebLink Component
symfony/web-profiler-bundle        v4.3.1  v4.3.1  Symfony WebProfilerBundle
symfony/web-server-bundle          v4.3.1  v4.3.1  Symfony WebServerBundle
symfony/yaml                       v4.3.1  v4.3.1  Symfony Yaml Component

PHP version

$ php -v
PHP 7.3.3 (cli) (built: Mar 30 2019 08:43:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.3, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.3, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.7.1, Copyright (c) 2002-2019, by Derick Rethans

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.

@kunicmarko20
Copy link
Contributor

So just by calling ->getRepository() it produces another entity? Crazy

@pdesgarets
Copy link
Contributor Author

pdesgarets commented Jun 25, 2019

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 $entityManager->getRepository(Student::class); makes the test fail, but $entityManager->getRepository(AnotherEntity::class); would be alright).

But, yes, just getting the repository seems to confuse the entity fetching in CRUDController...

@linaori
Copy link

linaori commented Jun 25, 2019

Can you verify if the entity managers are the same object or if they are a different instance?

@kunicmarko20
Copy link
Contributor

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.

@linaori
Copy link

linaori commented Jun 25, 2019

Reason I mention it: symfony/symfony#30091

@pdesgarets
Copy link
Contributor Author

Yup, instances are different (but only one EM configured)

@pdesgarets
Copy link
Contributor Author

Thanks @linaori and @kunicmarko20 , I guess I can close this issue

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

No branches or pull requests

3 participants