title: Filter records between dates with Doctrine2
body: <p>I've an application based on Symfony2. As Model I work with Doctrine2. I need to filter records by a certain range of dates. I've three entity:</p>
  
  <ul>
  <li>Food</li>
  <li>MealTag</li>
  <li>Meal</li>
  </ul>
  
  <p>I can record a meal. A meal has OneToMany relationship with MealTag. Food, has OneToMany relationship with MealTag. For each Meal, I can tag some foods. I can keep all food eaten by a user via this query:</p>
  
  <pre><code>class FoodRepository extends EntityRepository
  {
      public function findByQuantityEaten()
      {
          $query = $this-&gt;createQueryBuilder('Food');
          $query-&gt;select('Food.name, count(mt.food) as counted');
          $query-&gt;innerJoin('Food.mealTag', 'mt', 'WITH', 'Food.id = mt.food');
          $query-&gt;innerJoin('mt.meal', 'm', 'WITH', 'mt.meal = m.id');
          $query-&gt;groupBy('Food.name');
          $query-&gt;orderBy('counted', 'DESC');
          return $query-&gt;getQuery()-&gt;getResult();
      }
  }
  </code></pre>
  
  <p>I need to keep only food eaten during this week (last 7 days). I've a field (m.datetime) that corresponds at the datetime of the meal.</p>
  
  <p>How can I filter a datetime range with Doctrine2? This tentative returns</p>
  
  <blockquote>
    <p>[Syntax Error] line 0, col 211: Error: Expected Literal, got '"'</p>
  </blockquote>
  
  <pre><code>class FoodRepository extends EntityRepository
  {
      public function findByQuantityEaten()
      {
          $query = $this-&gt;createQueryBuilder('Food');
          $query-&gt;select('Food.name, count(mt.food) as counted');
          $query-&gt;innerJoin('Food.mealTag', 'mt', 'WITH', 'Food.id = mt.food');
          $query-&gt;innerJoin('mt.meal', 'm', 'WITH', 'mt.meal = m.id');
          $query-&gt;groupBy('Food.name');
          $query-&gt;orderBy('counted', 'DESC');
          $query-&gt;where('m.datetime &gt; "'. date('Y-m-d') .'"');
          return $query-&gt;getQuery()-&gt;getResult();
      }
  }
  </code></pre>
  
  <blockquote>
    <p>[1/2] QueryException: SELECT Food.name, count(mt.food) as counted, m.datetime FROM Sensorario\MagnaBundle\Entity\Food Food INNER JOIN Food.mealTag mt WITH Food.id = mt.food INNER JOIN mt.meal m WITH mt.meal = m.id WHERE m.datetime > "2014-10-26" GROUP BY Food.name ORDER BY counted DESC</p>
  </blockquote>