【问题标题】:Yii2 Timestamp Behavior is returning 0000-00-00 00:00:00Yii2 时间戳行为正在返回 0000-00-00 00:00:00
【发布时间】:2015-06-27 11:24:25
【问题描述】:

我从其他网站和 stackoverflow 答案 (yii2 behaviors ActiveRecord::EVENT_BEFORE_INSERT not working) 复制了以下代码,但无法正常工作:

 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()'),
            ],
        ];
    }

我的模型代码是:

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;
    }
}

当我创建新设置时,created_atupdated_at 列设置为 0000-00-00 00:00:00,但是当我再次更新该行时,updated_at 有效。似乎 EVENT_BEFORE_INSERT 没有执行。

【问题讨论】:

  • 列的数据类型是什么?
  • 时间戳,所以 yyyy-mm-dd hh:mm:ss
  • CURRENT_TIMESTAMP()函数检查。

标签: php activerecord yii2 behavior


【解决方案1】:

在出现更合适的答案之前,我使用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;
   }
}

【讨论】:

    【解决方案2】:

    我在 MySQL 中也遇到过同样的问题。问题是我的“created_at”数据库列被设置为时间戳数据类型。我发现您需要将数据库列声明为 int(11) 才能正常工作。

    【讨论】:

    • 那么您是否建议将日期保存为 unix 时间戳?
    • 是的,完全正确...它被保存为 Unix 时间戳。
    • 谢谢。这很有用。我最终选择不采用这种方法,因为它更容易在数据库中搜索。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      相关资源
      最近更新 更多