当您使用FormHelper并且departure_time
字段上的数据类型为DATETIME
或TIME
时,Cake将“自动地”正确处理所有查找和保存操作。
因此,事实上,实际的数据阵列拥有独立hour
,min
和meridian
领域不影响它存储在数据库中的方式,这将很好地转换为05:21:00
没有任何额外的逻辑是在模型或控制器必要。如果您想查找与此次匹配的所有记录,只需使用完整的时间戳即可找到。使用类似:
$this->Rideoffer->find('all', array(
'conditions' => array(
'Rideoffer.departure_time' => '05:21:00'
)
));
更新
在回答您的意见,如果你想立即使用插入的值,将其转换为一个DateTime对象和格式化,在你的病情。例如:
$time =& $this->request->data['Rideoffer']['DepartureTime'];
$date = new DateTime($time['hour'] . ':' . $time['min'] . ' ' . $time['meridian']);
然后格式化日期在你发现:
$this->Rideoffer->find('all', array(
'conditions' => array(
'Rideoffer.departure_time' => $date->format('H:i:s')
)
));
谢谢,但是当我匹配$ this-> request-> data ['Rideoffer'] ['DepartureTime']条件时,记录不匹配。如果我明确指定时间像你说的'05:00:00',记录被返回。为什么这样? – z22 2013-02-19 09:53:29
@ z22因为'$ this-> request-> data ['Rideoffer'] ['DepartureTime']'拥有一个数组,而不是字符串'05:00:00',所以你不能直接使用它作为条件。无论如何,请参阅我更新的答案以获取价值。 – Oldskool 2013-02-19 10:02:19
谢谢!它现在有效。 :) – z22 2013-02-19 10:18:55