title: How to manage collections through an API with symfony2 and forms
body: <p>I'm building an API where a user can update an entity with a collection as part of it. This works fine if I use forms throughout, but I'm building up the API. My entity looks like this:</p>
  
  <pre><code>&lt;?php
  class MyEntity {
      // ...
  
      /**
       * @ORM\OneToMany(targetEntity="TitleEntity", mappedBy="entityID", cascade={"persist"})
       */
      protected $myTitles;
  
      public function getMyTitles() {
          return $this-&gt;myTitles;
      }
  
      public function setMyTitles($titles) {
         foreach($titles as $key =&gt; $obj) { $obj-&gt;setEntity($this); }
         $this-&gt;myTitles = $collection;
      }
  
      public function addMyTitle($obj) {
          $obj-&gt;setEntity($this);
          $this-&gt;myTitles[] = $obj;
      }
  
      public function removeMyTitle($obj) {
          $this-&gt;myTitle-&gt;removeElement($obj);
      }
  }
  </code></pre>
  
  <p>The <code>myTitles</code> is an entity that has an ID, the ID of the entity it is attached to, and then a title.</p>
  
  <p>For the API, I'm passing a JSON content body back as a PUT request for the MyEntity object, so I end up with an array of the titles, and I'm prepping them like this to bind to a form for validation:</p>
  
  <pre><code>$myTitles = array();
  foreach($titles as $key =&gt; $title) {
      $titleObj = new TitleEntity();
      $titleObj-&gt;setTitle($title);
  }
  $myEntity-&gt;setTitles($titles);
  </code></pre>
  
  <p>but it complains with:</p>
  
  <pre><code>The form's view data is expected to be of type scalar, array or an instance of
  \ArrayAccess, but is an instance of class stdClass. You can avoid this error by 
  setting the &amp;quot;data_class&amp;quot; option to "stdClass" or by adding a view 
  transformer that transforms an instance of class stdClass to scalar, array or 
  an instance of \ArrayAccess
  </code></pre>
  
  <p>It looks like this happens because I call <code>getMyTitles()</code> before I bind my entity to the form I'm using to validate against. </p>
  
  <p>I'm binding to the form using an array:</p>
  
  <pre><code>$form = $this-&gt;createForm(new AddEntity(), $myEntity);
  $data = array( // Set all my data );
  $form-&gt;bind($data);
  if($form-&gt;isValid() {
  // ...
  </code></pre>
  
  <p>If I do the <code>createForm()</code> call first, and then add the titles afterward, I get this:</p>
  
  <pre><code>Call to a member function removeElement() on a non-object
  </code></pre>
  
  <p>which occurs inside <code>removeMyTitle()</code>.</p>
  
  <p>How do I handle this? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is the <code>AddEntity()</code> type:</p>
  
  <pre><code>&lt;?php
  class AddEntity extends AbstractType {
      public function buildForm(FormBuilderInterface $builder, array $options)
  {
  
      $builder
          -&gt;add('title', 'text')
          -&gt;add('subheading', 'text')
          -&gt;add('description', 'textarea')
          -&gt;add('myTitles', 'collection', array(
              'type' =&gt; new AddMyTitles(), // Basic type to allow setting the title for myTitle entities
              'allow_add' =&gt; true,
              'allow_delete' =&gt; true,
              'prototype' =&gt; true,
              'by_reference' =&gt; false,
              'options' =&gt; array(
                  'required' =&gt; false,
              ),
          ));
  }
  
  public function getName()
  {
      return 'addEntity';
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'My\TestBundle\Entity\Entity',
      ));
  }
  </code></pre>