php - 如何在 Laravel 中插入具有多个不同列的多行

标签 php mysql laravel laravel-5.6

我刚刚尝试了以下命令并收到了不希望的结果。

DB::table('locations')->insert([
    ['code' => 'YC', 'name' => 'York Clifton'],
    ['code' => 'YK', 'name' => 'York'],

    ['postcode' => 'DR1',  'name' => 'Jason'],
    ['postcode' => 'DLR',  'name' => 'Beckton']
]);

上面的代码会在表中插入数据:

                Expected                                         Actual
+-----+--------------+------+----------+        +----+--------------+------+----------+
| id  |     name     | code | postcode |        | id |     name     | code | postcode |
+-----+--------------+------+----------+        +----+--------------+------+----------+
|   1 | York Clifton | YC   | NULL     |        |  1 | York Clifton | YC   | NULL     |
|   2 | York         | YK   | NULL     |        |  2 | York         | YK   | NULL     |
|   3 | Jason        | NULL | DR1      |        |  3 | DR1          | Jaso | NULL     |
|   4 | Beckton      | NULL | DLR      |        |  4 | DLR          | Beck | NULL     |
+-----+--------------+------+----------+        +----+--------------+------+----------+

位置表是使用以下代码片段构建的:

$table->string('name', 100);
$table->string('code', 4)->nullable();
$table->string('postcode', 10)->nullable();

当然,我想要的结果是向数据库中插入四行;前两个将填充 codename 字段,而后两个插入将填充 postcodename

我看过文档说:

The query builder also provides an insert method for inserting records into the database table. The insert method accepts an array of column names and values:

You may even insert several records into the table with a single call to insert by passing an array of arrays. Each array represents a row to be inserted into the table.

我不完全确定 Laravel 在幕后做了什么,但它似乎预先构建了插入语句然后插入数据,忽略了列名键。
为了避免这个问题,我只是将插入语句与不同的列名分开。
这让我想到,如果所有记录都是多余的(第一个数组中的键除外),为什么还要为所有记录设置列键呢?为什么不为插入方法设置两个参数;一个带有列名数组,一个带有数据。


文档没有说数组键必须全部相同,所以如果我遗漏了什么,那么如果有人能提供一些关于为什么这不起作用的见解,我将不胜感激。

tl;博士

当使用不同的列名时,如何将多行插入到表中?

最佳答案

查看 Laravel 的代码刚刚看到了这种行为的原因。显然 Laravel 将插入查询编译为批量插入,而不是为每个传递的数组单独插入查询。

在插入方法中你可以看到查询是如何生成的:

$sql = $this->grammar->compileInsert($this, $values);

如果您进一步了解 compileInsert 方法,您会注意到查询的列仅从传递的第一个数组生成:

$columns = $this->columnize(array_keys(reset($values)));

// We need to build a list of parameter place-holders of values that are bound
// to the query. Each insert should have the exact same amount of parameter
// bindings so we will loop through the record and parameterize them all.
$parameters = [];

foreach ($values as $record) {
    $parameters[] = '('.$this->parameterize($record).')';
}

$parameters = implode(', ', $parameters);

return "insert into $table ($columns) values $parameters";

所以基本上,您的插入调用将执行查询:

INSERT INTO `locations` (`code`, `name`) 
VALUES ('YC', 'York Clifton'), 
       ('YK', 'York'),
       ('DR1', '...')

但是,您可以通过一次调用插入所有条目,方法是提供位置表中的所有列:

DB::table('locations')->insert([
    ['code' => 'YC', 'name' => 'York Clifton', 'postcode' => null],
    ['code' => 'YK', 'name' => 'York', 'postcode' => null],

    ['code' => null, 'name' => 'Jason', 'postcode' => 'DR1'],
    ['code' => null, 'name' => 'Beckton', 'postcode' => 'DLR']
]);

关于php - 如何在 Laravel 中插入具有多个不同列的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042571/

相关文章:

php - mysql中多次提交更改记录

php - 从字符串中提取启发式(模糊)日期?

javascript - php jquery克隆输入字段插入到数据库的同一列中

mysql - Laravel Eloquent - 对多个关系列求和

ajax - 我的 POST ajax 请求在 Laravel Controller 上返回 302

PHP、MySQL : how to select random records without RAND (attempt to avoid RAND())

php - 使用 jquery 从 ajax post 请求下载 zip 文件

mysql - WordPress 的数据库主机

c# - Winforms + mysql + asp.net 对于具有远程访问功能的 C# 数据库应用程序来说足够了吗?

javascript - 在 JavaScript 中使用 Assets 的图像路径