Symfony2: comparing a stored datetime object with the current date with doctrine
<p>I want to compare a stored DateTime entry in my Database with the current date using Doctrine. </p>

<p>I have a list of Keys and their relation to a Person. My goal now is to create a list for available keys or not available keys. </p>

<p>For that I have to look at 2 Tables, key and personToKey. Where personToKey has a field rueckgabe(time to return the key) (dateTime) and a field key (stored id of the key) with an OneToMany Relation to the field id of the Table key.</p>

<p>Thanks to some search here, I got a running SQL Query which gives me the values I am looking for. 
SQL Query:</p>

<pre><code> SELECT * FROM key LEFT OUTER JOIN personToKey on key.id = personToKey.KeyId 
  WHERE personToKey.rueckgabe &lt; NOW()
</code></pre>

<p>My Problem is now, that I don't get this to work using doctrine. I had 2 approaches for this.</p>

<pre><code>  $qb = $em-&gt;createQueryBuilder();
  $query = $qb-&gt;select('k')
  -&gt;from('MyBundle:key', 'k')
  -&gt;leftjoin('MyBundle:personToKey', 'ptk','WITH', 's.id = ptk.key')
  -&gt;where('ptk.rueckgabe &lt; :date_now')
  -&gt;setParameter('date_now', date('Y-m-d H:i:m'))
  -&gt;getQuery();
  $test = $query-&gt;getResult();
</code></pre>

<p>And</p>

<pre><code>$sql = 'SELECT k FROM MyBundle:key k LEFT OUTER JOIN MyBundle:PersonToKey ptk WITH k.id = ptk.key WHERE ( ptk.rueckgabe &lt; CURRENT_DATE()) ';
$query = $em-&gt;createQuery($sql);
$test = $query-&gt;getResult();
</code></pre>

<p>Since the SQL Query works I would have thought that at least the second approach should have worked. But in both cases I get the following results:</p>

<p>for ptk.rueckgabe >= :date_now the list is fine, but for ptk.rueckgabe &lt; :date_now I get a mix of available and not available keys. Also the length of the result is below the number of rows the SQL Query finds in PHPMyAdmin.</p>

<p>Thanks in advance.</p>