Php 数据库种子设定-在一个查询中插入多条记录

Php 数据库种子设定-在一个查询中插入多条记录,php,laravel,laravel-4,seeding,Php,Laravel,Laravel 4,Seeding,我有一个Laravel项目,我想在数据库中插入900多个城市作为数据库种子 例如,我可以这样做: $state_id = State::whereName('state name')->pluck('id'); $city = new City(); $city->name = 'my city name'; $city->state_id = $state_id; $city->save(); DB::table('cities')->insert(

我有一个Laravel项目,我想在数据库中插入900多个城市作为数据库种子

例如,我可以这样做:

$state_id = State::whereName('state name')->pluck('id');

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$city->save();
DB::table('cities')->insert(
    [
        [
            'name' => 'City name',
            'url' => Slug::create('City name'),
            'created_at' => $now,
            'updated_at' => $now,
            'state_id' => $state_id
        ],
        [
            'name' => 'City name 2',
            'url' => Slug::create('City name 2'),
            'created_at' => $now,
            'updated_at' => $now,
            'state_id' => $state_id
        ],
    ]);
在我的城市模型中,我定义了“另存为”:

public function save(array $options = array()) {
    $this->url = $this->createUrl($this->name);
    parent::save($options);
}
因此,它还为城市创建url

我可以输入900次这样的代码块,但有一个问题——它将在单独的查询中运行,所以将这些数据插入数据库需要30秒以上的时间

例如,我可以这样做:

$state_id = State::whereName('state name')->pluck('id');

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$city->save();
DB::table('cities')->insert(
    [
        [
            'name' => 'City name',
            'url' => Slug::create('City name'),
            'created_at' => $now,
            'updated_at' => $now,
            'state_id' => $state_id
        ],
        [
            'name' => 'City name 2',
            'url' => Slug::create('City name 2'),
            'created_at' => $now,
            'updated_at' => $now,
            'state_id' => $state_id
        ],
    ]);
这样,我可以在一个SQL查询中插入许多记录,但我认为这不是一个很好的解决方案-我需要手动设置所有数据库字段,但将所有数据插入数据库只需3-4秒

问题是-是否可以创建模型并使用一些神奇的方法返回就绪PHP数组来在多文件插入中使用它(我了解到Eloquent不能用于在一个查询中插入多个记录)

我认为最好是这样的代码:

$state_id = State::whereName('state name')->pluck('id');

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$city1 = $city->saveFake(); // magic method that returns complete array

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$city2 = $city->saveFake(); // magic method that returns complete array

DB::table('cities')->insert(
    [
        $city1,
        $city2,
    ]);
$state_id = State::whereName('state name')->pluck('id');
$cities = array();

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$cities[] = $city->attributesToArray();

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$cities[] = $city->attributesToArray();

DB::table('cities')->insert($cities);
代替saveFake()函数,您可以执行以下操作:

$city->attributesToArray()
这将返回必须存储在表中的所有属性

您可以将它们添加到数组中,并将其放入插入函数中

这将导致如下结果:

$state_id = State::whereName('state name')->pluck('id');

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$city1 = $city->saveFake(); // magic method that returns complete array

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$city2 = $city->saveFake(); // magic method that returns complete array

DB::table('cities')->insert(
    [
        $city1,
        $city2,
    ]);
$state_id = State::whereName('state name')->pluck('id');
$cities = array();

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$cities[] = $city->attributesToArray();

$city = new City();
$city->name = 'my city name';
$city->state_id = $state_id;
$cities[] = $city->attributesToArray();

DB::table('cities')->insert($cities);
getAttributes
返回插入数据库的“原始”基础属性

请记住,这将绕过诸如保存/创建之类的雄辩事件


还要记住,如果
$data
变得太大,您可能会遇到内存问题。如果是这种情况,请使用类似于
array\u chunk()
的方法将其拆分。

恐怕它不起作用。它将只返回数组中的
name
state\u id
,而不返回其他字段(
created\u at
modified\u at
url
,这些字段是根据
save
函数中的
name
计算的),您可以手动将它们添加到您的模型中。只需添加attributesToArray()并添加要手动添加的字段。并调用父函数。关键是我不想手动添加它们。如果我想要,我会使用我在问题中展示的方法,我甚至不需要创建模型。如果你想要所有的变量,你可以使用
toArray()
。您是否已经为您的URL实现了一个变体?比如
getUrlAttribute($value)