Seeder第一次运行良好,但是在Laravel 5的后续循环中没有执行某些任务吗? [英] Seeder runs fine the first time but does not do some tasks in the subsequent loops in Laravel 5?

查看:33
本文介绍了Seeder第一次运行良好,但是在Laravel 5的后续循环中没有执行某些任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个多租户多数据库应用程序,其中有一个主要的DB,它由一个租户表组成,然后为每个创建的租户创建一个新的DB.我为此编写了一个种子文件,并运行了3次以创建3个租户,并为它们创建3个DB.

I am working on a multi tenant multi database application where I have a main DB which consists of a tenant table and then for each tenant that is created, there is a new DB created. I am writing a seed file for this and I am running it 3 times to create 3 Tenants and subsequent 3 DB's for them.

第一次运行良好.在主DB中创建一个新的租户.使用新租户的username创建一个新的DB.通过代码中的Artisan命令调用,DB也是migrated.但是在接下来的2个循环中,将在主DB中创建租户,并同时为他们创建新的DB,但是Artisan命令调用不会迁移DB的租户.

It runs fine the first time. A new tenant is created in the main DB. A new DB is created using the username of the new tenant. The DB is also migrated with the Artisan command call from the code. But in the next 2 loops, the tenants are created in the main DB and new DB's are also created for them but the Artisan command call does not migrate the DB's.

public function run()
{
    $faker = Faker::create();

    // Fetching all tenants
    $tenants = App\Tenant::all();

    // Deleting their databases
    $tenants->each(function($tenant){
        DB::statement('DROP DATABASE IF EXISTS ' . $tenant->username);
    });

    // Truncating the tenants table itself
    DB::table('tenant')->truncate();

    for($i = 0; $i < 3; $i++){

        $company = $faker->company();
        $description = $faker->text();
        $logo = $faker->imageUrl(50, 50);
        $username = str_random(8);

        \Config::set('database.default', 'archive');

        echo 'Creating tenant ' . $i . "\r\n";

        Tenant::create([
            'name'          => $company,
            'description'   => $description,
            'logo'          => $logo,
            'username'      => $username,
        ]);

        DB::statement('CREATE DATABASE ' . $username);

        \Config::set('database.connections.tenant.database', $username);
        \Config::set('database.default', 'tenant');

        echo 'Migrating tenant ' . $i . "\r\n";

        \Artisan::call('migrate', [
            '--path' => 'database/migrations/tenants'
        ]);

        echo "\r\n";
    }
}

我在这里做错了什么.第一次完美运行.然后最后两次仅创建DB,但不迁移.从此仅成功迁移了第一个DB.而且工匠不会抛出任何错误.

What am I doing wrong here. It runs perfectly the first time. And then the last two times only the DB's are created but are not migrated. Only the first DB is migrated successfully from this. And no error is thrown from artisan.

命令行输出如下:

Creating tenant 0

Migrating tenant 0


Creating tenant 1

Migrating tenant 1


Creating tenant 2

Migrating tenant 2

推荐答案

数据库连接在Laravel中保持活动状态,这意味着当您通过config更改数据库名称时,它不会切换到新数据库.您需要通过DB::connection($connection)->reconnect()强制重新连接后台连接.

The database connection is kept alive in Laravel, which means that it will not switch to new database when you change the database name via config. You need to force the background connection to be reconnected by DB::connection($connection)->reconnect().

在运行迁移时,由于Laravel使用数据库名和表名来检查migrations表的存在,因此,您还需要更新当前连接的表名.

When running migrations, because Laravel use the database name and the table name to check the existence of the migrations table, you need to update the table name of the current connection as well.

在您的情况下:

# ...
\Config::set('database.connections.tenant.database', $username);
\Config::set('database.default', 'tenant');

\DB::reconnect();
\DB::setDatabaseName($username);
# ...

这篇关于Seeder第一次运行良好,但是在Laravel 5的后续循环中没有执行某些任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆