title: Doctrine 2 ORM DateTime field in identifier
body: <p>In our DB tables, we columns <code>refID</code> and <code>date</code> are a composite primary key, with one field of the identifier being mapped as a <code>datetime</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>class corpWalletJournal
  {
      /**
       * @ORM\Column(name="refID", type="bigint", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $refID;
  
      /**
       * @ORM\Column(name="date", type="datetime", nullable=false)     
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $date;
  
      public function setRefID($refID)
      {
          $this-&gt;refID = $refID;
      }
  
      public function setDate(\DateTime $date)
      {
          $this-&gt;date = $date;
      }
  }
  </code></pre>
  
  <p>If we describe them in entity as @ORM\Id this code will return exception "cannot convert datetime to string"...</p>
  
  <pre class="lang-php prettyprint-override"><code>$filter = array(
      'date' =&gt; $this-&gt;stringToDate($loopData['date']), 
      'refID' =&gt; $loopData['refID']
  ));
  
  $oCorpWJ = $this-&gt;em-&gt;getRepository('EveDataBundle:corpWalletJournal')-&gt;findOneBy($filter);
  // ...
  $oCorpWJ-&gt;setDate($this-&gt;stringToDate($loopData['date']));
  // ...
  </code></pre>
  
  <p>If we describe <code>corpWalletJournal#date</code> as a simple column, code works fine. Why? </p>
  
  <p>How can we deal with it? We need to have both <code>date</code> and <code>refID</code> in the primary key.</p>
  
  <p><strong>ADDED:</strong></p>
  
  <p>So i created new class</p>
  
  <pre><code>use \DateTime;
  
  class DateTimeEx extends DateTime
  {
  
  public function __toString()
  {
      return $this-&gt;format('Y-m-d h:i:s');
  }
  
  }
  </code></pre>
  
  <p>And new type for it</p>
  
  <pre><code>use Doctrine\DBAL\Types\Type;
  use Doctrine\DBAL\Platforms\AbstractPlatform;
  use Eve\DataBundle\Entity\Type\DateTimeEx;
  
  class DateTimeEx extends Type
  {
      const DateTimeEx = 'datetime_ex';
  
      public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
      {
          return 'my_datetime_ex';
      }
  
      public function convertToPHPValue($value, AbstractPlatform $platform)
      {
          return new DateTimeEx($value);
      }
  
      public function convertToDatabaseValue($value, AbstractPlatform $platform)
      {
          return $value-&gt;format('Y-m-d h:i:s');
      }
  
      public function getName()
      {
          return self::DateTimeEx;
      }
  
      public function canRequireSQLConversion()
      {
          return true;
      }
  
  }
  </code></pre>
  
  <p>How can i use them in entity?</p>
  
  <p>My (edited) Type Class</p>
  
  <pre><code>    use Doctrine\DBAL\Types\Type;
  use Doctrine\DBAL\Platforms\AbstractPlatform;
  
  class DateTimeEx extends Type
  {
      const DateTimeEx = 'datetime_ex';
  
      public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
      {
          return 'my_datetime_ex';
      }
  
      public function convertToPHPValue($value, AbstractPlatform $platform)
      {
          return $value;
      }
  
      public function convertToDatabaseValue($value, AbstractPlatform $platform)
      {
          return $value;
      }
  
      public function getName()
      {
          return self::DateTimeEx;
      }
  
  }
  </code></pre>