ZF2 将空的发布字段转换为 Null

ZF2 convert empty posted fields to Null

我正在使用字段集来填写 ZF2 中的表单。如果 postm 为空字段,则该字段在数据库中也为空。如何在数据库中强制为空字段设置 Null?

在 ZF2 中,我认为您需要使用 Zend\Filter\Null or Zend\Filter\ToNull,具体取决于您使用的 ZF2 版本,Zend\Filter\Null 在 ZF2.4 中已弃用。

在您的字段集中,假设您使用的是 Zend\InputFilter\InputFilterProviderInterface 使用:

public function getInputFilterSpecification()
{
    return array(
        'your_field' => array(
            'filters' => array(
                array('name' => 'ToNull'),
            ),
        ),
    );
}