我想到这样的代码是一对一插入的:
$anotherPet = \App\Pet::create($data);
$anotherUser = \App\User::create($data);
$anotherPet->user()->associate($anotherUser);
$anotherPet->save();create()是调用db还是事务是在save()上进行的?
3比1电话?
感谢你的帮助。
发布于 2016-11-04 14:26:43
假设您使用的是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个查询,我无法在没有外键约束的情况下在单个查询中使其失败。
https://stackoverflow.com/questions/40425029
复制相似问题