I have second connection and entity manager configured for logging only. It works fine in my logging service, but there is a problem with:
$user = $this->security->getUser();
$log->setUser($user);
When trying this I’ve got “A new entity was found through the relationship” exception. And it’s ok, because second entity manager doesn’t know about User and is trying to cascade persist object (which is not configured). In previous version of Symfony (3.x I guess) I used this workaround (from my previous version of code):
$user = $token->getUser();
$userManaged = $em->getRepository('AppBundle:User')->find($user->getId());
$log->setUser($userManaged);
It worked, but now (Symfony 6.0.7) it no longer works. I’m trying now:
$user = $this->security->getUser();
if($user){
$userManaged = $em->getRepository('App:User')->find($user->getId());
$log->setUser($userManaged);
}
And still having “A new entity was found through the relationship”.
How should I do this in proper way? Here is my doctrine.yml ORM section:
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App: ~
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'AppEntity'
alias: App
log:
connection: log
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'AppEntity'
alias: App