方法一、
使用$model->update_time = time();
public function actionUpdate ($id)
{
$model = $this->findModel($id);
#$model->update_time = time();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
$categorys = TestCat::getTestCatTree();
return $this->render('update', [
'model' => $model,
'categorys' => $categorys,
]);
}
}
方法二、
behaviors更新
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
class Test extends ActiveRecord
{
public static function tableName()
{
return '{{%test}}';
}
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => '标题',
'test_cat_id' => '分类',
'content' => '内容',
'url' => '链接',
'image' => '图片'
];
}
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['add_time', 'update_time'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
],
],
];
}