Laravel - 多插入行并检索 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25065317/
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
Laravel - multi-insert rows and retrieve ids
提问by Gustavo Silva
I'm using Laravel 4, and I need to insert some rows into a MySQL table, and I need to get their inserted IDs back.
我正在使用 Laravel 4,我需要在 MySQL 表中插入一些行,我需要取回它们插入的 ID。
For a single row, I can use ->insertGetId()
, however it has no support for multiple rows. If I could at least retrieve the ID of the first row, as plain MySQL does, would be enough to figure out the other ones.
对于单行,我可以使用->insertGetId()
,但是它不支持多行。如果我至少可以像普通 MySQL 那样检索第一行的 ID,就足以找出其他行的 ID。
采纳答案by peter.babic
As user Xrymz suggested, DB::raw('LAST_INSERT_ID();')
returns the first.
正如用户 Xrymz 建议的那样,DB::raw('LAST_INSERT_ID();')
返回第一个。
According to Schema apiinsertGetId()
accepts array
根据Schema apiinsertGetId()
接受数组
public int insertGetId(array $values, string $sequence = null)
public int insertGetId(array $values, string $sequence = null)
So you have to be able to do
所以你必须能够做到
DB::table('table')->insertGetId($arrayValues);
DB::table('table')->insertGetId($arrayValues);
Thats speaking, if using MySQL, you could retrive the first id by this and calculate the rest. There is also a DB::getPdo()->lastInsertId();
function, that could help.
也就是说,如果使用 MySQL,您可以通过此检索第一个 id 并计算其余部分。还有一个DB::getPdo()->lastInsertId();
功能,可以提供帮助。
Or if it returened the last id with some of this methods, you can calculate it back to the first inserted too.
或者,如果它使用某些方法返回最后一个 id,您也可以将其计算回第一个插入的 ID。
EDIT
编辑
According to comments, my suggestions may be wrong.
根据评论,我的建议可能是错误的。
Regarding the question of 'what if row is inserted by another user inbetween', it dependson the store engine. If engine with table level locking (MyISAM, MEMORY, and MERGE) is used, then the question is irrevelant, since thete cannot be two simultaneous writes to the table.
关于“如果行被另一个用户插入中间会怎样”的问题,这取决于存储引擎。如果使用具有表级锁定(MyISAM、MEMORY和MERGE)的引擎,则问题无关紧要,因为不能同时向表中写入两次。
If row-level locking engine is used (InnoDB), then, another possibility might be to just insert the data, and then retrieve all the rows by some known field with whereIn()
method, or figure out the table level locking.
如果使用行级锁定引擎(InnoDB),那么,另一种可能是只插入数据,然后使用whereIn()
方法通过某个已知字段检索所有行,或者找出表级锁定。
回答by Xrymz
It's mysql behavior of last-insert-id
这是last-insert-id的 mysql 行为
Important
If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.
重要
如果您使用单个 INSERT 语句插入多行,则 LAST_INSERT_ID() 仅返回为第一个插入的行生成的值。这样做的原因是可以轻松地针对其他服务器重现相同的 INSERT 语句。
u can try use many insert and take it ids or after save, try use $data->id should be the last id inserted.
你可以尝试使用多次插入并获取它的 id 或保存后,尝试使用 $data->id 应该是最后插入的 id。