2012-07-27 78 views
0

我有一個Yii模型的問題,我創建了一個包含兩個日期時間列的MySQL表。Yii mysql日期時間空

然後用gii創建模型。問題是,當我從模型中獲取數據時,我得到的日期時間字段爲空。

<?php 

/** 
* This is the model class for table "Template". 
* 
* The followings are the available columns in table 'Template': 
* @property string $tmpId 
* @property integer $fanPageId 
* @property string $name 
* @property string $title 
* @property string $description 
* @property string $headerFile 
* @property string $msgNotTlt 
* @property string $msgNotMsg 
* @property string $msgNotImg 
* @property string $msgNotLnk 
* @property string $terms 
* @property string $strProm 
* @property string $endProm 
*/ 
class Template extends CActiveRecord 
{ 
    /** 
    * Returns the static model of the specified AR class. 
    * @param string $className active record class name. 
    * @return Template the static model class 
    */ 
    public static function model($className=__CLASS__) 
    { 
     return parent::model($className); 
    } 

    /** 
    * @return string the associated database table name 
    */ 
    public function tableName() 
    { 
     return 'Template'; 
    } 

    /** 
    * @return array validation rules for model attributes. 
    */ 
    public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
        array('fanPageId', 'numerical', 'integerOnly'=>true), 

        array('name, title, description, headerFile, msgNotTlt, msgNotMsg, msgNotImg, msgNotLnk, terms', 'length', 'max'=>11), 
        array('strProm, endProm', 'safe'), 
        //array('endProm, strProm', 'type', 'type'=>'datetime', 'datetimeFormat'=>'yyyy/M/d H:m:s'), 

        // The following rule is used by search(). 
        // Please remove those attributes that should not be searched. 
        array('tmpId, fanPageId, name, title, description, headerFile, msgNotTlt, msgNotMsg, msgNotImg, msgNotLnk, terms, strProm, endProm', 'safe', 'on'=>'search'), 
     ); 
    } 

    /** 
    * @return array relational rules. 
    */ 
    public function relations() 
    { 
     // NOTE: you may need to adjust the relation name and the related 
     // class name for the relations automatically generated below. 
     return array(
     ); 
    } 

    /** 
    * @return array customized attribute labels (name=>label) 
    */ 
    public function attributeLabels() 
    { 
     return array(
      'tmpId' => 'Tmp', 
      'fanPageId' => 'Fan Page', 
      'name' => 'Name', 
      'title' => 'Title', 
      'description' => 'Description', 
      'headerFile' => 'Header File', 
      'msgNotTlt' => 'Msg Not Tlt', 
      'msgNotMsg' => 'Msg Not Msg', 
      'msgNotImg' => 'Msg Not Img', 
      'msgNotLnk' => 'Msg Not Lnk', 
      'terms' => 'Terms', 
      'strProm' => 'Str Prom', 
      'endProm' => 'End Prom', 
     ); 
    } 

    /** 
    * Retrieves a list of models based on the current search/filter conditions. 
    * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. 
    */ 
    public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->compare('tmpId',$this->tmpId,true); 
     $criteria->compare('fanPageId',$this->fanPageId); 
     $criteria->compare('name',$this->name,true); 
     $criteria->compare('title',$this->title,true); 
     $criteria->compare('description',$this->description,true); 
     $criteria->compare('headerFile',$this->headerFile,true); 
     $criteria->compare('msgNotTlt',$this->msgNotTlt,true); 
     $criteria->compare('msgNotMsg',$this->msgNotMsg,true); 
     $criteria->compare('msgNotImg',$this->msgNotImg,true); 
     $criteria->compare('msgNotLnk',$this->msgNotLnk,true); 
     $criteria->compare('terms',$this->terms,true); 
     $criteria->compare('strProm',$this->strProm,true); 
     $criteria->compare('endProm',$this->endProm,true); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 
} 
+0

當用戶在填寫領域,併發送日期空,所以你不能保存數據庫,這是問題嗎? – FabianoLothor 2012-07-27 18:25:38

+0

問題是當我做的時候:Template :: model() - > find('fanPageId =?AND tmpId =?',array($ fanPageId,$ idTmp));我得到所有的信息,但日期時間爲空 – Tony 2012-07-28 17:14:28

+0

什麼類型的列?日期時間或時間戳? – FabianoLothor 2012-07-28 21:18:34

回答

0

在你的規則模型添加兩列:

array('datetime_1, datetime_2', 'safe'), 
array('id, <attributes>, datetime_1, datetime_2', 'safe', 'on'=>'search'), 

如果可能的話,你張貼代碼/模型...

+0

我發佈模型,我有你說的模型 – Tony 2012-07-27 18:14:42