PHP code example of jamband / yii2-ensure-unique-behavior
1. Go to this page and download the library: Download jamband/yii2-ensure-unique-behavior library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jamband / yii2-ensure-unique-behavior example snippets
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
],
];
}
}
$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_"