Created
October 2, 2012 06:48
-
-
Save slywalker/3816902 to your computer and use it in GitHub Desktop.
CakePHP2.x add datetime field (RFC2822) for Date Object of JS into results of Model::find()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('ModelBehavior', 'Model'); | |
class DateTimeRFCBehavior extends ModelBehavior { | |
public $settings = array(); | |
protected $_defaults = array( | |
'fields' => array('updated', 'modified', 'created'), | |
'format' => array( | |
'rfc2822' => 'r' | |
), | |
); | |
public function setup(Model $Model, $config = array()) { | |
$this->settings[$Model->alias] = array_merge($this->_defaults, $config); | |
} | |
public function afterFind(Model $Model, $results, $primary) { | |
foreach ($results as &$result) { | |
$result = $this->_addField($Model, $result); | |
} | |
return $results; | |
} | |
protected function _addField(Model $Model, $result) { | |
$setting = $this->settings[$Model->alias]; | |
foreach ($setting['fields'] as $field) { | |
if (!isset($result[$Model->alias][$field])) { | |
continue; | |
} | |
foreach ($setting['format'] as $name => $format) { | |
if ($datetime = $result[$Model->alias][$field]) { | |
$result[$Model->alias][$field . '_' . $name] = date($format, strtotime($datetime)); | |
} else { | |
$result[$Model->alias][$field . '_' . $name] = null; | |
} | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment