简体   繁体   中英

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. eg { "available": null } . The database field is a nullable boolean type which is set by Doctrine. So in effect, the value needs to be able to be 1, 0 or null (ie unknown). Sending a null value seems to be ignored though. This could be an issue with either Symfony2 forms or with FOSRest. I have tried adding empty_value => null and empty_data => null to the form, but nothing seems to work. Any idea if there is a setting which allows accepting null as an option?

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. So if you want 3 choices better use choice field not checkbox, with (Choose, Yes, No) with values (null, 1, 0).

You can check this but i don't think it will help much 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。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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