title: Unable to persist because of uniqueness constraints for 1-n
body: <p>I let users persist <code>Brands</code> and <code>Cars</code> data in a single form instead of having two different forms however I'm getting an error message "<strong><em>The Brand already exist in database.</em></strong>" for those brands which already exist in <code>Brands</code> table. How it should work is: if the <strong>brand name</strong> already exists then don't try to persist it again because <code>name</code> field is marked as unique in database. How can I solve this issue?</p>
  
  <p><strong>Note:</strong> The reason why I have two extra form types is because I also have two different interfaces to collect brands and cars data individually.</p>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>public function createAction(Request $request)
  {
      if ($request-&gt;getMethod() != 'POST')
      {
          return new Response('Only POST method is allowed');
      }
  
      $form = return $this-&gt;createForm(new BothType(), null,
              array('action' =&gt; $this-&gt;generateUrl('bothCreate')));
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid())
      {
          $brandsData = $form-&gt;get('brands')-&gt;getData();
          $carsData = $form-&gt;get('cars')-&gt;getData();
  
          $brands = new Brands();
          $brands-&gt;setName($brandsData-&gt;getName());
  
          $cars = new Cars();
          $cars-&gt;setModel($carsData-&gt;getModel());
          $cars-&gt;setBrands($brands);
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($brands);
          $em-&gt;persist($cars);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('both'));
      }
  
      return $this-&gt;render('CarBrandBundle:Default:both.html.twig',
              array('page' =&gt; 'Both', 'form' =&gt; $form-&gt;createView()));
  }
  </code></pre>
  
  <p><strong>BOTH TYPE</strong></p>
  
  <pre><code>class BothType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;setMethod('POST')
              -&gt;setAction($options['action'])
              -&gt;add('brands', new BrandsType())
              -&gt;add('cars', new CarsType())
              -&gt;add('button', 'submit', array('label' =&gt; 'Add'))
              ;
      }
  } 
  </code></pre>
  
  <p><strong>BRANDS ENTITY</strong></p>
  
  <pre><code>/**
   * @ORM\Entity
   * @ORM\Table(name="brands", uniqueConstraints={@ORM\UniqueConstraint(columns={"name"})})
   * @UniqueEntity(fields="name", message="The Brand already exist in database.")
   * @package Car\BrandBundle\Entity
   */
  class Brands
  {
      protected $id;
      protected $name;
  
      /**
       * @ORM\OneToMany(targetEntity="Cars", mappedBy="brands")
       */
      protected $cars;
  
      public function __construct() { $this-&gt;cars = new ArrayCollection(); }
  
      public function getId() { return $this-&gt;id; }
      public function setName($name) { $this-&gt;name = $name; return $this; }
      public function getName() { return $this-&gt;name; }
  
      public function addCar(\Car\BrandBundle\Entity\Cars $cars)
      {
          $this-&gt;cars[] = $cars;
          return $this;
      }
  
      public function removeCar(\Car\BrandBundle\Entity\Cars $cars)
      {
          $this-&gt;cars-&gt;removeElement($cars);
      }
  
      public function getCars()
      {
          return $this-&gt;cars;
      }
  }
  </code></pre>
  
  <p><strong>BRANDS TYPE</strong></p>
  
  <pre><code>class BrandsType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;setAction($options['action'])
              -&gt;setMethod('POST')
              -&gt;add('name', 'text', array('label' =&gt; 'Name'))
              -&gt;add('button', 'submit', array('label' =&gt; 'Add'))
          ;
      }
  }
  </code></pre>
  
  <p><strong>CARS ENTITY</strong></p>
  
  <pre><code>class Cars
  {
      protected $id;
      protected $model;
  
      /**
       * @ORM\ManyToOne(targetEntity="Brands", inversedBy="cars")
       * @ORM\JoinColumn(name="brands_id", referencedColumnName="id", nullable=false)
       */
      protected $brands;
  
      public function getId() { return $this-&gt;id; }
      public function setModel($model) { $this-&gt;model = $model; return $this; }
      public function getModel() { return $this-&gt;model; }
  
      public function setBrands(\Car\BrandBundle\Entity\Brands $brands = null)
      {
          $this-&gt;brands = $brands;
          return $this;
      }
  
      public function getBrands()
      {
          return $this-&gt;brands;
      }
  }
  </code></pre>
  
  <p><strong>CARS TYPE</strong></p>
  
  <pre><code>class CarsType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;setAction($options['action'])
              -&gt;setMethod('POST')
              -&gt;add('model', 'text', array('label' =&gt; 'Model'))
              -&gt;add('price', 'text', array('label' =&gt; 'Price'))
              -&gt;add('button', 'submit', array('label' =&gt; 'Add'))
          ;
      }
  } 
  </code></pre>