title: Symfony2 Forms Adding New Entities on the fly
body: <p>I have 3 entities. <code>Customers</code>, <code>Products</code>, and <code>Purchases</code>.</p>
  
  <p>New purchase forms should be able to be added by clicking an Add Another Purchase button. Every Purchase is a separate row in DB.</p>
  
  <p>I have tried using a collection which fills/adds necessary number of forms for existing purchases for a customer but I'm not sure how to have a button to add additional purchase forms. Also, when adding a new customer, no purchase forms show.</p>
  
  <p>I've been stuck on this for days!! PLEASE HELP</p>
  
  <p>What is the best way to create a form like the following:</p>
  
  <h1>Add/Edit New Customer<br /></h1>
  
  <h3>Customer Information</h3>
  
  <p>First Name<br />
  Last Name<br />
  Email<br /></p>
  
  <h3>Purchases [Add Another Purchase Button]</h3>
  
  <ol>
  <li>Select Product (Dropdown retrieved from Products entity by User service) - Enter Quantity<br /></li>
  <li>Select Product (Dropdown retrieved from Products entity by User service) - Enter Quantity<br /></li>
  <li>Select Product (Dropdown retrieved from Products entity by User service) - Enter Quantity</li>
  </ol>
  
  <p>[Add Another Purchase Button]
  <br /><br /><br />
  CustomersType Class</p>
  
  <pre><code>class CustomersType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('first_name')
              -&gt;add('last_name')
              -&gt;add('email')
              -&gt;add('purchases', 'collection', array(
                  'type'   =&gt; new PurchasesType(),
                  'allow_add' =&gt; true,
                  ))
          ;
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'My\Bundle\MyBundle\Entity\Customers',
          ));
      }
  
      public function getName()
      {
          return 'customers';
      }
  }
  </code></pre>
  
  <p><br />
  PurchasesType Class</p>
  
  <pre><code>class PurchasesType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('products', 'entity', array(
                  'class'         =&gt; 'My\Bundle\MyBundle\Entity\Products',
                  'query_builder' =&gt; function ($repository) { return $repository-&gt;createQueryBuilder('p')-&gt;orderBy('p.name', 'ASC'); }
              ))
              -&gt;add('quantity')
          ;
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'My\Bundle\MyBundle\Entity\Purchases',
          ));
      }
  
      public function getName()
      {
          return 'purchases';
      }
  }
  </code></pre>
  
  <p><br />
  Customers Class</p>
  
  <pre><code>class Customers
  {
      /** 
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /** 
       * @ORM\Column(type="string")
       */
      private $first_name;
  
      /** 
       * @ORM\Column(type="string")
       */
      private $last_name;
  
      /** 
       * @ORM\Column(type="string")
       */
      private $email;
  
      /** 
       * @ORM\ManyToOne(targetEntity="Users", inversedBy="customers")
       * @ORM\JoinColumn(name="users", referencedColumnName="id")
       */
      private $users;
  
      /** 
       * @ORM\OneToMany(targetEntity="Purchases", mappedBy="customers")
       */
      private $purchases;
  }
  </code></pre>
  
  <p><br />
  Products entity</p>
  
  <pre><code>class Products
  {
      /** 
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /** 
       * @ORM\Column(type="string")
       */
      private $name;
  
      /**
       * @ORM\Column(type="decimal", precision=10, scale=2)
       */
      private $price = 0.0;
  
      /** 
       * @ORM\OneToMany(targetEntity="Purchases", mappedBy="products")
       */
      private $purchases;
  
      /** 
       * @ORM\ManyToOne(targetEntity="Users", inversedBy="products")
       * @ORM\JoinColumn(name="users", referencedColumnName="id")
       */
      private $users;
  }
  </code></pre>
  
  <p><br />
  Purchases Entity</p>
  
  <pre><code>class Purchases
  {
      /** 
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(type="decimal", precision=10, scale=2)
       */
      private $price;
  
      /**
       * @ORM\Column(type="integer")
       */
      private $quantity;
  
      /** 
       * @ORM\ManyToOne(targetEntity="Products", inversedBy="purchases")
       * @ORM\JoinColumn(name="products", referencedColumnName="id")
       */
      private $products;
  
      /**
       * @ORM\ManyToOne(targetEntity="Customers", inversedBy="purchases")
       * @ORM\JoinColumn(name="fans", referencedColumnName="id")
       */
      private $customers;
  }
  </code></pre>