Yii2时间戳行为正在返回00:00:00

时间:2015-06-27 11:24:26

标签: php activerecord yii2 behavior

我已经从其他网站和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没有执行。

3 个答案:

答案 0 :(得分: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;
   }
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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