【发布时间】:2016-06-04 20:14:38
【问题描述】:
我想播种我的一张嵌套表。我正在使用 Baum 的嵌套集,它提供了一种查看嵌套集的简单方法:https://github.com/etrepat/baum#seeding
如何使用来自/database/seeds/ 的 Baum?根据 Laravel 文档,我需要扩展 Seeder 类才能使播种工作。但是,为了使用 Baum 的方法,我需要扩展 Baum\Node。
建议?
【问题讨论】:
我想播种我的一张嵌套表。我正在使用 Baum 的嵌套集,它提供了一种查看嵌套集的简单方法:https://github.com/etrepat/baum#seeding
如何使用来自/database/seeds/ 的 Baum?根据 Laravel 文档,我需要扩展 Seeder 类才能使播种工作。但是,为了使用 Baum 的方法,我需要扩展 Baum\Node。
建议?
【问题讨论】:
哦,好吧,如果您可以直接使用 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';
}
然后直接在你的种子中使用它。就这些 。 此外,我向应用程序添加种子的首选方式是使用迁移:
up() 中添加Artisan::call('db:seed',['--class'=> 'YourDesiredSeed'])。这样我可以在已经运行的应用上添加演示或真实数据。
【讨论】:
php artisan db:seed 命令为我的数据库播种。我已经完成了我的迁移文件,目前正在做种子。我还没有做过任何模型。
我用于我的产品
我只是使用带有 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);
}
}
【讨论】: