简体   繁体   English

Symfony2 FOSRest将值设置为null

[英]Symfony2 FOSRest set value to null

In a web application built with Symfony2, I need to be able to send null values as part of a PUT or PATCH request to an FOSRestBundle endpoint. 在使用Symfony2构建的Web应用程序中,我需要能够将null值作为PUTPATCH请求的一部分发送到FOSRestBundle端点。 eg { "available": null } . 例如{ "available": null } The database field is a nullable boolean type which is set by Doctrine. 数据库字段是可以为nullable boolean类型,由Doctrine设置。 So in effect, the value needs to be able to be 1, 0 or null (ie unknown). 因此,实际上,该值必须能够为1, 0 or null (即未知)。 Sending a null value seems to be ignored though. 但是,发送null值似乎被忽略了。 This could be an issue with either Symfony2 forms or with FOSRest. 这可能是Symfony2表单或FOSRest的问题。 I have tried adding empty_value => null and empty_data => null to the form, but nothing seems to work. 我尝试将empty_value => nullempty_data => null到表单中,但似乎没有任何效果。 Any idea if there is a setting which allows accepting null as an option? 知道是否有允许接受null作为选项的设置?

Boolean is either true or false normally, how can you determine that it was unchecked because it was null or unchecked because it was false, either way it is false. Boolean通常为true或false,如何确定它是未选中的,因为它为null或未选中,因为它为false,无论是否为false。 So if you want 3 choices better use choice field not checkbox, with (Choose, Yes, No) with values (null, 1, 0). 因此,如果你想要3个选择,最好使用选择字段而不是复选框,使用(选择,是,否)和值(null,1,0)。

You can check this but i don't think it will help much https://github.com/symfony/symfony/issues/10364 你可以检查这个,但我认为它不会有多大帮助https://github.com/symfony/symfony/issues/10364

Try to set in your config: 尝试在您的配置中设置:

fos_rest:
    serializer:
        serialize_null: true

也许您可以尝试发送一个字符串“0”,“1”或“null”的虚拟属性,并将其公开为您的其他属性的名称。

在DB设计时将默认值设置为NULL并在使用查询插入时忽略该字段,它将自动插入NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM