symfony-查询和

8fsztsew  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(370)

我正在写一个查询,它将返回该用户的总花费金额。总花费=所有交易金额之和,其中类型=花费。我已经把我的类型字段设置为“花费”。。
它回来了
无效参数:查询中未定义令牌类型。
我的密码。

public function getTotal(User $user, $type)
{
    $query = $this->getSpendingRepository()
        ->createQueryBuilder('p')
        ->select("sum(p.amount) as total_amount")
        ->where('p.type = :spend')
        ->andWhere('p.user = :user')
        ->setParameters(['user' => $user,
                         'type' => $type])
        ->getQuery()
        ->getOneOrNullResult();

    return $query;
}

这是sql,所以我不知道我做错了什么。。

select user_id, sum(amount) as amount
from transaction
where type = 'spend'
group by user_id
vc6uscn9

vc6uscn91#

我修好了。

public function getTotalSpent(User $user, $type = 'spend')
{
    $query = $this->getTransactionRepository()
        ->createQueryBuilder('p')
        ->select("sum(p.amount) as total_amount")
        ->where('p.type = :type')
        ->andWhere('p.user = :user')
        ->setParameters(['user' => $user,
                         'type' => $type])
        ->getQuery()
        ->getOneOrNullResult();

    return $query;
}
ocebsuys

ocebsuys2#

代替

->where('p.type = :spend')

具有

->where('p.type = :type')

相关问题