yii2行为的ActiveRecord :: EVENT_BEFORE_INSERT不工作 [英] yii2 behaviors ActiveRecord::EVENT_BEFORE_INSERT not working
问题描述
在我的模型我的行为功能有:
My behavior function in my model is as follows
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['log_in_time' ],
ActiveRecord::EVENT_BEFORE_UPDATE => ['log_in_time'],
],
'value' => new Expression('NOW()'),
],
];
}
/**
* validation rules
*/
public function rules()
{
return [
['username','filter', 'filter' => 'trim'],
['username','required'],
//['username','unique'],
['username','string', 'min' => 2, 'max' => 255],
['password','required'],
];
}
/* Your model attribute labels */
public function attributeLabels()
{
return [
/* Your other attribute labels */
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['login'] = ['username','log_in_time'];//Scenario Values Only Accepted
return $scenarios;
}
但它没有更新 log_in_time
列。 log_in_time
是 DATETIME
。得到插入值 0000-00-00 00:00:00
。什么是问题?
But it is not updating the log_in_time
column . log_in_time
is DATETIME
. The value getting inserted is 0000-00-00 00:00:00
.What is the problem ?
推荐答案
您的任何机会在特定的模式覆盖beforeSave(或beforeInsert或更新前)?如果你这样做,那么你必须调用像
Are you by any chance overwriting beforeSave (or beforeInsert or beforeUpdate) in that particular model? If you do then you have to call something like
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
............
return true;
}
return false;
}
我最近做类似的东西,花了一些很好的时间研究这个才意识到,我并没有调用父之前保存。
I recently did something similar and spent some good time researching this only to realize that I did not call the parent before save.
如果您使用AttributeBehavior而不是TimestampBehavior,做你所做的事,我相信它会工作。
If you use AttributeBehavior instead of TimestampBehavior and do exactly what you did, I believe it will work.
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\AttributeBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['log_in_time' ],
ActiveRecord::EVENT_BEFORE_UPDATE => ['log_in_time'],
],
'value' => new Expression('NOW()'),
],
];
}
或者你可以尝试设置createdAtAttribute和updatedAtAttribute $为log_in_time在TimestampBehavior,这也应该工作。
Or you can try setting createdAtAttribute and $updatedAtAttribute to 'log_in_time' in the TimestampBehavior, that should also work.
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'createdAtAttribute' => 'log_in_time',
'updatedAtAttribute' => 'log_in_time',
],
];
}
我不知道为什么它不工作就像你贴。
I am not sure why it does not work like you posted.
这对我的作品100%
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'blameable' => [
'class' => BlameableBehavior::className(),
'attributes' => [
BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_by', 'update_by'],
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'update_by'
],
],
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
],
'value' => new Expression('NOW()'),
],
];
}
这篇关于yii2行为的ActiveRecord :: EVENT_BEFORE_INSERT不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!