【问题标题】:Laravel: how to seed Baum's nested set?Laravel:如何播种 Baum 的嵌套集?
【发布时间】:2016-06-04 20:14:38
【问题描述】:

我想播种我的一张嵌套表。我正在使用 Baum 的嵌套集,它提供了一种查看嵌套集的简单方法:https://github.com/etrepat/baum#seeding

如何使用来自/database/seeds/ 的 Baum?根据 Laravel 文档,我需要扩展 Seeder 类才能使播种工作。但是,为了使用 Baum 的方法,我需要扩展 Baum\Node。

建议?

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    哦,好吧,如果您可以直接使用 Baum 模型并且可以满足您的目的,我建议您使用它。这样您就可以轻松访问 baum 的功能。我直接在种子中使用我的 Baum 模型,而不扩展播种器类。 这是一个小sn-p:

    class BusinessUnitTableSeeder extends Seeder
        {
            public function run()
            {
                $admin = DB::table('user')->where('login_name', '=', 'superadmin')->pluck('id');
                $root = BusinessUnit::create([
                        'name' => 'Headquarters',
                        'created_by' => $admin,
                        'updated_by' => $admin,
                        'owned_by'   => $admin
                    ]);
                $user = \User::find($admin);
                $user->business_unit = $root->id;
                $user->save();
                $child1 = $root->children()->create([
                        'name' => 'Business unit 1',
                        'created_by' => $admin,
                        'updated_by' => $admin,
                        'owned_by'   => $admin
                ]);
                $child2 = $child1->children()->create([
                        'name' => 'Business unit 2',
                        'created_by' => $admin,
                        'updated_by' => $admin,
                        'owned_by'   => $admin
                ]);
    
    
            }
        }
    

    UPD:要了解在哪里放置种子以及如何调用主题,请直接参考文档here。制作模型非常快。所以我建议你这样做,它会导致更少的动作做进一步的事情。模型必须放在models目录下,模型扩展Baum的快速sn-p会是

    class YourModel extends extends Baum\Node
    {
     /**
       * Column name to store the reference to parent's node.
       *
       * @var string
       */
      protected $parentColumn = 'parent_id';
    
      /**
       * Column name for left index.
       *
       * @var string
       */
      protected $leftColumn = 'lft';
    
      /**
       * Column name for right index.
       *
       * @var string
       */
      protected $rightColumn = 'rgt';
    
      /**
       * Column name for depth field.
       *
       * @var string
       */
      protected $depthColumn = 'depth';
    
    /**
             * Table name.
             *
             * @var string
             */
            protected $table = 'your_baum_table';
    
    }
    

    然后直接在你的种子中使用它。就这些 。 此外,我向应用程序添加种子的首选方式是使用迁移:

    1. 创建种子
    2. 创建迁移
    3. 在函数up() 中添加Artisan::call('db:seed',['--class'=> 'YourDesiredSeed'])。

    这样我可以在已经运行的应用上添加演示或真实数据。

    【讨论】:

    • 你能告诉我包含这个方法的类是如何定义的吗?那么,这个run()方法是怎么调用的呢?
    • 请记住,我是 Laravel 的新手。我正在学习如何为数据库播种并使用php artisan db:seed 命令为我的数据库播种。我已经完成了我的迁移文件,目前正在做种子。我还没有做过任何模型。
    • 我已为我的答案添加了更新。请随时提问
    • 您有关于第 3 步的文档吗?这应该调用 Baum 模型中的方法吗?
    • 不,这是一个简单的迁移文件,正如您所提到的,您已经执行过了。将其视为一个新的迁移文件;)
    【解决方案2】:

    我用于我的产品

    我只是使用带有 baum 中使用的变量的数组来完成。

    我使用以下方法创建了一个模型:

    php artisan baum:安装产品

    这将创建一个模型(我更改为 app 文件夹)和迁移。 只需更新迁移文件:

    <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateProductsTable extends Migration {
    
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up() {
        Schema::create('products', function(Blueprint $table) {
          // These columns are needed for Baum's Nested Set implementation to work.
          // Column names may be changed, but they *must* all exist and be modified
          // in the model.
          // Take a look at the model scaffold comments for details.
          // We add indexes on parent_id, lft, rgt columns by default.
          $table->increments('id');
          $table->integer('parent_id')->nullable()->index();
          $table->integer('lft')->nullable()->index();
          $table->integer('rgt')->nullable()->index();
          $table->integer('depth')->nullable();
    
          // Add needed columns here (f.ex: name, slug, path, etc.)
          // $table->string('name', 255);
    
          $table->string('name',500);
          $table->string('slug');
          $table->string('brand',255);
          $table->string('sku',255)->unique();
          $table->string('description',1000);
          $table->integer('quantity');
          $table->decimal('price',6,2);
          $table->boolean('badge',2);
          $table->string('image1',255);
          $table->boolean('visible',2);
          $table->string('created_at');
          $table->string('updated_at');
    
          });
      }
    
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down() {
        Schema::drop('products');
      }
    
    }
    

    然后做你的种子,例如使用。

    use App\Product;
    use Baum\Node;
    use Illuminate\Database\Seeder;
    
    class ProductTableSeeder extends Seeder
    {
       public function run()
        {
            $data = array(
                [
                    'name'          => 'product1',
                    'slug'          => 'slug blablabla',
                    'brand'         => 'brand',
                    'sku'           => 'xxxxxx',
                    'description'   => 'Lorem ipsum dolor sit amet',
                    'quantity'      => 100,
                    'price'         => 275.00,
                    'badge'         => 1,
                    'image1'        => 'xxx.jpeg',
                    'visible'       => 1,
                    'created_at'    => new DateTime,
                    'updated_at'    => new DateTime,
                ],
            );
            Product::insert($data);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 2018-05-26
      • 2019-05-18
      • 2021-11-04
      • 2020-02-21
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多