Open Glideh opened 6 years ago
At first glance, I think this should totally be possible. There is a method on the Registry class called getManagerForClass()
that could be used:
Someone care to open a PR?
As a possible solution, while the issue is solved, you can use groups in fixtures:
Later, you can exec fixtures as:
$ php bin/console doctrine:fixtures:load --em em1 --group=em1 --no-interaction
$ php bin/console doctrine:fixtures:load --em em2 --group=em2 --no-interaction
This works for me on Symfony 4.2 :+1:
For now, groups seem to be the only way this can be done reliably. In fixture services, you could easily have the manager registry injected and then select the appropriate manager using the getManagerForClass
method that @weaverryan mentioned above. However, the fixtures will always be run in the transactional context of the entity manager provided to the command (or the default entity manager if none was provided), so you'll have to take care of transactional logic yourself.
I'm not sure what a good way to solve this would be, apart from what @migueabellan suggested above.
@alcaeus Don't we just change the default behavior so when an entity manager isn't specified we try to auto detect it for the objects?
I just ran into this problem and the fix is pretty gross. I expected it to auto detect when not specifying the entity manager.
The fixtures only receive a single entity manager. It is up to the fixture itself to ensure that the entity manager can actually handle the entity you want to create.
When using multiple entity managers, it would definitely be advantageous to pass in the entire manager registry. I'm not sure how we can accomplish that without a hard BC break, but it could be a way to get rid of the em
setting entirely.
I have multiple connections used by multiple entity managers with separate bundles (Symfony 3.4):
I have fixtures in both, but Doctrine seem to use only the default entity manager for both.
Is there a way to help Doctrine in our fixtures guessing which manager to use ? This would avoid to have to use this for each distinct entity manager.
Maybe there is some simpler way I'm not aware of.