/** * Override Model::deconstruct() in order to use an integrated date * value, but multipart time value. The parent method expects both * date and time to be segmented, but, if a date picker is used to * select the date, then that component is unified. * * In order to use what's already in place, we'll deconstruct the date * portion here and then pass everything to the parent method for * reassimilation. * * @param string $field The name of the field to be deconstructed * @param mixed $data An array or object to be deconstructed into a field * @return mixed The resulting data that should be assigned to a field * @access protected */ public function deconstruct($field, $data) { $type = $this->getColumnType($field); if (in_array($type, array('datetime', 'timestamp'))) { if (isset($data['date']) && !empty($data['date'])) { $date = date('U', strtotime($data['date'])); if ($date) { $data['month'] = date('m', $date); $data['day'] = date('d', $date); $data['year'] = date('Y', $date); } } } return parent::deconstruct($field, $data); }
/** * Deconstructs complex data (specifically here, date) and creates a partial * date from a Cake date array * * The Form helper in Cake splits dates into 3 pieces: month, day and year. * If the column in the database is set to a string, we'll allow a "partial * date" so users can, say, estimate the time they were baptized. * * @param string $field The name of the column * @param array $value The complex value being saved * @return mixed A string if it should be a date string, or deconstructed data * as determined by Model::deconstruct() * @see Model::deconstruct() * @see FormHelper::dateTime() */ public function deconstruct($field, $value) { if ($this->getColumnType($field) == 'string' && is_array($value)) { if (isset($value['month']) && empty($value['month'])) { $value['month'] = '00'; } if (isset($value['day']) && empty($value['day'])) { $value['day'] = '00'; } if (isset($value['year']) && empty($value['year'])) { $value['year'] = '0000'; } // convert to proper type if (array_key_exists('month', $value) && array_key_exists('day', $value) && array_key_exists('year', $value)) { $value = $value['year'] . '-' . $value['month'] . '-' . $value['day']; } } return parent::deconstruct($field, $value); }