yii 如何禁用update_at的时间戳值?

xyhw6mcr  于 2022-11-09  发布在  其他
关注(0)|答案(4)|浏览(150)

错误如下:

Database Exception – yii\db\Exception

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1504595835' for column 'updated_at' at row 1
The SQL being executed was: UPDATE `user` SET `password_reset_token`='zDPxEMdJ2EaaCf2VsI_Uf9QNf0q2MKcn_1504595835', `updated_at`='1504595835' WHERE `id`=19
Error Info: Array
(
    [0] => 22007
    [1] => 1292
    [2] => Incorrect datetime value: '1504595835' for column 'updated_at' at row 1
)

我不知道updated_at字段的时间戳是从哪里分配的。
我尝试将updated_at的值设置为date(“Y-m-d H:i:s”),但仍然没有成功。
我也试着设置行为,但仍然没有运气。
有人能帮我解决这个问题吗?

ckx4rj1h

ckx4rj1h1#

您声明的列“updated_at”和“created_at”是错误的。您必须将其更改为

int(11)

here中所述

vqlkdk9b

vqlkdk9b2#

如果要以数字格式保存updated_at,请将数据类型更改为int(11)
或者如果要以日期时间格式保存,请使用

$model->updated_at = date_format(date_create(), 'Y:m:d: H:i:s')

并将数据类型更改为datetime

fxnxkyjh

fxnxkyjh3#

首先将表中dataType更改为datetime。
在通用\组件中创建ActiveRecord.php

<?php
namespace common\components;

use Yii;
use yii\base\Exception;

/**
 * Class ActiveRecord
 * @property string $created_at
 * @property string $updated_at
 * @package common\components
 */
class ActiveRecord extends \yii\db\ActiveRecord {

    public function beforeSave($v){
        if($this->isNewRecord) {
            try {
                $this->created_at = date('Y-m-d H:i:s');
            } catch (Exception $e) {}
        }

        try {
            $this->updated_at=date('Y-m-d H:i:s');
        } catch (Exception $e) {}
        return parent::beforeSave($v);
    }

}

检查beforeSave函数从公共\组件\ActiveRecord扩展ActiveRecord,例如

<?php
namespace common\models;
use common\components\ActiveRecord;
class User extends ActiveRecord implements IdentityInterface
{
    public static function tableName()
    {
        return '{{%user}}';
    }

}
rbl8hiat

rbl8hiat4#

首先考虑created_at和updated_at是由common/models/User模型中的行为管理的,并且您的数据库表列数据类型应该是“BIGINT”以保存这些记录。
另一种以“yyyy/mm/dd h:i:s”格式保存updated_at的方法需要删除使用模型行为函数,并在更改数据库表列数据类型后手动保存这些记录

相关问题