@YahorDanchanka
Frontend developer

Как вынести общие столбцы миграции в Yii2?

Я имею две миграции:
(1):

<?php

use yii\db\Migration;

class m211025_183630_create_user_table extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%user}}', [
            'id' => $this->primaryKey(),
            'email' => $this->string()->notNull()->unique(),
            'password_hash' => $this->string()->notNull(),
            'auth_key' => $this->string()->notNull(),

            'created_by' => $this->integer()->null(),
            'updated_by' => $this->integer()->null(),
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'updated_at' => $this->integer()->notNull(),
            'created_at' => $this->integer()->notNull(),
        ], Yii::$app->params['tableOptions']);
        $this->addForeignKey(
            'fk-user-created_by',
            'user',
            'created_by',
            'user',
            'id',
            'SET NULL',
            'CASCADE'
        );
        $this->addForeignKey(
            'fk-user-updated_by',
            'user',
            'updated_by',
            'user',
            'id',
            'SET NULL',
            'CASCADE'
        );
    }

    public function safeDown()
    {
        $this->dropForeignKey('fk-user-created_by', 'user');
        $this->dropForeignKey('fk-user-updated_by', 'user');
        $this->dropTable('{{%user}}');
    }
}

(2):
<?php

use yii\db\Migration;

class m211025_191412_create_post_table extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%post}}', [
            'id' => $this->primaryKey(),
            'title' => $this->string()->notNull(),
            'content' => $this->text()->null(),
            'image' => $this->string()->null(),

            'created_by' => $this->integer()->null(),
            'updated_by' => $this->integer()->null(),
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'updated_at' => $this->integer()->notNull(),
            'created_at' => $this->integer()->notNull(),
        ], Yii::$app->params['tableOptions']);
        $this->addForeignKey(
            'fk-post-created_by',
            'post',
            'created_by',
            'user',
            'id',
            'SET NULL',
            'CASCADE'
        );
        $this->addForeignKey(
            'fk-post-updated_by',
            'post',
            'updated_by',
            'user',
            'id',
            'SET NULL',
            'CASCADE'
        );
    }

    public function safeDown()
    {
        $this->dropForeignKey('fk-post-created_by', 'post');
        $this->dropForeignKey('fk-post-updated_by', 'post');
        $this->dropTable('{{%post}}');
    }
}


Есть ли адекватный способ вынести столбцы "created_by", "updated_by", "status", "updated_at", "created_at", если они будут использоваться в каждой таблице?
  • Вопрос задан
  • 58 просмотров
Решения вопроса 1
vitaly_74
@vitaly_74
создайте класс MigrationWithDates - переопределите метод createTable
который будет вставлять в каждую таблицу created_at и т.д.
class MigrationWithDates  extends Migrations{
    public function createTable($tableNane, array $data){
        $addDates = [
           'created_by' => $this->integer()->null(),
            'updated_by' => $this->integer()->null(),
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'updated_at' => $this->integer()->notNull(),
            'created_at' => $this->integer()->notNull(),
            ];
            $mergedData = array_merge ($data, $addDates );
            parent::createtable($tableName, $mergedData);
          }
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@FernandoErrNando
Как вариант, можно попытаться переписать компонент миграции и пользоваться своим
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы