Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jamband/yii2-ensure-unique-behavior
Insert unique identifier automatically for the Yii 2 framework
https://github.com/jamband/yii2-ensure-unique-behavior
php yii yii2-extension
Last synced: 6 days ago
JSON representation
Insert unique identifier automatically for the Yii 2 framework
- Host: GitHub
- URL: https://github.com/jamband/yii2-ensure-unique-behavior
- Owner: jamband
- License: mit
- Archived: true
- Created: 2016-01-19T06:21:05.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2022-05-26T17:51:54.000Z (over 2 years ago)
- Last Synced: 2024-12-14T01:20:06.668Z (about 1 month ago)
- Topics: php, yii, yii2-extension
- Language: PHP
- Homepage:
- Size: 126 KB
- Stars: 1
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# yii2-ensure-unique-behavior
[![Build Status](https://github.com/jamband/yii2-ensure-unique-behavior/workflows/tests/badge.svg)](https://github.com/jamband/yii2-ensure-unique-behavior/actions?workflow=tests) [![Latest Stable Version](https://img.shields.io/packagist/v/jamband/yii2-ensure-unique-behavior)](https://packagist.org/packages/jamband/yii2-ensure-unique-behavior) [![Total Downloads](https://img.shields.io/packagist/dt/jamband/yii2-ensure-unique-behavior)](https://packagist.org/packages/jamband/yii2-ensure-unique-behavior)
Insert unique identifier automatically for the Yii 2 framework.
## Requirements
* PHP 7.4 or later
* Yii 2.x## Installation
```
composer require jamband/yii2-ensure-unique-behavior
```## Examples
Creates a post table:
```sql
CREATE TABLE `post` (
`id` CHAR(11) COLLATE utf8_bin NOT NULL,
`title` VARCHAR(255) NOT NULL,
`content` TEXT NOT NULL,
`created_at` INT(11) NOT NULL,
`updated_at` INT(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
```Settings EnsureUniqueBehavior in Model:
```php
namespace app\models;use jamband\behaviors\EnsureUniqueBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;class Post extends ActiveRecord
{
public function behaviors()
{
return [
TimestampBehavior::class,
[
'class' => EnsureUniqueBehavior::class,
'attribute' => 'id', // default
'length' => 11, // default
],
];
}
}
```And saves a new model:
```php
$model = new \app\models\Post();
$model->title = 'title';
$model->content = 'content';
$model->save();// This value is eusure uniqueness
var_dump($model->id); // string(11) "-ZRLSS-4vl_"
```## License
This extension is licensed under the MIT license.