Laravel 4:处理种子中的关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14666277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:32:20  来源:igfitidea点击:

Laravel 4: Working with relationships in seeds

databasemany-to-manylaravellaravel-4

提问by AndHeiberg

Is there an easy way to manage many-to-many relationships in the new seeds feature of L4?

在 L4 的新种子功能中,是否有一种简单的方法来管理多对多关系?

One way would be to make a seed for the pivot table, but I would be a lot of work.

一种方法是为数据透视表制作种子,但我会做很多工作。

Any thoughts on a good workflow for this sort of thing?

关于此类事情的良好工作流程的任何想法?

回答by Beau

In the latest version of Laravel 4 you define the order that all the seeder scripts are run in the "run" method of the DatabaseSeeder class.

在最新版本的 Laravel 4 中,您可以在 DatabaseSeeder 类的“run”方法中定义所有播种脚本的运行顺序。

public function run()
{
    DB::statement('SET FOREIGN_KEY_CHECKS=0;');

    $this->call('PrimaryTableOneSeeder');
    $this->command->info('The first primary table has been seeded!');

    $this->call('PrimaryTableTwoSeeder');
    $this->command->info('The second primary table has been seeded!');

    $this->call('PivotTableSeeder');
    $this->command->info('The pivot table has been seeded!');

    DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}

You'll notice that I disable the foreign key constraints before and after running all my seeding. This may be bad practice but it's the only way I can use the truncate function to re-set the id count for each table. If you follow the guide on inserting related modelsthis practice may be unnecessary.

您会注意到我在运行所有种子之前和之后禁用了外键约束。这可能是不好的做法,但这是我可以使用 truncate 函数重新设置每个表的 id 计数的唯一方法。如果您按照插入相关模型的指南进行操作,则可能不需要这种做法。

class PrimaryTableOneSeeder extends Seeder {

public function run()
{
    DB::table('primaryone')->truncate();
    Primaryone::create(array(
        'field' => 'value',
        'created_at' => new DateTime,
        'updated_at' => new DateTime
    ));
}

To use mass assignmentas I'm doing in my example and as the latest version of the documentation does, you'll need to specify either some guarded or fillable columns for the model. To do this simply add property to your model like this:

要像我在示例中所做的那样使用质量分配以及最新版本的文档所做的那样,您需要为模型指定一些受保护的或可填充的列。为此,只需将属性添加到您的模型中,如下所示:

class Primaryone extends Eloquent {

protected $guarded = array('id');

回答by Max Ehsan

Laravel seed files are regular PHP scripts (except they need to return an array). You can query the database in seed files (using Eloquent, Fluent builder or even PDO).

Laravel 种子文件是常规的 PHP 脚本(除非它们需要返回一个数组)。您可以在种子文件中查询数据库(使用 Eloquent、Fluent builder 甚至 PDO)。

One way to tackle the many-to-many problem is to deliberately name your seed files so that the pivot table is populated last... For example, you could prepend a numeric value to the file name (i.e. 1_authors.php, 2_books.php, 3_authors_books.php etc.). Artisan sorts the filenames alphabetically before executing them.

解决多对多问题的一种方法是故意命名您的种子文件,以便最后填充数据透视表……例如,您可以在文件名前添加一个数值(即 1_authors.php、2_books.php)。 php、3_authors_books.php 等)。Artisan 在执行之前按字母顺序对文件名进行排序。

I have posted a small tutorial on Laravel 4 database seeding- this should get you going. Additionally, you may consult the official doc on seeding.

我已经发布了一个关于Laravel 4 数据库播种的小教程- 这应该会让你开始。此外,您可以查阅关于播种的官方文档。

回答by Darren Craig

Seeding is for simple information, test data and static information. I wouldn't recommend using it to handle relationships. Personally, I only use it for 2 or 3 record per table, to help test my application.

播种用于简单信息、测试数据和静态信息。我不建议用它来处理关系。就个人而言,我只将它用于每表 2 或 3 条记录,以帮助测试我的应用程序。

When developing your application, think about working on the data entry (admin) area first then the front end. That way you can easily add test data.

在开发您的应用程序时,首先考虑在数据输入(管理)区域工作,然后是前端。这样您就可以轻松添加测试数据。