php创建的时间不出来,php – Yii框架创建时的日期时间格式无效

我有这样的项目表结构

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 ;

当我尝试用下一个表单创建新记录时

模型中的规则:

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'),

);

}

我收到了错误

CDbCommand failed to execute the SQL statement: SQLSTATE[22007]:

Invalid datetime format: 1292 Incorrect datetime value: ” for column

‘create_time’ at row 1. The SQL statement executed was: INSERT INTO

projects (title, description, url, create_time,

create_user_id, update_time, update_user_id) VALUES (:yp0, :yp1,

:yp2, :yp3, :yp4, :yp5, :yp6)

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

解决方法:

试试这个,因为这是Yii本身内部的自包含行为,它应该工作:

在规则模型中添加:

public function behaviors(){

return array('CTimestampBehavior'=>array(

'class' => 'zii.behaviors.CTimestampBehavior',

'createAttribute' => 'create_time',

'updateAttribute' => 'update_time',

'setUpdateOnCreate' => true,

));

}

标签:php,mysql,database,yii

来源: https://codeday.me/bug/20190825/1721644.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值