如何保存实体在DB与空字符串值


How to save entity in DB with empty string value

我有一个实体的属性:

/**
 * @var string The title attr
 *
 * @ORM'Column(type="string", length=255)
 */
protected $title = '';

表单类型为:

    $builder
        ->add('title', 'text', array(
            'required' => false,
        ))

但是当我从form得到这个空值并试图将这个实体保存到DB时,我得到以下错误:

SQLSTATE[23000]:完整性约束违规:1048 Column 'title'不能为空

为什么是null ?我将此属性设置为空字符串'',而不是null

我如何在数据库中存储这个实体与空字符串''(和没有@ORM'Column(type="string", length=255, nullable=true))?

设置默认列值为空字符串:

/**
 * @var string The title attr
 *
 * @ORM'Column(type="string", length=255, options={"default":""})
 */
protected $title = '';