This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

[Solved] Store NULL values instead of empty string

Discussion relating to version 1.3.x and 1.4.x

[Solved] Store NULL values instead of empty string

by conrad » Thu Sep 23, 2010 4:48 pm

Hi, I have a simple table that has three foreign keys from which all are optional in saving because only one of them matters at the end (it isn't any trouble either if all are empty).
Symfony beautifully generates my form and shows foreign relations in select fields, because of "notnull: false" in my schema.yml, empty value fields are added too.

Unfortunately, I receive an mysql error because of empty strings entered in the INSERT statement. Replacing those empty strings with NULL, it works (in phpmyadmin)
- so my question is, how I can I force the form validator/doctrine save method to use NULL values instead of empty strings?

Does anyone know how to do this?
Thanks in advance.
conrad
Junior Member
 
Posts: 17
Joined: Mon Jul 27, 2009 9:37 pm
Location: Düsseldorf, Germany

Re: Store NULL values instead of empty string

by conrad » Sat Sep 25, 2010 12:53 pm

Got the solution, which is quite simple:
I overloaded my base class' method setTableDefinition() where I changed the 'default' value from '' to NULL.
Before that I call parent::setTableDefinition() and change only the culumns where NULL attributes should be stored to ensure a maximum on compatibility when changing the schema.
works like charm :) and no foreign key that can be null throws an mysql error anymore.
conrad
Junior Member
 
Posts: 17
Joined: Mon Jul 27, 2009 9:37 pm
Location: Düsseldorf, Germany

Re: [Solved] Store NULL values instead of empty string

by kstro21 » Mon Sep 15, 2014 9:06 pm

Hi conrad, i know this is an old post and symfony 2 is out of there, but i'm trying to accomplish the same you talk about here, but no success until know, can you provide me with an example of what you did?

Thanks
kstro21
Junior Member
 
Posts: 7
Joined: Fri Dec 11, 2009 4:14 pm
Location: Cuba