我有一个拥有属性(property)的实体:
/**
* @var string The title attr
*
* @ORM\Column(type="string", length=255)
*/
protected $title = '';
表单类型为:
$builder
->add('title', 'text', array(
'required' => false,
))
但是当我从 form
获取此空值并尝试将此实体保存到数据库时,出现以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null
为什么它是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 = '';
关于php - 如何使用空字符串值将实体保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929636/