Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony-查询和_Php_Mysql_Symfony_Sum - Fatal编程技术网

Php Symfony-查询和

Php Symfony-查询和,php,mysql,symfony,sum,Php,Mysql,Symfony,Sum,我正在写一个查询,它将返回该用户的总花费金额。支出总额=所有交易金额之和,其中类型=支出。我已将基中的类型字段设置为“花费” 它回来了 无效参数:查询中未定义令牌类型 我的密码 public function getTotal(User $user, $type) { $query = $this->getSpendingRepository() ->createQueryBuilder('p') ->select("sum(p.amou

我正在写一个查询,它将返回该用户的总花费金额。支出总额=所有交易金额之和,其中类型=支出。我已将基中的类型字段设置为“花费”

它回来了

无效参数:查询中未定义令牌类型

我的密码

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
替换

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

我修好了

 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;
}

加油:
:expense
不是
类型
我想在那里定义字段“type”的值。。还有别的办法吗?你真的确定吗?你试过了吗<代码>:expense是查询中变量的名称。错误清楚地表明,在查询中找不到变量
type
。因为您将其命名错误。Expense是我在db表的“type”字段中输入的值。。其中type='spend'按用户分组在查询中定义了变量“:spend”和“:user”,但通过
setParameters
传递“type”和“user”的值。这个答案实际上应该解决问题。
 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;
}