0

我想用 Doctrine 的查询生成器写这个语句:

WHERE e.date LIKE '%$month'

(我希望所选日期以变量 $month 的值结束)

e.date 是 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.

你能帮我吗?提前非常感谢!

4

1 回答 1

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();
}
于 2013-05-21T14:37:55.317 回答