title: Symfony forms: how to include in the Doctrine entity form a form for a Doctrine embeddable used by the entity itself
body: <p>I have a Doctrine <code>Entity</code> that uses a <a href="http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/embeddables.html" rel="nofollow noreferrer">Doctrine <code>embeddable</code></a>.</p>
  
  <p>I want to create a form to edit the <code>Entity</code> and the <code>Embeddable</code>.</p>
  
  <p>My <code>Entity</code>:</p>
  
  <pre><code>/**
   * MyEntity
   *
   * @ORM\Table(name="my_entities")
   * @ORM\Entity(repositoryClass="\AppBundle\Entity\MyEntityRepository")
   * @ORM\HasLifecycleCallbacks
   */
  class MyEntity
  {
      ...
  
      /**
       * @var MyEmbeddableInfo
       *
       * @ORM\Embedded(class="AppBundle\Entity\Embeddable\MyInfoEmbeddable")
       */
      private $myInfo;
  
      ...
  </code></pre>
  
  <p>My <code>Embeddable</code>:</p>
  
  <pre><code>/**
   * @ORM\Embeddable
   */
  class MyInfoEmbeddable
  {
      /**
       * @var string
       *
       * @ORM\Column(name="info1", type="string", nullable=true)
       *
       * @Assert\NotBlank(groups={"setUp"})
       */
      private $info1;
  
      /**
       * @var string
       *
       * @ORM\Column(name="info2", type="string", nullable=true)
       *
       * @Assert\NotBlank(groups={"setUp"})
       */
      private $info2;
  
      /**
       * @var
       *
       * @ORM\Column(name="info3", type="string", nullable=true)
       *
       * @Assert\NotBlank(groups={"setUp"})
       */
      private $info3;
  
      ...
  </code></pre>
  
  <p><strong>WHAT I'VE TRIED</strong></p>
  
  <p>I found this <a href="https://stackoverflow.com/questions/24228392/doctrine-embeddables-not-working-with-symfony-forms">question here on Stackoverflow</a> and it seems the user had my same problem, but the difference is that I never get that <code>Warning</code> as I simply don't understand the basic procedure to create a form for the embeddable object.</p>
  
  <p>I make two tests: one passing directly the embeddable object to the <code>MyEntityType</code> form builder and one creating a <code>MyInfoType()</code> form type.</p>
  
  <p><strong>TEST 1: PASS DIRECTLY THE EMBEDDABLE OBJECT</strong></p>
  
  <p>This is the code of my <code>MyEntityType</code>class:</p>
  
  <pre><code>class MyEntityType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              ...
              -&gt;add('myInfo', 'entity', ['class' =&gt; '\AppBundle\Entity\Embeddable\MyInfoEmbeddable'])
              ...
      }
  
      public function getName()
      {
          return 'MyEntity';
      }
  }
  </code></pre>
  
  <p>This solutions throws a <code>RuntimeException</code>:</p>
  
  <blockquote>
    <p>Class <code>\AppBundle\Entity\Embeddable\MyInfoEmbeddable</code> seems not to be
    a managed Doctrine entity. Did you forget to map it?</p>
  </blockquote>
  
  <p><strong>TEST 2: PASSING THE <code>MyInfoType</code> OBJECT</strong></p>
  
  <p>The second test I make is using a form type (as suggested in the <a href="https://stackoverflow.com/a/24231517/1399706">linked SO question</a>):</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              ...
              -&gt;add('myInfo', new MyInfoType())
              ...
      }
  </code></pre>
  
  <p>And this is the code of <code>MyInfoType</code>:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('infoField1', 'text', ['required' =&gt; false])
          -&gt;add('infoField2', 'text', ['required' =&gt; false])
          -&gt;add('infoField3', 'choice', [
                  'choices'   =&gt; [
                      '' =&gt; '',
                      'choice1' =&gt; 'Choice 1',
                      'choice2' =&gt; 'Choice 2',
                      'choice3' =&gt; 'Choice 3'
                  ],
                  'required' =&gt; false
              ]
          );
  }
  </code></pre>
  
  <p>This, instead, returns a <code>Symfony\Component\PropertyAccess\Exception\NoSuchIndexException</code>:</p>
  
  <blockquote>
    <p>Cannot read index <code>infoField1</code> from object of type
    <code>AppBundle\Entity\Embeddable\MyInfoEmbeddable</code> because it doesn't
    implement <code>\ArrayAccess</code>.</p>
  </blockquote>
  
  <p>So, this solution doesn't work, too.</p>
  
  <p>My goal is to create a form for <code>MyEntity</code> and in this form create also the fields to update <code>MyInfoEmbeddable</code>.</p>
  
  <p>Which is the correct procedure to follow?</p>