Let's say that I have the simple object like this:
class User
{
public id;
public name;
public surname;
public gender;
public age;
}
In my code I want to create dynamical UPDATE statement for such an object according to properties that were set. So I want my UPDATE to (transfer to the database) and change only columns that were changed. If one of user's properties is unset, this property should not be included as db column in UPDATE statement.
For example, if I create User object and set only id, name and surname (age and gender leave unset)
$User=new User();
$User->id=1;
$User->name='Bob';
$User->surname='Geldof';
from this object my code should create UPDATE statement like this:
UPDATE users SET name='Bob',surname='Geldof' WHERE id=1
The problem is when a column in database can have NULL values (like age for example). In this case I should be able to null this column in database. There I should check if User object property is explicitly set to NULL. If it is set to NULL then UPDATE statement should result with:
UPDATE users SET name='Bob',age=NULL WHERE id=1
But there's no way to test if the property is set but set to NULL value!
At the moment in such a case I set property to 'NULL' string and parse it in my code to translate 'NULL' string into NULL value in creating UPDATE statement.
Is it possible to distinguish if object property is explicitly set to NULL value (not 'NULL' string)?