过滤日期。 Cakephp返回一个数组,而不是一个日期,是正常吗? [英] filter dates. Cakephp is returning an array instead of a date, is that normal?
问题描述
我正在为一些分页列表创建过滤器,我想能够显示在两个日期之间创建的元素。
pre>
<?php echo $ this-> Form-> create('Logs');?>
< fieldset>
<?php
echo $ this-> Form-> input('start',array('type'=>'date'))
echo $ this-> Form-> input('end',array('type'=>'date'));
?>
< / fieldset>
<?php echo $ this-> Form-> end('Filter');?>
控制器:
...
$ conditions ['Logs.created BETWEEN? AND?'] = array($ this-> data ['Logs'] ['start'],$ this-> data ['Logs'] ['end']);
...
问题是 $ this-> ; data ['Logs'] ['start']
和 $ this-> data ['Logs'] ['end']
数组和我需要字符串:
[Logs] => Array
(
[start] => Array
(
[month] => 04
[day] => 19
[year ] => 2011
)
[end] => Array
(
[month] => 04
[day] => ; 19
[year] => 2011
)
)
我知道我可以使用一些php函数将数组转换为字符串,但必须有一些函数或某事在蛋糕。
感谢您的帮助。 / div>
我认为最好的打赌是你会写自己的帮助函数,并在你的控制器中调用它。
此主题介绍如何创建助手:如何更好地转换CakePHP日期选择器表单数据到PHP DateTime对象?
您可以将其加载到控制器中
App :: import('Helper','DateHelper');
$ dateHelper = new DateHelper();
此外,为什么不切换到常规文本输入或者JS日期选择器?这将解决你的问题,而不必诉诸辅助路由。你会失去下拉的可用性,虽然。
I'm building a filter for some paginated lists, and i want to be able to show the elements created between two dates. But I'm not sure how to do it correctly.
The view:
<?php echo $this->Form->create('Logs');?>
<fieldset>
<?php
echo $this->Form->input('start',array('type'=>'date'));
echo $this->Form->input('end',array('type'=>'date'));
?>
</fieldset>
<?php echo $this->Form->end('Filter');?>
The controller:
...
$conditions['Logs.created BETWEEN ? AND ?'] = array( $this->data['Logs']['start'],$this->data['Logs']['end']);
...
the problem is that $this->data['Logs']['start']
and $this->data['Logs']['end']
are arrays and i need strings:
[Logs] => Array
(
[start] => Array
(
[month] => 04
[day] => 19
[year] => 2011
)
[end] => Array
(
[month] => 04
[day] => 19
[year] => 2011
)
)
I know that i could use some php functions to transform the array into string, but there must be some function or something in cake. I feel that i'm not constructing the view correctly
Thanks for your help.
I think the best bet for you would be to write your own helper function and call it in your controller.
This thread talks about how to create your helper : How best to convert CakePHP date picker form data to a PHP DateTime object?
You can load it into the controller like so
App::import('Helper', 'DateHelper');
$dateHelper = new DateHelper();
Also, why not just switch to a regular text input and maybe a JS datepicker? This would solve your problems without having to resort to the helper route. You'd lose the dropdown availability though.
这篇关于过滤日期。 Cakephp返回一个数组,而不是一个日期,是正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!