我是基于Symfony2的应用程序。作为模型我使用Doctrine2。我需要按一定的日期范围过滤记录。我有三个实体:
我可以记录一顿饭。一顿饭与MealTag有OneToMany关系。食物,与MealTag有OneToMany关系。对于每顿饭,我可以标记一些食物。我可以通过此查询保留用户食用的所有食物:
class FoodRepository extends EntityRepository
{
public function findByQuantityEaten()
{
$query = $this->createQueryBuilder('Food');
$query->select('Food.name, count(mt.food) as counted');
$query->innerJoin('Food.mealTag', 'mt', 'WITH', 'Food.id = mt.food');
$query->innerJoin('mt.meal', 'm', 'WITH', 'mt.meal = m.id');
$query->groupBy('Food.name');
$query->orderBy('counted', 'DESC');
return $query->getQuery()->getResult();
}
}
我需要在本周(过去7天)内仅保留食物。我在用餐的日期时间对应一个字段(m.datetime)。
如何使用Doctrine2过滤日期时间范围?这个暂定的回报
[语法错误]第0行,第211栏:错误:预期文字,得到'"'
class FoodRepository extends EntityRepository
{
public function findByQuantityEaten()
{
$query = $this->createQueryBuilder('Food');
$query->select('Food.name, count(mt.food) as counted');
$query->innerJoin('Food.mealTag', 'mt', 'WITH', 'Food.id = mt.food');
$query->innerJoin('mt.meal', 'm', 'WITH', 'mt.meal = m.id');
$query->groupBy('Food.name');
$query->orderBy('counted', 'DESC');
$query->where('m.datetime > "'. date('Y-m-d') .'"');
return $query->getQuery()->getResult();
}
}
[1/2] QueryException:SELECT Food.name,count(mt.food)as count,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计算DESC
答案 0 :(得分:1)
变化:
$query->where('m.datetime > "'. date('Y-m-d') .'"');
要:
$query->where('m.datetime > :date');
$query->setParameter('date', new \DateTime());