title: Not inserting duplicate items into MySQL DB - Doctrine and Symfony2
body: <p>I am persisting some data from an API into a MySQL db using Symfony2 and Doctrine.  I have an entity called Gig and I use annotations to set up the constraints. I think the relevant part of this looks like:</p>
  
  <pre><code>namespace London\HelloBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  /**
   * Gig
   * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(columns={"Artist", "ConcertDate"})})
   * @ORM\Entity
   */
  class Gig
  {
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string
   * @ORM\Column(name="Artist", type="string", length=255, nullable=true)
   */
  private $artist;
  
  /**
   * @var string
   * @ORM\Column(name="ConcertDate", type="string", length=255)
   */
  private $concertDate;
  </code></pre>
  
  <p>In my controller I use Buzz <a href="https://github.com/sensiolabs/SensioBuzzBundle" rel="nofollow">https://github.com/sensiolabs/SensioBuzzBundle</a> to go through the API and persist the bits I want to the DB.  This all works fine for the first import.  </p>
  
  <p>However I have set the combination of Artist and ConcertDate columns to be unique because I don't want to read the same items from the API into the database twice.  When I trigger the action that handles this however I get the error</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Plaid-2014-08-12' for key      'UNIQ_ED7D66426F593B176DAA24E'
  500 Internal Server Error - DBALException
  1 linked Exception: PDOException »
  </code></pre>
  
  <p>I think this is as it should be but what I really want is to persist the new data and skip over the data that I already have.  Can anyone help me with this?</p>
  
  <p><strong>EDIT:</strong>
  Adding the code that persists the data to db as per comment request.</p>
  
  <pre><code>namespace London\APIBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use London\HelloBundle\Entity\Gig;
  
  class DefaultController extends Controller
  {
    public function indexAction()
    {
      $buzz = $this-&gt;container-&gt;get('buzz');
      $response = $buzz-&gt;get('http://myapiuri');
      echo $buzz-&gt;getLastRequest(). "\n";
      $content = $response-&gt;getContent();
      $data = json_decode($content);
      $stuff=$data-&gt;resultsPage-&gt;results-&gt;event;
      for ($i=0; $i &lt; sizeof($stuff) ; $i++) {
          $gig = new Gig();
          if(isset($stuff[$i]-&gt;performance[0])){
              $gig-&gt;setArtist($stuff[$i]-&gt;performance[0]-&gt;displayName);
          }
          $gig-&gt;setConcertDate($stuff[$i]-&gt;start-&gt;date);
          $gig-&gt;setVenueName($stuff[$i]-&gt;venue-&gt;displayName);
          $gig-&gt;setGenre($stuff[$i]-&gt;type);
          $gig-&gt;setVenueAddress('45 Queen Caroline Street, London, England W6 9QH');
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($gig);
          $em-&gt;flush();   
  
      }
      return $this-&gt;render('LondonAPIBundle:Default:index.html.twig');
    }
  }
  </code></pre>