当我想更新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'],
答案 0 :(得分:2)
您的代码是正确的" Nader"但有时候分配值不起作用所以只需删除值或使用此
const string SQL = "SELECT IntegerColumn, StringColumn FROM SomeTable WHERE IntegerColumn IN @listOfIntegers";
var conditions = new { listOfIntegers };
var results = connection.Query(SQL, conditions);
只需评论价值, 它会工作。 有关详细信息https://www.yiiframework.com/doc/api/2.0/yii-behaviors-timestampbehavior
答案 1 :(得分:1)
可以使用可用的TimestampBehavior行为
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
use yii\db\ActiveRecord;
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