eluhr / yii2-uuid-attribute-behavior
This behavior automatically generates a UUID for the given attribute on create.
Installs: 1 859
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.4
- ramsey/uuid: ^4.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2024-10-22 16:26:31 UTC
README
This behavior automatically generates a UUID for the given attribute on create.
Installation
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist eluhr/yii2-uuid-attribute-behavior "*"
or add
"eluhr/yii2-uuid-attribute-behavior": "*"
to the require
section of your composer.json
file.
Usage
In a yii\base\Model
or a derivation thereof, the behavior can be used as follows:
public function behaviors(): array { $behaviors = parent::behaviors(); $behaviors['uuid-attribute'] = [ 'class' => eluhr\uuidAttributeBehavior\UuidAttributeBehavior::class ]; return $behaviors; }
By using this behavior it does not matter if the attribute is a string or an array. The behavior will always ensure, that the attribute is an array before saving the data to the database and yii will handle the rest.
This behavior supports i18n. By adding the uuid-attribute-validator
category in your config you can overwrite the default error messages.
Testing
After installing dependencies via composer you can run the tests with:
make test