title: Symfony 4 - Two entities in one form using doctrine, one entity has to insert before the other
body: <p>I can't get the textarea to show up inside my form when combining two entities. No javascript please, I've seen the javascript method with collections.</p>
  
  <p>The logic is, have a user add a title on a form, and then add text in a textarea. When user submits, a thread gets inputted using the title, and thread id is used to add a post to it (the textarea content).</p>
  
  <p>Form example :</p>
  
  <p><a href="https://i.stack.imgur.com/AvA51.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AvA51.jpg" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>I believe this is enough to output it, if it's setup correctly via the 2 "type" classes.</p>
  
  <pre><code>{{ form_start(forum_thread_form) }}
     {{ form_widget(forum_thread_form) }}
  {{ form_end(forum_thread_form) }}
  </code></pre>
  
  <hr>
  
  <p>Code : </p>
  
  <p>Entities</p>
  
  <p><strong><em>Thread.php</em></strong></p>
  
  <pre><code>     /**
       * @ORM\Id
       * @ORM\GeneratedValue
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\ManyToOne(targetEntity="App\Entity\Account", inversedBy="forum_posts")
       * @ORM\JoinColumn(nullable=false)
       */
      private $account;
  
      /**
       * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="forum_threads")
       * @ORM\JoinColumn(nullable=false)
       */
      private $category;
  
      /**
       * @ORM\Column(type="string", length=128, nullable=true)
       * @Assert\NotBlank()
       */
      private $title;
  
      /**
       * @ORM\OneToMany(targetEntity="App\Entity\Post", mappedBy="thread")
       */
      private $posts;
  
      public function __construct()
      {
          $this-&gt;posts = new ArrayCollection();
      }
  
      ## getters and setters.. some ommitted ##
          /**
  
       /*
       * @return mixed
       */
      public function getPosts()
      {
          return $this-&gt;posts;
      }
  
      /**
       * @param mixed $posts
       */
      public function setPosts($posts): void
      {
          $this-&gt;posts = $posts;
      }
  </code></pre>
  
  <p><strong><em>Post.php</em></strong></p>
  
  <pre><code>    /**
       * @ORM\Id
       * @ORM\GeneratedValue
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\ManyToOne(targetEntity="App\Entity\Account", inversedBy="forum_posts")
       * @ORM\JoinColumn(nullable=false)
       */
      private $account;
  
      /**
       * @ORM\ManyToOne(targetEntity="App\Entity\Thread", inversedBy="posts")
       * @ORM\JoinColumn(nullable=true)
       */
      private $thread;
  
      /**
       * @ORM\Column(type="text")
       * @Assert\NotBlank()
       */
      private $content;
  
   ## GETTERS AND SETTERS, some ommitted ##
   /**
   * @return mixed
   */
  public function getThread()
  {
      return $this-&gt;thread;
  }
  
  /**
   * @param mixed $thread
   */
  public function setThread($thread): void
  {
      $this-&gt;thread = $thread;
  }
  
  /**
   * @return mixed
   */
  public function getContent()
  {
      return $this-&gt;content;
  }
  
  /**
   * @param mixed $content
   */
  public function setContent($content): void
  {
      $this-&gt;content = $content;
  }
  </code></pre>
  
  <p><strong><em>PostType.php</em></strong></p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('content', TextareaType::class)
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; Post::class,
          ));
      }
  </code></pre>
  
  <p><strong><em>ThreadType.php</em></strong></p>
  
  <pre><code>public function __construct(){
          $this-&gt;posts = new ArrayCollection();
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('title', TextType::class);
  
          $builder-&gt;add('content', CollectionType::class, array(
              'entry_type' =&gt; PostType::class,
              'entry_options' =&gt; array('label' =&gt; false),
          ));
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; Thread::class,
          ));
      }
  </code></pre>
  
  <p><strong><em>ForumController.php</em></strong></p>
  
  <pre><code>$forum_thread = new Thread();
  $forum_thread_form = $this-&gt;createForm(ThreadType::class,$forum_thread);
  
          $forum_thread_form-&gt;handleRequest($request);
  
          if ($forum_thread_form-&gt;isSubmitted() &amp;&amp; $forum_thread_form-&gt;isValid()) {
              $forum_thread = $forum_thread_form-&gt;getData();
  
              $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
              $entityManager-&gt;persist($forum_thread);
              $entityManager-&gt;flush();
  
          }
  
          $forum_thread_form = $forum_thread_form-&gt;createView();
  
          return $this-&gt;render('forum/threads.html.twig',compact('forum_thread_form'));
  </code></pre>