title: Is there a way to enforce a unique column using doctrine2?
body: <p>I know I can always set a unique DB key using MYSQL schema however was just curious if ORM's like doctrine allowed you to set a column to be unique in code?</p>
  
  <p>For example how can I make it in code so that username's are unique in code at run time?</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
      `email` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
      `password` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
      PRIMARY KEY (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
  </code></pre>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  function insert_user($username,$email,$password) 
  {
      $user = new User();
      $user-&gt;setUsername($username); //HOW CAN I MAKE THIS UNIQUE IN CODE?
      $user-&gt;setEmail($email);
      $user-&gt;setPassword($password);
  
      try {
          //save to database
          $this-&gt;em-&gt;persist($user);
          $this-&gt;em-&gt;flush();
      }
      catch(Exception $err) {
          die($err-&gt;getMessage());
          return false;
      }
  
      return true;
  }
  </code></pre>