我是Yii框架中的新成员,当新记录entered.here成为我的模型规则时,我想在数据库中插入日期
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user_id, question, creation_Datetime, creator_id', 'required'),
array('user_id, creator_id', 'numerical', 'integerOnly'=>true),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user_id, question, creation_Datetime, creator_id', 'safe', 'on'=>'search'),
);
}请帮帮忙
发布于 2015-07-31 13:31:42
您可以使用参照系
public function beforeSave() {
if ($this->isNewRecord)
$this->created = new CDbExpression('NOW()');
else
$this->modified = new CDbExpression('NOW()');
return parent::beforeSave();
}或
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('title','length','max'=>255),
array('title, created, modified', 'required'),
array('modified','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'update'),
array('created,modified','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'insert')
);
}发布于 2015-07-31 11:25:24
评论@chris将其指向Yii 2,但通过查看数组格式,可以清楚地看到您使用的是YI1.0或YI1.1。
所以在这里我写了两个解决方案。
Yi1.1
public function behaviors(){
return array(
'CTimestampbehavior' => array(
'class' => 'zii.behaviors.CTimestampbehavior',
'createAttribute' => 'creation_Datetime',
)
);
}Yi1.0
因为YI1.0没有CTimestampbehavior类。您可以在您的模型中使用这种方法:
public function beforeSave() {
if($this->isNewRecord) {
$this->creation_Datetime = new CDbExpression('NOW()');
}
}发布于 2015-07-31 10:42:12
在MySQL中,您可以使用now()输入当前日期。
https://stackoverflow.com/questions/31743439
复制相似问题