我想用Doctrine的Query Builder编写这个语句:
WHERE e.date LIKE '%$month'
(我希望所选日期以变量$ month的值结束)
e.date是日期类型,其值如下:'05 / 05/2013'
$ month变量将是一个字符串,其值如下:'05 / 2013'
所以我想用我的查询
获取05/2013个月的所有项目这是我的全部功能:
public function findForMonthAndCat($user, $month, $category)
{
$q = $this
->createQueryBuilder('e')
->where('e.date LIKE :month') //I don't know how to write this line
->andWhere('e.user = :user')
->andWhere('e.category = :category')
->setParameter('date', $month)
->setParameter('user', $user)
->setParameter('category', $category)
->getQuery();
return $q->getResult();
}
我试过了:
->where($q->expr()->like('e.category', '%:month')
但它说我在非对象上调用expr()。
我试过了:
public function findExpensesForMonthAndCat($user, $month, $category)
{
$q = $this
->createQueryBuilder('e')
->where('e.date LIKE :month')
->andWhere('e.user = :user')
->andWhere('e.category = :category')
->setParameter('date', '%'.$month)
->setParameter('user', $user)
->setParameter('category', $category)
->getQuery();
return $q->getResult();
}
但它说
Invalid parameter: token date is not defined in the query.
答案 0 :(得分:1)
将日期替换为'月',这是参数的名称:
public function findForMonthAndCat($user, $month, $category)
{
$q = $this
->createQueryBuilder('e')
->where('e.date LIKE :month')
->andWhere('e.user = :user')
->andWhere('e.category = :category')
->setParameter('month', "%$month")
->setParameter('user', $user)
->setParameter('category', $category)
->getQuery();
return $q->getResult();
}