Yii框架在创建时无效的日期时间格式

Dmy*_*nko 3 php mysql database yii

我有这样的projects表结构

CREATE TABLE IF NOT EXISTS `projects` (
  `project_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `url` varchar(255) NOT NULL,
  `create_time` datetime DEFAULT NULL,
  `create_user_id` int(11) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `update_user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Run Code Online (Sandbox Code Playgroud)

当我尝试用下一张表格创建新记录时

在此处输入图片说明

模型中的规则:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, description, url', 'required'),
            array('create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
            array('title, url', 'length', 'max'=>255),
            array('create_time, update_time', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('project_id, title, description, url, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
        );
    }
Run Code Online (Sandbox Code Playgroud)

我收到错误

CDbCommand未能执行SQL语句:SQLSTATE [22007]:无效日期时间格式:1292不正确的日期时间值: ''列'CREATE_TIME'在第1行执行的SQL语句是:INSERT INTO projectstitledescriptionurlcreate_timecreate_user_idupdate_timeupdate_user_id)VALUES (:yp0,:yp1,,yp2,:yp3,:yp4,:yp5,:yp6)

为什么?我如何告诉Yii日期时间字段不是必需的,如果未输入,则可以包含默认值。

San*_*gha 5

尝试一下,由于这是Yii内部的自包含行为,因此它应该可以工作:

在规则模型中添加以下内容:

public function behaviors(){
        return array('CTimestampBehavior'=>array(
        'class' => 'zii.behaviors.CTimestampBehavior',
        'createAttribute' => 'create_time',
        'updateAttribute' => 'update_time',
        'setUpdateOnCreate' => true,  
        ));
    }
Run Code Online (Sandbox Code Playgroud)