【发布时间】:2019-04-30 12:59:10
【问题描述】:
我正在使用 symfony 和 api 平台。
我有一个资源:
/**
* @ApiResource()
*/
class Credit
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $value;
}
/api/credits 的结果是:
{
"@context": "/api/contexts/Credit",
"@id": "/api/credits",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/api/credits/1",
"@type": "Credit",
"id": 1,
"value": 200,
"createdAt": "2019-03"
},
{
"@id": "/api/credits/2",
"@type": "Credit",
"id": 2,
"value": 200,
"createdAt": "2019-04"
}
],
"hydra:totalItems": 2
}
我想在结果中添加额外信息,例如 totalValues : 400(所有结果的“值”之和)
我该怎么做
预期结果:
{
"@context": "/api/contexts/Credit",
"@id": "/api/credits",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/api/credits/1",
"@type": "Credit",
"id": 1,
"value": 200,
"createdAt": "2019-03"
},
{
"@id": "/api/credits/2",
"@type": "Credit",
"id": 2,
"value": 200,
"createdAt": "2019-04"
}
],
"hydra:totalItems": 2,
"totalValues": 400
}
【问题讨论】:
-
这个答案可能对你有帮助:stackoverflow.com/a/52592464/1545904
-
感谢您的回复,但这为我添加了每个元素的信息,但我想添加孔结果的信息,例如 totalItems
-
here 我们必须实现一个集合规范器
标签: symfony doctrine api-platform.com