horat1us / yii2-carbon-behavior
Yii2 Carbon Timestamps Behavior
Installs: 1 666
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=7.4
- nesbot/carbon: ^1.36 | ^2.0
- yiisoft/yii2: ^2.0.47
Requires (Dev)
- horat1us/yii2-asset-free: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-11-04 22:04:16 UTC
README
Behavior to generate timestamps (created_at, updated_at etc.) using Carbon instance.
Main purpose of using Carbon (instead of native time()
function, implemented in \yii\behaviors\TimestampBehavior
)
is ability to mock this values without tricks (using Carbon::setTestNow()
).
Used in real projects with MySQL and PostgreSQL.
Installation
Using packagist.org:
composer require horat1usyii2-carbon-behavior:^1.0
Usage
To generate timestamps using 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, ], ]; } }
in followed example created_at and updated_at attributes will be filled with date and time generated using Carbon class.