t3mnikov / yii2-ensure-unique-behavior

Insert unique identifier automatically for the Yii 2 framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

yii2-ensure-unique-behavior

Latest Stable Version Total Downloads Latest Unstable Version License Build Status

Insert unique identifier automatically for the Yii 2 framework.

Requirements

  • PHP 7.2 or later
  • Yii 2.x

Installation

composer require jamband/yii2-ensure-unique-behavior

Examples

Creates a post table:

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:

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:

$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.

About

Insert unique identifier automatically for the Yii 2 framework

License:MIT License


Languages

Language:PHP 100.0%