Laravel/Composer:以编程方式添加种子



目前,我正在尝试使用以下工作流程开发软件包更新系统。我正在创建一个包含以下文件的软件包(.zip):

  • 迁移(文件夹)
    • MigrationClass
    • ..
  • 种子
    • seedfile
    • ..
  • package.xml
  • UpdateInstructionseeder.php

作为管理员,我可以将此软件包上传到管理员控制面板中以更新我的数据库。

后端工作流程:

  1. 从package.xml获取数据(获取种子和迁移)
  2. 检查是否需要迁移/种子
  3. 迁移(正常工作)
  4. 种子(失败)

所以,正如您所看到的那样,我的播种遇到了一些麻烦。

首先,我尝试将种子从包装种子文件夹移动到database/seed/目录。我尝试用Artisan::call('db:seed','--class']);播种它,但是出现了Class MyClass does not exist错误。我猜想我的自动加载器有一些问题,所以我尝试将其倒入system('composer dump-autoload', $test);$test的输出为1,但autoload_classmap未更新。

现在,我已经添加了一个UpdateInstructionSeeder.php,默认情况下,该CC_9可以在我的框架中使用以解决自动加载问题。上传我的软件包后,我现在使用Storage::move()0和Storage::put()用所需的种子进行更新。

然后,我正在使用Artisan::call('make:seeder', ['name' => $className]);添加种子($ className是我的包装中的种子的名称),并使用Storage::get()Storage::put()更新它们。

最后,我用Artisan::call('db:seed','--class' => 'UpdateInstructionSeeder']);打电话给我的播种机。结果: Class MyClass does not exist

内容:

软件包updateinStructionseeder

use IlluminateDatabaseSeeder;
use IlluminateDatabaseEloquentModel;
class UpdateInstructionSeeder extends Seeder
{
   /**
    * Run the database seeds.
    *
    * @return void
    */
   public function run()
   {
       Model::unguard();
       $this->call(DemoTableSeeder::class);
       Model::reguard();
   }
}

软件包DemotableSeeder

<?php
use IlluminateDatabaseSeeder;
use IlluminateDatabaseEloquentModel;
class DemoTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        DB::table('demos')->insert([
            [
                'demoInt' => 1,
                'demoString' => "a"
            ],
            [
                'demoInt' => 11,
                'demoString' => "aa"
            ],
        ]);
        Model::reguard();
    }
}

我现在浪费了很多小时,我绝对不知道解决这个问题在程序上

尝试

php artisan optimize
php artisan cache:clear 
chmod -R guo+w storage
composer dump-autoload

然后最后

php artisan db:seed

您可以使用Artisan ::呼叫如下

,以编程方式调用所有这些命令。
Artisan::call('optimize', ['--quiet' => true, '--force' => true]);

编辑

制作一个具有以下内容的.sh文件,并使用php

运行它

optimize.sh


php artisan optimize
php artisan cache:clear 
chmod -R guo+w storage
composer dump-autoload

https://laravelcollactive.com/docs/5.0/ssh

最新更新