PHP code example of lav45 / yii2-db-migrate

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

    

lav45 / yii2-db-migrate example snippets


use lav45\db\MainMigration;

class m000000_000000_init extends MainMigration
{
    // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
    public $defaultTableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';

    public function table_news()
    {
        $this->createTable('news', [
            'id' => $this->primaryKey(),
            'author_id' => $this->integer()->notNull(),
            // ...
        ]);
        
        $this->addForeignKey('news', 'author_id', 'user', 'id', 'SET NULL');
    }
    
    public function table_news_tag()
    {
        $this->createTable('news_tag', [
            'news_id' => $this->integer()->notNull(),
            'tag_id' => $this->integer()->notNull(),
        ]);

        $this->addPrimaryKey('news_tag', ['news_id', 'tag_id']);

        $this->addForeignKey('news_tag', 'news_id', 'news', 'id');
        $this->addForeignKey('news_tag', 'tag_id', 'tag', 'id');
    }

    public function table_tag()
    {
        $this->createTable('tag', [
            'id' => $this->primaryKey(),
            // ...
        ]);
    }

    public function table_user()
    {
        $this->createTable('user', [
            'id' => $this->primaryKey(),
            // ...
        ]);
    }
}