Answers
这样写试试:
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->date = time();
return true;
} else {
return false;
}
}
不知道题主是什么困扰,但是你如果是非得不走
rules()
,可以直接在
save()
方法指定
$runValidation
为
false
RTFM
http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#save(...
cdrwind
answered 10 years, 1 month ago
如果楼主是单纯想要给时间字段赋值,建议在模型里添加如下代码:
php
public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at','updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at' ], ], }
然后我再正面回答一下楼主问题,流程如下:
flow
st=>start: $model->save(runValidation) e=>end: 整个请求结束 runValidation=>condition: runValidation? beforeValidate=>operation: beforeValidate validate=>operation: validate(rules在这儿执行) afterValidate=>operation: afterValidate beforeSave=>operation: beforeSave save=>operation: save afterSave=>operation: afterSave st->runValidation runValidation(yes,right)->beforeValidate runValidation(no)->beforeSave beforeValidate->validate->afterValidate(left)->beforeSave beforeSave->save->afterSave->e
我勒个去,为了画这个流程图,我专门去看了下
markdown
的流程图语法。。。一晚上时间就白费了。。。楼主,你要负责
体外预应力
answered 10 years, 1 month ago