可以使用迁移来创建动态 table 名称吗?

Can migrations be used to create dynamic table names?

我想为每个用户创建一个新的 table。例如,当用户注册时,应创建名称为 'User-uniqueID' 的 table。这可以通过迁移来实现吗?或者我应该使用 stored procedure 吗?

是的,我通常使用这种方法进行动态 table 创建,见下文(在这种情况下,我为用户 region/land_scope_code 创建了一个 table:

class m150420_221138_test extends Migration
{
    public function up()
    {


        $tableOptions = 'CHARACTER SET utf8 ENGINE=InnoDB';

        $modelUserParam = UserParam::findOne(['user_id'=> Yii::$app->user->id]);


        $tableName = '_yourTableName';

        $this->createTable( $modelUserParam->land_scope_code . $tableName, [
            yourfield,
                ....... 
            'id' =>  'pk', 
        ], $tableOptions);

    }

    public function down()
    {

        $modelUserParam = UserParam::findOne(['user_id'=> Yii::$app->user->id]);


        $tableName = '_yourTableName';

        $this->droptable(  $modelUserParam->land_scope_code. $tableName);
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}