PHP code example of illusiard / yii2-db-toolkit

1. Go to this page and download the library: Download illusiard/yii2-db-toolkit 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/ */

    

illusiard / yii2-db-toolkit example snippets


use illusiard\dbtoolkit\components\CreateTable;

final class m240101_000001_create_post_table extends CreateTable
{
    public function getTableName(): string
    {
        return 'post';
    }

    public function getColumns(): array
    {
        return [
            'title' => $this->string(255)->notNull()->comment('Заголовок'),
            'body' => $this->text()->null()->comment('Текст'),
        ];
    }

    public function getIndexes(): array
    {
        return [
            ['title'],
        ];
    }

    public function getForeignKeys(): array
    {
        return [
            'user_id' => ['user', 'id', 'onDelete' => 'RESTRICT', 'onUpdate' => 'CASCADE'],
        ];
    }
}

public function getColumns(): array
{
    return [
        'id' => false,
        'created_at' => false,
        'updated_at' => false,
        'is_active' => false,

        'code' => $this->string(64)->notNull()->comment('Код'),
    ];
}

use illusiard\dbtoolkit\components\BaseDictionaryCreateTableMigration;

final class m240101_000002_create_country_dict extends BaseDictionaryCreateTableMigration
{
    public function getTableName(): string
    {
        return 'dict_country';
    }
}

use illusiard\dbtoolkit\schema\SchemaIntrospector;

$introspector = new SchemaIntrospector($db);

$schema = $introspector->introspectSchema();
$table = $introspector->introspectTable('post');

$desired = [
    'tables' => [
        [
            'name' => 'device',
            'primaryKey' => ['id'],
            'columns' => [
                ['name' => 'id', 'type' => 'uuid', 'allowNull' => false],
                ['name' => 'status', 'type' => 'string(32)', 'defaultValue' => 'draft'],
            ],
            'indexes' => [
                ['columns' => ['status'], 'unique' => false],
            ],
            'foreignKeys' => [
                [
                    'columns' => 'tenant_id,author_id',
                    'refTable' => 'author',
                    'refColumns' => ['tenant_id', 'id'],
                    'onDelete' => 'CASCADE',
                    'onUpdate' => 'NO ACTION',
                ],
            ],
        ],
    ],
];