使用 API-Platform 在 json 中允许 NULL 值
Allowing NULL value in json with API-Platform
我目前有这个实体,我想在我的 JSON 中显示我的 属性 "firedDate" 即使值为 null。
/**
* @ApiResource(normalizationContext={"groups"={"employee"}})
* @ApiFilter(DateFilter::class, properties={"dateProperty": DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER})
* @ORM\Table(name="employee")
*/
class Employee
{
// ...
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"employee"})
*/
private $firedDate;
public function getFiredDate(): ?\DateTimeInterface
{
return $this->firedDate;
}
// ...
}
我做错了什么? :/
谢谢!
也许您的实体缺少像这样的 getter?
public function getFiredDate(): \DateTime
{
return $this->firedDate;
}
您是否低于 PHP 7.0 或更高版本?
在 PHP 7.1 中,您可以为函数设置可为空的 return 类型,因此您的
public function getFiredDate(): ?\DateTime
{
return $this->firedDate;
}
对于 \DateTime 之前的 ?
,该函数也将 return null
。
刚刚从 github 上的朋友那里得到了解决方案,这里是:
* @ApiResource(
* itemOperations={"get"},
* )
之前:
{
"@context": "/contexts/Employee",
"@id": "/employees/1",
"@type": "Employee",
"id": 1,
"name": "Oliver",
"hired": "2019-10-10T00:00:00+00:00",
"experience": 0,
"salary": "1200.00",
"job": {
"@id": "/employee_jobs/1",
"@type": "EmployeeJob",
"id": 1,
"name": "Mécanicien"
}
}
之后:
{
"@context": "/contexts/Employee",
"@id": "/employees/1",
"@type": "Employee",
"id": 1,
"name": "Oliver",
"hired": "2019-10-10T00:00:00+00:00",
"experience": 0,
"salary": "1200.00",
"firedDate": null,
"job": {
"@id": "/employee_jobs/1",
"@type": "EmployeeJob",
"id": 1,
"name": "Mécanicien"
}
}
我想我找到了解决这个问题的正确方法。
在 normalizationContext
:
的 false
中设置 skip_null_values
* @ApiResource(
* itemOperations={
* "get" = {
* //...
* }
* "put" = {
* //...
* },
* "patch" = {
* //...
* }
* },
* collectionOperations={
* "get",
* "post" = {
* //...
* }
* },
* normalizationContext={
* "skip_null_values" = false,
* "groups" = {"object:read"}
* },
* denormalizationContext={"groups" = {"object:write"}}
* )
我目前有这个实体,我想在我的 JSON 中显示我的 属性 "firedDate" 即使值为 null。
/**
* @ApiResource(normalizationContext={"groups"={"employee"}})
* @ApiFilter(DateFilter::class, properties={"dateProperty": DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER})
* @ORM\Table(name="employee")
*/
class Employee
{
// ...
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"employee"})
*/
private $firedDate;
public function getFiredDate(): ?\DateTimeInterface
{
return $this->firedDate;
}
// ...
}
我做错了什么? :/ 谢谢!
也许您的实体缺少像这样的 getter?
public function getFiredDate(): \DateTime
{
return $this->firedDate;
}
您是否低于 PHP 7.0 或更高版本? 在 PHP 7.1 中,您可以为函数设置可为空的 return 类型,因此您的
public function getFiredDate(): ?\DateTime
{
return $this->firedDate;
}
对于 \DateTime 之前的 ?
,该函数也将 return null
。
刚刚从 github 上的朋友那里得到了解决方案,这里是:
* @ApiResource(
* itemOperations={"get"},
* )
之前:
{
"@context": "/contexts/Employee",
"@id": "/employees/1",
"@type": "Employee",
"id": 1,
"name": "Oliver",
"hired": "2019-10-10T00:00:00+00:00",
"experience": 0,
"salary": "1200.00",
"job": {
"@id": "/employee_jobs/1",
"@type": "EmployeeJob",
"id": 1,
"name": "Mécanicien"
}
}
之后:
{
"@context": "/contexts/Employee",
"@id": "/employees/1",
"@type": "Employee",
"id": 1,
"name": "Oliver",
"hired": "2019-10-10T00:00:00+00:00",
"experience": 0,
"salary": "1200.00",
"firedDate": null,
"job": {
"@id": "/employee_jobs/1",
"@type": "EmployeeJob",
"id": 1,
"name": "Mécanicien"
}
}
我想我找到了解决这个问题的正确方法。
在 normalizationContext
:
false
中设置 skip_null_values
* @ApiResource(
* itemOperations={
* "get" = {
* //...
* }
* "put" = {
* //...
* },
* "patch" = {
* //...
* }
* },
* collectionOperations={
* "get",
* "post" = {
* //...
* }
* },
* normalizationContext={
* "skip_null_values" = false,
* "groups" = {"object:read"}
* },
* denormalizationContext={"groups" = {"object:write"}}
* )