Model::create是否创建数据库事务?


Does Model::create make a db transaction

我得出了一对一插入的代码:

$anotherPet = 'App'Pet::create($data);
$anotherUser = 'App'User::create($data);
$anotherPet->user()->associate($anotherUser);
$anotherPet->save();

create()调用db还是在save()上进行交易?

3 vs 1调用?

感谢您的帮助。

假设你正在使用laravel, create做一个事务,如果你只想做一个,你可以创建一个新的实例,并像这样填充它:

$anotherPet = new 'App'Pet;
$anotherPet->fill($data);
$anotherUser = 'App'User::create($data); // transaction is made here; mandatory to have an id for your association
$anotherPet->user()->associate($anotherUser); // no transaction
$anotherPet->save(); // transaction is made here

这样你就有2个查询而不是3个,我没有办法使它在没有外键约束的单个查询中失败。