我需要计算一个value属性,并将其添加到API平台响应中。
我有这个对象:
"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,
}我正在使用带有API平台的Symfony 4
发布于 2019-07-09 17:18:03
您可以创建一个getter方法并向其添加@Groups注释。例如:
/**
* @Groups({"your_group_name_here"})
*/
public function getEstEndDatetime(): DateTimeInterface
{
return $this->getBeginDatetime()->modify('+ ' . ($this->getMaxHours() * 3600) . ' seconds');
}请注意,在执行此操作时,如果日期是可变日期,则它将被更改。因此,对beginDateTime属性使用不可变的日期会更好。
https://stackoverflow.com/questions/56948760
复制相似问题