kartavik/yii2-enum Yii2 enum mapping behavior / validator / database column type

993323

Enum integration for Yii Framework 2.0

  1. Installation
  2. Usage
  3. Suggest
  4. Author
  5. License

Yii2 enum

  1. Installation
  2. Usage
  3. Suggest
  4. Author
  5. License

Latest Stable Version Build Status codecov

Based on myclabs\php-enum package;

Installation

composer require kartavik/yii2-enum

Usage

Example enum:

<?php

class YourEnum extends \MyCLabs\Enum\Enum
{
    public const FIRST_VALUE = 'first';
    public const SECOND_VALUE = 'second';
}
Validator

This validator used MyCLabs\Enum\Enum::isValid($value) method and also checked value on instance of your enum;

<?php

use YourEnum;
use Kartavik\Yii2;

/**
 * @property YourEnum $attribute1
 * @property YourEnum $attribute2
 */
class Record extends \yii\db\ActiveRecord
{
    public function behaviors(): array
    {
        return [
            'enum' => [
                'class' => Yii2\Behaviors\EnumMappingBehavior::class,
                'map' => [
                    'attribute1' => YourEnum::class,
                    'attribute2' => YourEnum::class,
                ],
                // Set attribute type if need explicitly specify it
                'attributesType' => [
                    'attribute1' => 'integer',
                    'attribute2' => 'float'
                ],
            ]
        ];
    }
    
    public function rules(){
        return [
            [
                ['attribute1', 'attribute2'],
                Yii2\Validators\EnumValidator::class,
                'targetEnum' => YourEnum::class,
            ]
        ];
    }
}

$record = new Record([
    'attribute1' => YourEnum::FIRST_VALUE, // use constant
    'attribute2' => YourEnum::SECOND_VALUE(), // use method
]);

$record->trigger(Yii2\Behaviors\EnumMappingBehavior::EVENT_TO_ENUMS); // trigger if you put values not instance of Enum

$record->validate(); // will return true
Behavior

It can be used for classes that extends ActiveRecord

<?php

use YourEnum;

/**
 * @property YourEnum $attribute1
 * @property YourEnum $attribute2
 */
class Record extends \yii\db\ActiveRecord
{
    public function behaviors(): array
    {
        return [
            'enum' => [
                'class' => \Kartavik\Yii2\Behaviors\EnumMappingBehavior::class,
                'map' => [
                    'attribute1' => YourEnum::class,
                    'attribute2' => YourEnum::class,
                ]
            ]
        ];
    }
}

$record = new Record([
    'attribute1' => YourEnum::FIRST_VALUE, // use const
    'attribute2' => YourEnum::SECOND_VALUE() // use method
]);

$this->trigger(\Kartavik\Yii2\Behaviors\EnumMappingBehavior::EVENT_TO_ENUMS);
$record->save(); // will return true

$record = Record::find()->where(['id' => $record->id])->all()[0];

print_r($record->attribute1); // Will output YourEnum object with value `first`

Record::updateAll(['attribute1' => YourEnum::SECOND_VALUE()]); // Updating records with new enum

$record = Record::find()->where(['id' => $record->id])->all()[0];

print_r($record->attribute1); // Will output YourEnum object with value `second`
Migration
PgSql
<?php

use Kartavik\Yii2\Database\Pgsql;

class CreateRecordTable extends Pgsql\Migration
{
    // You can use trait do receive all methods for enum
    use Pgsql\MigrationTrait;
    
    public function safeUp()
    {
         // simple usage
        $this->addEnum('test_enum_from_array', [/** your values */]);
         // use your enum that extends MyCLabs\Enum\Enum
         // use it for package, for big project this usage is not good practice
        $this->addEnum('test_enum_from_enum', YourEnum::class);
        
         // if enum already exists
        $column = $this->enum('test_enum_from_array');
         // you can do not to use addEnum to create it
        $column = $this->enum('test_enum_dynamic_from_array', [/** your values */]);
         // use your enum that extends MyCLabs\Enum\Enum
         // use it for package, for big project this usage is not good practice
        $column = $this->enum('test_enum_dynamic_from_enum', YourEnum::class);
        
        $this->createTable('record', [
            'enum' => $column->null()->default('default')
        ]);
    }
}
MySql
<?php

use Kartavik\Yii2\Database\Mysql;

class CreateRecordTable extends Mysql\Migration
{
    // You can use trait do receive all methods for enum
    use Mysql\MigrationTrait;
    
    public function safeUp()
    {
        $this->createTable('record', [
            'enum_column_from_array' => $this->enum(['1', '2', '3', '4']),
            // use your enum that extends MyCLabs\Enum\Enum
            // use it for package, for big project this usage is not good practice
            'enum_column_from_enum' => $this->enum(YourEnum::class),
        ]);
    }
}

Suggest

Author

License

MIT

0 0
1 follower
159 downloads
Yii Version: 2.0
License: MIT
Category: Validation
Developed by: Roman Varkuta
Created on: May 15, 2019
Last updated: (not set)
Packagist Profile
Github Repository

Related Extensions