In my database schema, not all values have to be set, but instead of setting them to null
, I would prefer to set them to an empty string.
在我的数据库模式中,并非所有值都必须设置,而是将它们设置为null,我宁愿将它们设置为空字符串。
How is this possible using Doctrine 2 in Zend Framework 2?
如何在Zend Framework 2中使用Doctrine 2?
In the Doctrine 2 docs I found the annotation nullable
that can be set to true, but in my case using a MySql database, this results in actual null
values being written. Should I just set the default value to empty string?
在Doctrine 2文档中,我发现可以设置为true的注释可为空,但在我的情况下使用MySql数据库,这会导致写入实际的空值。我应该将默认值设置为空字符串吗?
2 个解决方案
#1
Although null
is the representation of "nothing" and should be used for this: When you want the empty string to be the default you should set it as default.
尽管null是“nothing”的表示,但应该用于此:当您希望将空字符串作为默认值时,应将其设置为默认值。
class Foo
{
private $bar = '';
private $baz;
public function __construct($baz = '')
{
$this->baz = $baz;
}
}
#2
You could always just set the fields in your model to an empty string when initializing the object. Just add the __construct method to your model and set the values there.
初始化对象时,您始终只需将模型中的字段设置为空字符串即可。只需将__construct方法添加到模型中并在那里设置值。
#1
Although null
is the representation of "nothing" and should be used for this: When you want the empty string to be the default you should set it as default.
尽管null是“nothing”的表示,但应该用于此:当您希望将空字符串作为默认值时,应将其设置为默认值。
class Foo
{
private $bar = '';
private $baz;
public function __construct($baz = '')
{
$this->baz = $baz;
}
}
#2
You could always just set the fields in your model to an empty string when initializing the object. Just add the __construct method to your model and set the values there.
初始化对象时,您始终只需将模型中的字段设置为空字符串即可。只需将__construct方法添加到模型中并在那里设置值。