Ricardo Stuven’s comment inspired me to completely refactor a function described in an earlier post. So from now on my app model contains two functions: setBoolean and setDate.
First, Ricardo’s setBoolean function:
function setBoolean($field) { $this->data[$this->name][$field] = isset($this->data[$this->name][$field]) ? 1 : 0; }
Second, my setDate function:
function setDate($field, $format = 'Y-m-d H:i') { $model = $this->name; $fieldAddOns = array('min', 'hour', 'day', 'month', 'year'); $dateInfo = array(); foreach ($fieldAddOns as $fieldAddOn) { $dateInfo[$fieldAddOn] = isset($this->data[$model][$field . '_' . $fieldAddOn]) ? intval($this->data[$model][$field . '_' . $fieldAddOn]) : null; } $this->data[$model][$field] = date($format, mktime($dateInfo['hour'], $dateInfo['min'], null, $dateInfo['month'], $dateInfo['day'], $dateInfo['year'])); }
If you want to use these functions, you have to copy them to your app/app_model.php. Copy cake/app_model.php, if app/app_model.php does not exist. After that you can use these functions in your models as shown in this example:
function beforeSave() { $this->setBoolean('active'); $this->setDate('start'); return true; }
Hmm this looks very cool (!). I’ve worked around this problem (date’s) by using a function out of the scaffold controller called _cleanUpFields() (see thinkingphp.org) but this is better! Nice job!
Daniel, this is a great solution to the problem of POST data not returning anything for unchecked checkboxes. Just be careful when using Model::saveField( ), because it will automatically write a 0 (false) value in any checkbox that is not set.
gwoo also has a paste in Cakebin on a conversion of all the HTML helper date and time fields to SQL time.
@Troy: Thanks for the tip. Here is the link: http://cakephp.org/pastes/show/00e37cf0187d6b50ab921776f3ee7b8f
@Nate: Thanks for the warning. I was not aware of that.
[…] Important: As it turns out this might not be the most “best practice” solution to working with date fields since this stuff should be better inside of the model. If you care to do things the right way definitly have a look at CakerBaker’s Blog post that deals with that topic and forget about the this step here ; ). […]
You can make this
$this->data[$this->name][$field] = isset($this->data[$this->name][$field]) ? 1 : 0;
even shorter to:
$this->data[$this->name][$field] = isset($this->data[$this->name][$field]);
because isset already returns true or false.
If you want it to be numeric consider typecasting to integer which is faster than intval() as this:
$this->data[$this->name][$field] = (int) isset($this->data[$this->name][$field]);
@Markus: Thanks for your hint, the shorter version is much cleaner than what I used in the article :)
…A Friend recommended your blog
[…]The overall look of your website is excellent, let neatly as the content material![…]
…Links
[…]Great weblog right here! Additionally your website loads up very fast![…]
…Recent Blogroll Additions
[…]The overall look of your website is excellent, let neatly as the content material![…]
…Click here for or more Information
[…] What web host are you the usage of? Can I get affiliate hyperlink to your host? I wish web site loaded up as quickly as yours lol[…]