By dutchslab on
Hi-
...
$node -> attFoo = NULL;
node_save($node);
This saves attFoo in the Database as 0, not NULL. Debuging, I can see that the value is indeed NULL when it comes in to the hook_node_save function, but for some reason, its translating to a zero upon save.
I've put some logic into my hook_node_save implementation so that if attFoo == NULL, I just run a seperate query which omits the attFoo attribute altogether, so its saved correctly, but I'd rather know what this is happening in the first place.
thanks,
geremy
Comments
NULL is not a value
NULL is not a value in itself, it represents a variable with no value (see http://nl3.php.net/manual/en/language.types.null.php#language.types.null...). When you send stuff to the database on submit, it will expect a value, which NULL isn't. And so your NULL is converted to 0 -- probably by some function like mysql_real_escape_string(). You can check database.inc and database.mysql.inc or database.pgsql.inc for the details.
If you ever assigned an object to $node->attFoo (say $node->attFoo = (object) someArray), you would need to serialize the object before sending it to the database, and unserialize it when it is retrieved. Check php.net for the syntax.
thanks! whats the advantage
thanks! whats the advantage of converting my NULL variable to anything other than a NULL value for the DB, when NULL is a perfectly legal value for a DB column (assuming its set NULL ALLOWED)?
NULL is not NULL
George Bush and George Clooney are both called George, yet one is a Bush and the other is a Clooney.