Migrate Full (ok)

Ex 1

C:\xampp74\htdocs\oectest\migrations\m220724_073246_create_tables.php

<?php
use yii\db\Migration;

/**
 * Class m220724_073246_create_tables
 */
class m220724_073246_create_tables extends Migration {
	/**
	 * {@inheritdoc}
	 */
	public function up() {
		$this->createTable('user', [
			'id' => $this->primaryKey(),
			'username' => $this->string(55)->notNull(),
			'password' => $this->string(255)->notNull(),
			'auth_key' => $this->string(255)->notNull(),
			'access_token' => $this->string(255)->notNull(),
		]);
		$this->createTable('posts', [
			'id' => $this->primaryKey(),
			'title' => $this->string(125)->defaultValue(null),
			'excerpt' => $this->string(125)->defaultValue(null),
			'content' => $this->text()->defaultValue(null),
			'category' => $this->string(45)->defaultValue(null),
			'tags' => $this->string(125)->defaultValue(null),
			'author' => $this->string(65)->defaultValue(null),
			'slug' => $this->string(125)->defaultValue(null),
			'createdOn' => $this->timestamp(),
			'featuredImage' => $this->string(125)->defaultValue(null),
			'published' => $this->integer(11),
			'comments' => $this->integer(11)->defaultValue(null),
			'likes' => $this->integer()->defaultValue(0),
			'deleted' => $this->integer()->defaultValue(0),
		]);
    $this->createTable('post_comment', [
      'id' => $this->primaryKey(),
      'post_id' => $this->integer(11),
      'user' => $this->string(65)->defaultValue(null),
      'comment' => $this->text()->defaultValue(null),
      'createdOn' => $this->timestamp()
    ]);
	}
	/**
	 * {@inheritdoc}
	 */
	public function down() {
		$this->dropTable('user');
		$this->dropTable('posts');
	}
}

Ex 2

C:\xampp74\htdocs\oectest\migrations\m220724_073246_create_tables.php

<?php
use yii\db\Migration;
use yii\db\Schema;
/**
 * Class m220724_073246_create_tables
 */
class m220724_073246_create_tables extends Migration
{
  /**
   * {@inheritdoc}
   */
  public function up()
  {
    // MySql table options
    $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
    // table blog_category
    $this->createTable(
      '{{%blog_category}}',
      [
        'id' => Schema::TYPE_PK,
        'parent_id' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',
        'title' => Schema::TYPE_STRING . '(255) NOT NULL',
        'slug' => Schema::TYPE_STRING . '(128) NOT NULL',
        'banner' => Schema::TYPE_STRING . '(255) ',
        'is_nav' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
        'sort_order' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 50',
        'page_size' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 10',
        'template' => Schema::TYPE_STRING . '(255) NOT NULL DEFAULT "post"',
        'redirect_url' => Schema::TYPE_STRING . '(255) DEFAULT NULL',
        'status' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
        'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
        'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL'
      ],
      $tableOptions
    );
    // Indexes
    $this->createIndex('is_nav', '{{%blog_category}}', 'is_nav');
    $this->createIndex('sort_order', '{{%blog_category}}', 'sort_order');
    $this->createIndex('status', '{{%blog_category}}', 'status');
    $this->createIndex('created_at', '{{%blog_category}}', 'created_at');
    // table blog_post
    $this->createTable(
      '{{%blog_post}}',
      [
        'id' => Schema::TYPE_PK,
        'category_id' => Schema::TYPE_INTEGER . ' NOT NULL',
        'title' => Schema::TYPE_STRING . '(255) NOT NULL',
        'brief' => Schema::TYPE_TEXT,
        'content' => Schema::TYPE_TEXT . ' NOT NULL',
        'tags' => Schema::TYPE_STRING . '(255) NOT NULL',
        'slug' => Schema::TYPE_STRING . '(128) NOT NULL',
        'banner' => Schema::TYPE_STRING . '(255) ',
        'click' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',
        'user_id' => Schema::TYPE_INTEGER . '',
        'status' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
        'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
        'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL'
      ],
      $tableOptions
    );
    // Indexes
    $this->createIndex('category_id', '{{%blog_post}}', 'category_id');
    $this->createIndex('status', '{{%blog_post}}', 'status');
    $this->createIndex('created_at', '{{%blog_post}}', 'created_at');
    // table blog_comment
    $this->createTable(
      '{{%blog_comment}}',
      [
        'id' => Schema::TYPE_PK,
        'post_id' => Schema::TYPE_INTEGER . ' NOT NULL',
        'content' => Schema::TYPE_TEXT . ' NOT NULL',
        'author' => Schema::TYPE_STRING . '(128) NOT NULL',
        'email' => Schema::TYPE_STRING . '(128) NOT NULL',
        'url' => Schema::TYPE_STRING . '(128) NULL',
        'status' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
        'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
        'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL'
      ],
      $tableOptions
    );
    // Indexes
    $this->createIndex('post_id', '{{%blog_comment}}', 'post_id');
    $this->createIndex('status', '{{%blog_comment}}', 'status');
    $this->createIndex('created_at', '{{%blog_comment}}', 'created_at');
    // Foreign Keys
    $this->addForeignKey('{{%FK_comment_post}}', '{{%blog_comment}}', 'post_id', '{{%blog_post}}', 'id', 'CASCADE', 'CASCADE');
    // table blog_tag
    $this->createTable(
      '{{%blog_tag}}',
      [
        'id' => Schema::TYPE_PK,
        'name' => Schema::TYPE_STRING . '(128) NOT NULL',
        'frequency' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
      ],
      $tableOptions
    );
    // Indexes
    $this->createIndex('frequency', '{{%blog_tag}}', 'frequency');
  }
  /**
   * {@inheritdoc}
   */
  public function down()
  {
    $this->dropTable('user');
    $this->dropTable('posts');
  }
}

Last updated

Was this helpful?