我发现这对我来说是最好的解决方案:
/// models/DateFormatBehaior.php
class DateFormatListener extends Doctrine_Record_Listener{
public function preInsert(Doctrine_event $Event){
$this->_prepare_date($Event);
}
public function preUpdate(Doctrine_event $Event){
$this->_prepare_date($Event);
}
// Private stuff
private function _prepare_date(Doctrine_event $Event){
$Model = $Event->getInvoker();
foreach($Model->getTable()->getColumns() as $FieldName=>$Field){
if($Field['type']=='timestamp'){
if(preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/",$Model[$FieldName],$Matches)){
$Model->$FieldName = sprintf("%s-%s-%s 00:00:00",$Matches[3],$Matches[2],$Matches[1]); // YYYY-DD-MM HH:MM:SS
}
}
}
}
}
class DateFormatTemplate extends Doctrine_Template{
public function setTableDefinition(){
$this->addListener(new DateFormatListener);
}
}
然后,每个具有时间戳字段的模型:
/// models/MyModel.php
abstract class MyModel extends Doctrine_Record{
public function setTableDefinition(){
// [...]
}
public function setUp(){
parent::setUp();
$this->actAs("DateFormatTemplate");
// [...]
}
}