Here is something I would like to do:
User:
id
first_name
second_name
gender
and I make a php Object “User”, and I modify the “User”‘s attribute, I update the database, for example,
$aUser->setGender("M");
in the set gender, I execute an update statement using php… But I got a problems here, if the user only have 4 attributes, it is ok for me to write one by one. But if the object have many attributes, I need to write more update statement….(I can write a general update statement, whatever I change which attribute, I update all the attribute once, but I think it is no point to do so.. … ) How can I simplify that? Thank you.
The comments of the question suggests it: You should use a method like
save()from time to time, instead of querying the database every time anything changes.Personally I don’t see a problem in updating every column, even if the corresponding property didn’t changed. However, you can save the original values in another (array) property and then compare the values against each other.