简体   繁体   English

Yii2 Timestamp Behavior正在返回0000-00-00 00:00:00

[英]Yii2 Timestamp Behavior is returning 0000-00-00 00:00:00

I have copied the following code from other websites and stackoverflow answers ( yii2 behaviors ActiveRecord::EVENT_BEFORE_INSERT not working ) and can't get it to work: 我已经从其他网站复制了以下代码,并获得了stackoverflow的答案( yii2行为ActiveRecord :: EVENT_BEFORE_INSERT无法正常工作 ),并且无法正常工作:

 public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
                'value' => new \yii\db\Expression('NOW()'),
            ],
        ];
    }

My model's code is: 我模型的代码是:

namespace app\models;

use Yii;

class Setting extends \yii\db\ActiveRecord
{
    /*
     * Autoupdate created_at and updated_at fields.
     */

    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
                'value' => new \yii\db\Expression('NOW()'),
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'settings';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['value'], 'required'],
            [['value', 'reference', 'notes'], 'string'],
            [['created_at', 'updated_at'], 'safe'],
            [['property'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'property' => 'Property',
            'value' => 'Value',
            'reference' => 'Reference',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'notes' => 'Notes',
        ];
    }

    /**
     *  Get a property
     */
    public static function getSetting($property_name)
    {
        $setting = self::find()->where(['property' => $property_name])->one();
        if(is_null($setting))
            return NULL;
        else
            return $setting->value;
    }
}

When I create a new setting, the created_at and updated_at column is set to 0000-00-00 00:00:00 but when I then update the row again, updated_at works. 创建新设置时, created_atupdated_at列设置为0000-00-00 00:00:00但是当我再次更新该行时, updated_at可以使用。 Seems like the EVENT_BEFORE_INSERT isn't executing. 似乎EVENT_BEFORE_INSERT没有执行。

Until a more suitable answer comes along, I achieved it using beforeSave() : 在找到更合适的答案之前,我使用beforeSave()实现了它:

/*
* Autoupdate created_at and updated_at fields.
*/

public function beforeSave($insert)
{
   if (parent::beforeSave($insert)) {
      if($insert)
         $this->created_at = date('Y-m-d H:i:s');
      $this->updated_at = date('Y-m-d H:i:s');
      return true;
   } else {
      return false;
   }
}

I have had this exact same problem in MySQL. 我在MySQL中有这个完全相同的问题。 The issue was that my database column for "created_at" was set as a timestamp datatype. 问题是我的“ created_at”数据库列设置为时间戳数据类型。 I have found that you need to declare the database column as int(11) for it to work properly. 我发现您需要将数据库列声明为int(11)才能正常工作。

您可以通过以下代码在Yii2中使用time()方法:

Yii::$app->formatter->asTimestamp(date('Y-d-m h:i:s'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM