>当我想在 yii2 中更新表单时
它告诉我这个错误
创建时间无效 -- 更新时间无效
你觉得这是什么?
public function beforeSave($insert)
{
$date = new DateTime();
$date->setTimestamp(time());
$date->setTimezone(new DateTimezone('Istanbul'));
$this->update_time = $date->format('Y-m-d H:i:s');
if($this->isNewRecord)
$this->creation_time = $date->format('Y-m-d H:i:s');
return parent::beforeSave($insert);
}
我的问题已解决
在规则中将日期更改为安全
[['creation_time', 'update_time'], 'safe'],
您的代码是正确的"Nader",但有时分配值不起作用,因此只需删除该值或使用它
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
//'value' => new Expression('NOW()'),
],
];
}
只需注释值,它会起作用。有关更多详细信息 https://www.yiiframework.com/doc/api/2.0/yii-behaviors-timestampbehavior
只是可以使用可用的时间戳行为行为
use yiibehaviorsTimestampBehavior;
use yiidbExpression;
use yiidbActiveRecord;
class Mymodel extends ActiveRecord{
public function behaviors() {
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'creation_time',
'updatedAtAttribute' => 'update_time',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',
ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
],
'value' => new Expression('NOW()')
]
];
}
}
从模型中删除您可能对这两个属性具有的任何验证规则 creation_time
和update_time