title: Symfony 2 related entities in 1 form
body: <p>I have 2 entities, <code>topic</code> and <code>post</code>. <code>topic</code> contains the name of the topic. <code>post</code> contains the text and the topic id.
  I want to implement a function into my project that allows users to create a new topic and write a post for their topic.</p>
  
  <p>The form should look like this:</p>
  
  <ol>
  <li>Name of the topic [input type=text] (property of topic entity)</li>
  <li>text [textarea] (property of post entity)</li>
  </ol>
  
  <p>Now I need help building the form. Only using the TopicType gives me the result I want, just without the input field to write a post. I tried adding</p>
  
  <pre><code>$builder-&gt;add('post', 'entity', array('class' =&gt; 'MyTestBundle:Post', 'property' =&gt; 'text'));
  </code></pre>
  
  <p>to my PostType but that creates a dropdown menu of the previous posts.</p>
  
  <p>How can I create a form that contains all the input fields of 2 related entites?</p>
  
  <p>Edit:</p>
  
  <p><code>Topic.orm.yml</code></p>
  
  <pre><code>My\TestBundle\Entity\Topic:
  type: entity
  table: topics
  id:
      id:
          type: integer
          generator: { strategy: AUTO }
  
  
  oneToMany:
      posts:
          targetEntity: Post
          mappedBy: topic
  
  fields:
      name:
          type: string
          length: 50
  </code></pre>
  
  <p><code>Post.orm.yml</code></p>
  
  <pre><code>My\TestBundle\Entity\Post:
  type: entity
  table: posts
  id:
      id:
          type: integer
          generator: { strategy: AUTO }
  
  manyToOne:
      topic:
          targetEntity: Topic
          inversedBy: posts
          joinColumn:
              name: topic_id
              referencedColumnName: id
  
  
  fields:
      text:
           type: text
      datetime:
           type: datetime
  </code></pre>
  
  <p>Rendering the form worked with <code>$builder-&gt;add('posts', new PostType());</code> but when I submit the form i get this error:</p>
  
  <pre><code>Warning: spl_object_hash() expects parameter 1 to be object, string given in C:\projects\symtests\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1375
  </code></pre>
  
  <p>How exactly do I have to handle the form and persist the submitted data to my database?</p>
  
  <p>Unfortunately I deleted the code that should persist the data to the database, but it looked something like this, and it returns the same error.</p>
  
  <pre><code>public function persistAction()
  {
      $topic= new Topic();
      $post = new Post();
  
      $topic-&gt;setCreateDate(new \Datetime('now'));
      // set all properties for $topic that you can't set in the form
  
      $post-&gt;setTopic($thread);
      $post-&gt;setDatetime(new \Datetime('now'));
      // set all properties for $psot that you can't set in the form
  
      $request = $this-&gt;getRequest();
  
      $form = $this-&gt;createForm(new ThreadType(), $thread);
      $form-&gt;bind($request);
  
      if($form-&gt;isValid())
      {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
          $em-&gt;persist($topic);
          $em-&gt;persist($post);
          $em-&gt;flush();
  
          return new Response('success');
      }
      else
      {
          throw new \Exception('form invalid');
      }
  </code></pre>
  
  <p>Maybe I should try this: <a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow">How to Embed a Collection of Forms</a></p>