如何在Symfony';API平台响应?
我需要计算一个value属性并将其添加到API平台响应中 我有一个目标:如何在Symfony';API平台响应?,symfony,api-platform.com,Symfony,Api Platform.com,我需要计算一个value属性并将其添加到API平台响应中 我有一个目标: "project": { "@id": "/api/projects/1", "@type": "Projects", "id": 1, "name": "UI/UX", "beginDatetime": 1562662050, "maxHours": 8, "isActive": true, } "project": { "@id": "/api/pr
"project": {
"@id": "/api/projects/1",
"@type": "Projects",
"id": 1,
"name": "UI/UX",
"beginDatetime": 1562662050,
"maxHours": 8,
"isActive": true,
}
"project": {
"@id": "/api/projects/1",
"@type": "Projects",
"id": 1,
"name": "UI/UX",
"estEndDatetime": 1562662050 + (8*3600), // Autocalculated value
"maxHours": 8,
"isActive": true,
}
我需要这个东西:
"project": {
"@id": "/api/projects/1",
"@type": "Projects",
"id": 1,
"name": "UI/UX",
"beginDatetime": 1562662050,
"maxHours": 8,
"isActive": true,
}
"project": {
"@id": "/api/projects/1",
"@type": "Projects",
"id": 1,
"name": "UI/UX",
"estEndDatetime": 1562662050 + (8*3600), // Autocalculated value
"maxHours": 8,
"isActive": true,
}
我正在将Symfony 4与API平台一起使用您可以创建一个getter方法并向其添加
@Groups
注释。例如:
/**
*@Groups({“你的组名在这里”})
*/
公共函数getEstEndDatetime():DateTimeInterface
{
返回$this->getBeginDatetime()->modify('+'($this->getMaxHours()*3600)。'seconds');
}
请注意,在执行此操作时,如果日期是可变日期,则它将被更改。因此,最好为
beginDateTime
属性使用不可变日期。非常感谢Simeon!你的解决方案就是我一直在寻找的