I'm experiencing some trouble with saving data to some of my custom columns in the sales_flat_order_payment table.

After quite some time I realized that when I call $this->payment->save(), the data is not actually saved immediately to the database.

For example I get the current time from php with the time() function that returns a number in seconds, I want to save it to the database, which works fine.

Apparently MySql converts the number to the correct string representation. But when I want to pull the same data from the database again after having called save(), I'm not getting the data from the database, but rather the actual unconverted time() value that I saved to the object.

So I'm expecting a date string to call strtotime(), but I get the number in seconds that I inserted earlier. It's even worse when I'm saving a float value that gets rounded in the database but the object still contains the unrounded number.

So is there a way to update the data in the object after calling save()?


Short answer: call $object->load($object->getId()) on the object; this should load the object again, with the cost of additional queries; worst case scenario, load a completely new object.

Long boring answer: there is a lot of magic regarding data formatting in PHP, MySQL and Magento and the two cases you've mentioned dates and floats are the most susceptible to this magic.

Before you save the object, the data that you set on the object usually comes from the view, so it's formatted according to the view rules: most of the inputs in the forms are texts to fields tend to be strings, the dates tend to be in human readable localized format and set to a specific timezone, floats tend to have a set precision etc.

When the object is saved, the data is automagically converted to the format in which the database can store it, e.g floats tend to have a higher precision, dates tend to be unix timestamps in UTC timezone, or standards MySQL date format etc.

To give you a practical example, let's say that your shop has two store views, one for the US and one for France. Dates in the US are in the mm/dd/yy format while in France dates are dd/mm/yyyy. You want to allow users to select the date in their own format but want to save it to the DB in a single normalized format, hence magic. Unfortunately, this magic doesn't alwasy go both ways in Magento so you have this situation.

  • thanks, calling load() worked. I tried to call load before but I didn't know it needs the owns id as argument so it had no effect. – user1563232 Sep 18 '15 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.