3

So there is a form, and non-required fields, like say "first_name". When I leave this field empty, I got an error that "Column 'first_name' cannot be null", because indeed it can't. But why does Doctrine converts empty string to NULL?

But I have to questions regarding this:

  1. Is this possible to disable it?

  2. What's your opinion about it? Until now I was used to the fact that when a form field was empty, I simply inserted an empty string. Not NULL o_O But... somebody told me this is a principle we should follow. For me however, NULL means "there is no value", and '' (empty string) means something different - "there IS a value, but it's empty". What do you think?

p.s. I use MySql if it matters.

edit:

there is a similar question:

Symfony2 forms interpret blank strings as nulls but maybe there is a new solution as of 2016?

The only solution which work is the one from the accepted answer:

Or otherway when you have the function setRecap you can check if empty or not set and set the value you expect.

public function setRecap($recap) {
  if(!isset($recap)) {
    $this->recap = ''
  } else {
    $this->recap = $recap;
  }
}

But I have a lot of such fields, and this seems overkill to me, there must a simpler solution.

p.s. I did not try Thomas Decaux' answer below the accepted answer, because it seems hacky to me.

Community
  • 1
  • 1
konrad_firm
  • 859
  • 1
  • 11
  • 28

2 Answers2

2

Well, as somebody suggested me, it's as simple as:

public function setUserName($userName) {
    $this->userName = (string)$userName;
    return $this;
}

In an entity.

Edit: plus, we need to give a default value for the variable, like:

$userName = '';
konrad_firm
  • 859
  • 1
  • 11
  • 28
2

Frankly it would be best to allow NULL values for your first names in your DB. Doing what you did just make things more complicated.

Symfony is developped by professionals and to me that conversion seems reasonable, because an empty string isn't more interesting than NULL.

What you said above, that NULL and an empty strings are two different things, is true in a sense, but what does that mean to have an empty name (i.e. empty string)? Isn't it better to say that this guy has no known first name (i.e. NULL)?

EDIT:

In response to your first comment:

Well in that case, typecasting to string is the right approach.

Zephyr
  • 1,598
  • 11
  • 22
  • 1
    This view is at least debatable, other people have strong feelings against using NULLs in db, for example: http://www.databasedesign-resource.com/null-values-in-a-database.html, but you can find other examples as well. – Elzo Valugi Apr 04 '16 at 08:39
  • @Zephyr Well, yes, this was not a good example, but there are times when I want to say "we don't have this information" == NULL in contrast to "we have this information, but it's empty" == '' (empty string). But +1 because in case of "first name" you're 100% right. An example when I don't want NULL is an address like in: Flat 104/B 24, Roberts Street - this is when somebody lives in a flat, but if he/she doesn't there is only house number. And I want the "flat" column to be an empty string (we have this information) and not NULL, which has always meant "we don't have this information" to me. – konrad_firm Apr 04 '16 at 08:40
  • 1
    Here are some more: https://dba.stackexchange.com/questions/59/when-to-use-null-and-when-to-use-an-empty-string and https://dba.stackexchange.com/questions/5222/why-shouldnt-we-allow-nulls – Elzo Valugi Apr 04 '16 at 08:41
  • @ElzoValugi wow, this guy from your second link actually uses the exactly same argument as I do : ) https://dba.stackexchange.com/questions/59/when-to-use-null-and-when-to-use-an-empty-string – konrad_firm Apr 04 '16 at 08:45
  • 1
    Indeed. My opinion is that Doctrine should not convert empty to null, but is just my 2 cents. – Elzo Valugi Apr 04 '16 at 08:47
  • @ElzoValugi: I see your point, it's true that I frequently use `IS NULL` and such in my Doctrine requests to avoid the ambiguities depicted in your link. I personnally favor exact representation of data over complexity of use of said data, but in the end choosing one over the other is a matter of opinion. But for having used SF for quite some time now, I can tell the Symfony/Doctrine guys decided to favor `NULL` over empty strings in such cases. My advice would be to go along the inclination set by the framework for the sake of simplicity. – Zephyr Apr 04 '16 at 09:31
  • I hope some Symphony/Doctrine guys will give their opinions on this matter as well. – Elzo Valugi Apr 04 '16 at 09:47