According to the Symfony 2.4 Documentation, any form field that is not required, but submitted without any value (default value for choice fields or empty value for text fields), will be saved to the entity with a NULL value. So if your entity field is defined as NOT NULL (e.g. not nullable=true), when you persist the entity you will get a nasty error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'shell' cannot be null
So the documentation says that if you don't want it to use NULL as the default value, you can kindly specify the empty_data
attribute on a text or choice field. However, this is not working for me.
Entity Field (not nullable):
/**
* @ORM\Column(type="string")
*/
protected $shell = '';
Form Builder (not required):
->add('shell', 'choice', array(
'label' => 'Shell',
'choices' => array('shell' => 'Fancy Pants'),
'required' => false,
'empty_data' => ''
))
Am I misunderstanding this empty_data
attribute? Am I missing some important setting elsewhere? What is the recommended way of doing this?
UPDATE
This Github ticket explains that this was an issue back in 2012, and it hasn't been fixed yet.
That means that everyone who is using the form builder, is forced to make any field that is not required into a nullable field? That seems pretty opinionated for the framework... there are lots of reasons we don't want to use NULL when a default '' or 0 doesn't have unique meaning and we don't need a NULL. For many queries, having to check for the presence of both field=0 OR field=NULL, is a pain.
Is there some better solution other people are using?