2013-04-18 169 views
0

我在yii框架中使用EJuiDateTimePicker擴展的日期和時間選擇器。我已經下載EJuiDateTimePicker和保護下\擴展放在\瑞EJuiDateTimePicker值沒有保存到數據庫

瀏覽:

$this->widget(
        'ext.jui.EJuiDateTimePicker', 
        array(
         'model'  => $model, 
         'attribute' => 'todo_datetime', 
         'value' => $model->todo_datetime, 
         'options' => array(
          'dateFormat' => 'dd-mm-yy', 
          'timeFormat' => 'hh:mm:ss',//'hh:mm tt' default 
         ), 
        ) 
       ); 

行動:

if(isset($_POST['Todo'])) 
{ 
    $model->attributes=$_POST['Todo']; 
       if($model->save()) 
     $this->redirect(array('view','id'=>$model->id)); 
} 

問題:

的保存價值在數據庫表中是0000-00-00 00:00:00它沒有考慮日期時間選擇器的價值。

編輯:

模型驗證

return array(
    array('todo_text, todo_datetime, priority, status', 'required'), 
    array('priority, status', 'numerical', 'integerOnly'=>true), 
    // The following rule is used by search(). 
    // Please remove those attributes that should not be searched. 
    array('todo_text, todo_datetime, added_on, priority, status', 'safe', 'on'=>'search'), 
); 
+1

向我們展示你的模型驗證規則 – soju

+1

什麼是你的分貝的日期字段(日期時間,時間戳,...)? – darkheir

+0

我有日期時間作爲數據類型 – CodeManiac

回答

0

嘛,沒什麼好奇怪的,你應該用你的小部件,並在數據庫中同一日期的格式,實際上是:

    您的小工具中的
  • dd-mm-yy hh:mm:ss
  • 在數據庫:yyyy-mm-dd hh:mm:ss

可以使用的getter/setter或beforeSave /一個afterFind對付這個共同的問題,這裏是一個簡單例如,使用的getter/setter:

在你的模型

public $dateTimeIncomeFormat = 'yyyy-MM-dd H:mm:ss'; 
public $dateTimeOutcomeFormat = 'dd-MM-yy H:mm:ss'; 

public function getDateTime() 
{ 
    return Yii::app()->dateFormatter->format($this->dateTimeOutcomeFormat, CDateTimeParser::parse($this->todo_datetime, $this->dateTimeIncomeFormat)); 
} 

public function setDateTime($datetime) 
{ 
    $this->todo_datetime = Yii::app()->dateFormatter->format($this->dateTimeIncomeFormat, CDateTimeParser::parse($datetime, $this->dateTimeOutcomeFormat)); 
} 

這將創建一個可以在你的widget使用dateTime虛擬屬性。 不要忘記在模型驗證規則中添加此屬性。

http://www.yiiframework.com/doc/guide/1.1/fr/topics.i18n#date-and-time-formatting

+0

我有正確的日期時間格式在小工具選項'選項'=>數組( 'dateFormat'=>'yy-mm-dd', 'timeFormat'=>'h:mm:ss' ,//'hh:mm tt'默認 ), – CodeManiac