0

Before I used only one entity manager to interact with my database.

Everything worked well till I add 2 new entity manager (one entity manager to insert data and one entity manager to select data)

    -> I want to make a replication of my database later.

I think, I can solve the problem alone, but I just want to understand why this error appears :

    A new entity was found through the relationship 'AppBundle\Entity\Userinterest#user'
    that was not configured to cascade persist operations for entity: my_username. 
    To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity 
    or configure cascade persist this association in the mapping for example 
    @ManyToOne(..,cascade={"persist"})

I think, I understand the problem :) :

My User from FOSUserBundle is authenticate with the default entity manager.

    $userinteret->setUser($this->getUser());
    $em = $this->getDoctrine()->getManager();
    $em->persist($userinteret);
    $em->flush();

Then, I don't know how is stored the token_storage. But because I use the user from the token_storage, I think the probem come from there.

cretthie
  • 349
  • 1
  • 2
  • 11
  • Just a guess but you are probably pulling entities using one manager then trying to persist using the second one. Entity managers are self contained. They have no knowledge of what was done with other managers. So I suspect your approach will end badly. If you suspect something else is going on then update your question with a few lines showing how you are trying to persist the problem entity. – Cerad Oct 18 '17 at 17:12
  • Yep. The FOSUserBundle is loading the user with one entity manager while you are presumably trying to persist using the second one. There are various work around which you are probably trying now. – Cerad Oct 20 '17 at 11:23

1 Answers1

0

Try with :

    // All 3 return the "default" entity manager
    $em = $this->getDoctrine()->getManager();
    $em = $this->getDoctrine()->getManager('default');
    $em = $this->get('doctrine.orm.default_entity_manager');

    // Both of these return the "customer" entity manager
    $customerEm = $this->getDoctrine()->getManager('customer');
    $customerEm = $this->get('doctrine.orm.customer_entity_manager');

How to Work with multiple Entity Managers and Connections

mohamed jebri
  • 261
  • 3
  • 13
  • The question does not appear to be about how to access multiple entity managers but rather an error in persisting. Can you show how the code you posted would eliminate the error? – Cerad Oct 20 '17 at 11:24