title: Copy Symfony entity from one connection to another / Store entity in session for live interaction
body: <p>I am working on a Symfony project. The project is about generating worksheets and gives users the features to edit all aspects of a worksheet. For example questions, text, variables and system is able to return a worksheet with all answers and explanations. The system is able to generate random worksheets.</p>
  
  <p>So I have created several entities in Symfony. Starting with a Wordsheet as the base. So a Worksheet has questions, questions have answers, variables (changeable by the user) and child questions, etc and answers have explanations.</p>
  
  <pre><code>Worksheet-&gt;Questions
      Question-&gt;Answers
      Question-&gt;ChildQuestions
      Question-&gt;Variables
          Answer-&gt;Explanation
  </code></pre>
  
  <p>That is the basic structure but there are several other deep relationships. Each worksheet comes with a service that processes it. And a basic default/prototype of the worksheet is stored in the database and loaded when the user visits the worksheet page. I am trying to create a system where I can mount a worksheet on a session so that the user can interact with it via Ajax/AngularJS/API.</p>
  
  <p><strong>My Solutions</strong></p>
  
  <p>The first solution I thought was store a worksheet entity into a session with all it's relationships. But this is almost impossible because of PHP deep copy issue when using clone. I also hate the idea of serializing the worksheet entity due to it's complexity.</p>
  
  <p>The second solution was to create a second Smyfony connection and simply clone the worksheet entity to the other connection/database with the session id. I have been successfully able to clone the entity with all it's relationship to the second connection/database but when I access the entity a second time all relationships mappings are lost although I see very identical data from the first database in the second database but all relationships have been lost. i.e when I use Worksheets->GetQuestions() I get an empty array although the data is there in the database.</p>
  
  <p>Here is my Symfony doctrine multiple connections setup;</p>
  
  <pre><code>doctrine:
      dbal:
          default_connection: default
          connections:
              default:
                  driver:   pdo_mysql
                  host:     '%database_host%'
                  port:     '%database_port%'
                  dbname:   '%database_name%'
                  user:     '%database_user%'
                  password: '%database_password%'
                  charset:  UTF8
                  mapping_types:
                    enum: string
              session:
                  driver:   pdo_mysql
                  host:     '%database_host2%'
                  port:     '%database_port2%'
                  dbname:   '%database_name2%'
                  user:     '%database_user2%'
                  password: '%database_password2%'
                  charset:  UTF8
  
      orm:
          default_entity_manager: session
          auto_generate_proxy_classes: "%kernel.debug%"
          entity_managers:
              default:
                  connection:     default
                  naming_strategy: doctrine.orm.naming_strategy.underscore
                  auto_mapping: true
                  mappings:
                      AppBundle: ~
                  filters:
                      softdeleteable:
                          class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                          enabled: true
              session:
                  connection: session
                  naming_strategy: doctrine.orm.naming_strategy.underscore
                  mappings:
                      AppBundle: ~
                  filters:
                      softdeleteable:
                          class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                          enabled: true
  </code></pre>
  
  <p>I have had lots of problems with this setup working through it. Although now the data is correctly saved to the second connection/database. For example I can't have auto_mapping: true for more than 2 connections, I think this would have solved my relationships problem. In symfony I use code like this to clone the entity to the second connection/database;</p>
  
  <pre><code>$WorksheetClone = $RendererService-&gt;GetProcessedCopy ();
  $WorksheetClone-&gt;setGUID($WorksheetClone-&gt;getGUID().'.'.$worksheet_session_key);
  $WorksheetClone-&gt;setName($WorksheetClone-&gt;getName().'.'.$worksheet_session_key);
  $WorksheetClone-&gt;setSessionReference($worksheet_session_key);
  
  $session_em = $this-&gt;getDoctrine()-&gt;getManager('session');
  $session_em-&gt;persist($WorksheetClone);
  $session_em-&gt;flush();
  </code></pre>
  
  <p>I am looking for a solution to save the worksheet somehow maybe through sessions or database so that the user can interact with it via API. I welcome any solutions or help with my solutions. Thanks.</p>