horat1us/yii2-carbon-behavior

Yii2 Carbon 时间戳行为

1.2.0 2022-10-28 13:57 UTC

This package is auto-updated.

Last update: 2024-08-30 01:49:38 UTC


README

Test & Lint codecov

使用 Carbon 实例 生成时间戳(created_at, updated_at 等)的行为。

使用 Carbon(而不是原生 time() 函数,在 \yii\behaviors\TimestampBehavior 中实现)的主要目的是能够轻松模拟这些值(使用 Carbon::setTestNow())。

在真实项目中使用 MySQL 和 PostgreSQL。

安装

使用 packagist.org

composer require horat1usyii2-carbon-behavior:^1.0

使用

使用 Carbon 生成时间戳

<?php

namespace App;

use Horat1us\Yii\CarbonBehavior;
use yii\db;

/**
* Class Record
 * @package App
 * 
 * @property string $created_at [timestamp]
 * @property string $updated_at [timestamp]
 */
class Record extends db\ActiveRecord
{
    public function behaviors(): array {
        return [
            'uuid' => [
                'class' => CarbonBehavior::class,    
            ],    
        ];
    }
}

在以下示例中,created_at 和 updated_at 属性将使用 Carbon 类生成的日期和时间进行填充。

许可证

MIT