我在 app/views/mymodel/add.ctp 中这样做:
<?php echo $form->input('Mymodel.mydatefield'); ?>
然后,在app/controllers/mymodel_controller.php:
function add() {
# ... (if we have some submitted data)
$datestring = $this->data['Mymodel']['mydatefield']['year'] . '-' .
$this->data['Mymodel']['mydatefield']['month'] . '-' .
$this->data['Mymodel']['mydatefield']['day'];
$mydatefield = DateTime::createFromFormat('Y-m-d', $datestring);
}
绝对必须有更好的方法来做到这一点 - 我只是还没有找到 CakePHP 的方法......
There absolutly has to be a better way to do this - I just haven't found the CakePHP way yet...
我想做的是:
function add() {
# ... (if we have some submitted data)
$mydatefield = $this->data['Mymodel']['mydatefiled']; # obviously doesn't work
}
我知道这是一个老问题,但万一其他人来寻找答案:CakePHP 的通用 Model 类有一个方法, ::deconstruct(),用于在内部处理这个必要的逻辑.你可以这样使用它:
I know this is an old question, but in case anyone else comes looking for an answer: CakePHP's generic Model class has a method, ::deconstruct(), that is used to handle this necessary logic internally. You can use it like this:
$stringDate = $this->MyModel->deconstruct('fieldname', $arrayDate)
这篇关于如何最好地将 CakePHP 日期选择器表单数据转换为 PHP DateTime 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽