Symfony API平台-更新可空字符串错误

Symfony API平台-更新可空字符串错误,symfony,api-platform.com,Symfony,Api Platform.com,我有一个作为api平台资源公开的实体,它包含以下属性: /** * @ORM\Column(type="string", nullable=true) */ private $note; 当我尝试更新实体(通过PUT)时,发送以下json: { "note": null } 我从Symfony序列化程序中得到以下错误: [2017-06-29 21:47:33]request.CRITICAL:未捕获的PHP异常Symfony\Component\Serializer\Excepti

我有一个作为api平台资源公开的实体,它包含以下属性:

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $note;
当我尝试更新实体(通过PUT)时,发送以下json:

{
  "note": null
}
我从Symfony序列化程序中得到以下错误:

[2017-06-29 21:47:33]request.CRITICAL:未捕获的PHP异常Symfony\Component\Serializer\Exception\unexpected valueexception:“类型为“string”的预期参数”,“NULL”given“at/var/www/html/testapp/server/vendor/Symfony/Symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.PHP第196行{“Exception”:“[对象](Symfony\Component\Serializer\Exception\UnexpectedValueException(代码:0):在/var/www/html/testapp/server/vendor/Symfony/Symfony/Symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:196,Symfony\Component\PropertyAccess\Exception\InvalidArgumentException(代码:0):在/var/www/html/testapp/server/vendor/symfony/symfony/src/symfony/Component/PropertyAccess/PropertyAccessor.php:275)中给出的类型应为“string\”和“NULL\”的参数

似乎我缺少了一些允许此属性为null的配置?更奇怪的是,当我得到一个包含null注释的资源时,该注释将正确返回为null:

{
  "@context": "/contexts/RentPayment",
  "@id": "/rent_payments/1",
  "@type": "RentPayment",
  "id": 1,
  "note": null,
  "date": "2016-03-01T00:00:00+00:00"
}

我遗漏了什么-ps我是api平台的新手好吧,正如你在评论中使用的类型提示setterála所确定的:

public function setNote(string $note) {
    $this->note = $note;
    return $this;
}
从PHP7.1开始,我们有以下选项,因为它实际上检查null或string而不是任何类型

public function setNote(?string $note) {
在以前的版本中,只需删除类型提示,如果愿意的话,在内部添加一些类型检查

public function setNote($note) {
    if ((null !== $note) && !is_string($note)) {
        // throw some type exception!
    }

    $this->note = $note;
    return $this;
}
你可能需要考虑的另一件事是使用类似:

$this->note = $note ?: null;

这是sorthand if()。要在字符串为空时将值设置为null(但“0”上存在错误,因此您可能需要执行更长的版本)。

好的,然后根据注释中的确定,您正在使用类型提示的setterála:

public function setNote(string $note) {
    $this->note = $note;
    return $this;
}
从PHP7.1开始,我们有以下选项,因为它实际上检查null或string而不是任何类型

public function setNote(?string $note) {
在以前的版本中,只需删除类型提示,如果愿意的话,在内部添加一些类型检查

public function setNote($note) {
    if ((null !== $note) && !is_string($note)) {
        // throw some type exception!
    }

    $this->note = $note;
    return $this;
}
你可能需要考虑的另一件事是使用类似:

$this->note = $note ?: null;

这是sorthand if()。若字符串为空,则将值设置为null(但“0”上存在错误,因此可能需要执行更长的版本).

只是一个疯狂的尝试,但是你使用的是一个类型提示设置器吗?谢谢!呃,为什么我没有想到-如果你想添加它作为答案。只是一个疯狂的尝试,但是你使用的是一个类型提示设置器吗?谢谢!呃,我为什么没有想到-如果你想添加它作为答案。