CakePHP - saving dates in your model
Learning continues! Today I learned about Controller::cleanUpFields().
If you’ve used CakePHP’s scaffolding and are ready to create your own views you might run into a problem saving dates into your database.
Here’s the scenario:
You have a table with a datetime field in it. In your view you have a form that saves a date and the date field is create with the HTMLHelper:
$html->dateTimeOptionTag();
In your controller you have a simple Model::save() call to save your new record but low and behold your date is set to NULL.
Apparently CakePHP does not do a great job of automatically taking the selections that are created by dateTimeOptionTag() and converting them into a datetime string for the database.
Instead, before you call Model::save() you need to call Controller::cleanUpFields(); it will do some magic and give you a date string that the database likes.
Recently
- 08.24 Setting up flash messages in Zend Framework
- 09.08 CakePHP default dateTime()
- 03.24 Extending CakePHP’s beforeFilter()
- 03.17 Rails-like Flash messages in CakePHP
- 03.08 CakePHP thumbnails with phpThumb
- 02.15 CakePHP model validation
- 02.10 CakePHP - saving dates in your model
- 09.19 Importing multiple pdf pages and documents into PDFlib - Part 2
- 09.18 Quick Link - Every CSS based layout you could want
- 09.04 Mongrel pid error fixed