symfony 在Laravel中播种数据库时使用进度条

xiozqbni  于 2023-08-06  发布在  其他
关注(0)|答案(5)|浏览(93)

我必须种子相当多的数据到一个数据库,我希望能够向用户显示一个进度条,而这种情况发生。我知道这是有记录的:

但我不想把它放进播种机里

<?php

use Illuminate\Database\Seeder;

class SubDivisionRangeSeeder extends Seeder
{
    public function run()
    {
        $this->output->createProgressBar(10);
        for ($i = 0; $i < 10; $i++) {
            sleep(1);
            $this->output->advance();
        }
        $this->output->finish();
    }
}

字符串
或者是

<?php

use Illuminate\Database\Seeder;

class SubDivisionRangeSeeder extends Seeder
{
    public function run()
    {
        $this->output->progressStart(10);
        for ($i = 0; $i < 10; $i++) {
            sleep(1);
            $this->output->progressAdvance();
        }
        $this->output->progressFinish();
    }
}


关于https://mattstauffer.co/blog/advanced-input-output-with-artisan-commands-tables-and-progress-bars-in-laravel-5.1
有什么想法吗

gijlo24d

gijlo24d1#

您可以通过$this->command->getOutput()访问输出

public function run()
{
    $this->command->getOutput()->progressStart(10);
    for ($i = 0; $i < 10; $i++) {
        sleep(1);
        $this->command->getOutput()->progressAdvance();
    }
    $this->command->getOutput()->progressFinish();
}

字符串

zf9nrax1

zf9nrax12#

下面是我的实现代码

<?php

use Illuminate\Database\Seeder;

class MediaTypeTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $media_types = [
            ['name'=>'movie'],
            ['name'=>'channel'],
            ['name'=>'drama'],
            // ['name'=>'ebook'],
            // ['name'=>'shopping'],
            // ['name'=>'reseller'],
            // ['name'=>'broadcasting']
        ];
        $this->command->getOutput()->progressStart(count($media_types));
        App\Models\Backoffice\MediaType::query()->delete();
        foreach($media_types as $media_type){
            App\Models\Backoffice\MediaType::create($media_type);
            $this->command->getOutput()->progressAdvance();
        }
        $this->command->getOutput()->progressFinish();
    }
}

字符串

rta7y2nd

rta7y2nd3#

你的代码不能工作,因为你在错误的类中使用了$this->output。如果您再次查看您分享的文章,$this-output用于Artisan命令类中。
事实上,每个Artisan命令都是使用Symfony Console组件执行的。
你实际上是想在数据库播种器中使用它:)
我的建议是:构建您的seeder,然后根据您的需要使用“install”或“seed”自定义命令调用它们。

rxztt3cl

rxztt3cl4#

为了防止其他人遇到类似的情况并正在寻找解决方案,我创建了以下基类:

<?php

use Illuminate\Database\Schema\Blueprint;

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    protected $progress = 0;

    protected function outputInfo($text)
    {
        $this->output('info', $text, 34);
    }

    protected function outputSuccess($text)
    {
        $this->output('success', $text, 32);
    }

    protected function outputWarning($text)
    {
        $this->output('warning', $text, 33);
    }

    protected function outputError($text)
    {
        $this->output(strtoupper('error'), $text, 31);
    }

    private function output($status, $text, $colour_code = 34)
    {
        $this->newLine();
        print "\033[1;" . $colour_code . "m" . str_pad($status.':', 8) . " " . $text . " \033[0m";
    }

    protected function progressDots()
    {
        if ($this->progress % 20 == 0) {
            print '.';
        }
    }

    protected function newLine()
    {
        echo "\n";
    }

    /**
     * Prevents a warning on the command line
     */
    public function testDefault()
    {
        $this->assertTrue(true);
    }
}

字符串
然后用它来做这样的事。。

<?php

class SomethingTests extends TestCase
{

    public function testSomething()
    {
        $array = range(0, 100);

        foreach ($array as $value) {

            $this->outputInfo('Testing '.$value);

            $this->assertGreaterThan(101,$value,'101 is not greater then '.$value);

            $this->progressDots();

        }

        $this->newLine();

    }

}

hgqdbh6s

hgqdbh6s5#

<?php    
    use Illuminate\Database\Seeder;
    use Symfony\Component\Console\Helper\ProgressBar;
    use Symfony\Component\Console\Output\ConsoleOutput;
    
    class UsersTableSeeder extends Seeder
    {
        /**
         * Amount.
         *
         * @var int
         */
        private $amount = 100000;
    
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            // The output
            $output = new ConsoleOutput();
    
            // creates a new progress bar (50 units)
            $progressBar = new ProgressBar($output, $this->amount);
    
            // starts and displays the progress bar
            $progressBar->start();
    
            factory(\App\User::class, $this->amount)->make()->each(function ($user) use ($progressBar) {
    
                // advances the progress bar 1 unit
                if ($user->save()) {
                    $progressBar->advance();
                }
    
                // you can also advance the progress bar by more than 1 unit
                // $progressBar->advance(3);
            });
    
            // ensures that the progress bar is at 100%
            $progressBar->finish();
    
            $output->write(' `enter code here`Finished', true);
        }
    }

字符串

相关问题