cakePHP timestamp field is not saved properly
I have a record with a timestamp field, which its default is defined as current_timestamp in the mySql db. I use the code:
$this->Ad->Click->create();
$this->Ad->Click->set('ad_id', $id);
$this->Ad->Click->save($this->data);
after the record is saved, the timestamp field stays 0000-00-00 00:00:00. why is that?
开发者_如何学Cof course, i understand that i can use php time functions to set the field value, or mySQL functions, in a query.. but i prefer the 'satandard way'.
If you set the field name to "created" Cake will automatically use the current datetime when creating the record.
http://book.cakephp.org/view/1015/created-and-modified
After researching the issue further, it is an issue with the way CakePHP handles the CURRENT_TIMESTAMP keyword
"But CakePHP doesn’t understand the CURRENT_TIMESTAMP keyword and so treats it as a string and wraps it in quotes." - http://techlog.p2061.org/2009/04/17/cakephp-and-current_timestamp/
To enable the default behavior to happen in the database, you can blacklist the field you want to be set in the DB so CakePHP doesn't process it: http://nuts-and-bolts-of-cakephp.com/2009/03/11/blacklist-your-model-fields-for-save/
Although blacklisting works, I decided to go with the method of setting the value of the field before saving. In this case, it is important to ensure that the times of the app server and db server are in sync!
$this->data['Ad']['time'] = date('c');
I know you said this wasn't what you wanted, but it seems to be a bit of an easier fix in my opinion.
精彩评论