我正在与Laravel合作开展业余爱好项目(店铺管理)。我试图使用DB :: beginTransaction()和DB :: rollback(),但它无法正常工作。根据我的代码,我认为数据库中不应填充任何条目。
我已经搜索了可能性,但无法找到任何解决方案。 而且,我的MySQL表是InnoDB
这是我的Shop Controller文件。
class shopController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getView()
{
if(Auth::user()->shop_status==0)
return View::make('shop')->with('name',json_encode(Auth::user()->Vendor_Detail()->get(['first_name', 'last_name'])));
else
return redirect('/home');
}
public function addShop(ShopDataRequest $request){
//get the logged in vendor's model instance. Auth uses Vendor model
$vendor = Auth::user();
//create new Shop Model
$shop = new Shop;
$shop->name = $request['shop_name'];
$shop->address = $request->addressLine1;
$shop->pincode = $request->pincode;
$shop->phone = $request->phone;
$shop->shop_type = $request->shop_type;
DB::beginTransaction();
try{
//save shop details
$vendor->Shops()->save($shop);
//throw custom Exception
throw new \Exception('User not created for account');
}
catch (Exception $e){
//catch exception and rollback
DB::rollback();
}
}
}
型号:
1)购物:
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Shop extends Authenticatable
{
protected $connection = 'vendor';
protected $fillable = [
'shop_id','vendor_id','name','address','pincode','phone','shop_type'
];
public function Vendor(){
return $this->belongsTo('App\Models\Vendor','vendor_id','vendor_id');
}
public function Products(){
return $this->belongsToMany('App\Models\Product')->withPivot('mfg_date', 'exp_date','active','quantity');
}
}
2)供应商
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Vendor extends Authenticatable
{
protected $connection = 'vendor';
protected $primaryKey = 'vendor_id';
protected $fillable = [
'email','phone','password',
];
protected $hidden = [
'password', 'remember_token',
];
public function Vendor_Detail(){
return $this->hasOne('App\Models\Vendor_Detail','vendor_id','vendor_id');
}
public function Shops(){
return $this->hasMany('App\Models\Shop','vendor_id','vendor_id');
}
public function Documents(){
return $this->hasMany('App\Models\Vendor_document','vendor_id','vendor_id');
}
}
显示数据库引擎的MySQL表格详细信息。
的MySQL> SHOW TABLE STATUS
Name
=' shop&#39 ;; + ------- + -------- + --------- + ------------ + ------ + - -------------- + ------------- + ----------------- + --- ----------- ----------- + + + ---------------- --------- ------------ + ------------- + ------------ + ---------- ------- + ---------- + ---------------- + --------- + | 姓名|引擎 |版本| Row_format |行| Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | 整理|校验和| Create_options |评论| + ------- + -------- + --------- + ------------ + ------ + - -------------- + ------------- + ----------------- + --- ----------- ----------- + + + ---------------- --------- ------------ + ------------- + ------------ + ---------- ------- + ---------- + ---------------- + --------- + | 商店| InnoDB | 10 |紧凑| 1 | 16384 |
16384 | 0 | 16384 | 0 | 17 | 2016-07-03 04:56:27 | NULL | NULL | utf8_general_ci |
NULL | | | + ------- + -------- + --------- + ------------ + ------ + - -------------- + ------------- + ----------------- + --- ----------- ----------- + + + ---------------- --------- ------------ + ------------- + ------------ + ---------- ------- + ---------- + ---------------- + --------- + 1行集合(0.00秒)的MySQL>显示表状态
Name
='供应商&#39 ;; + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + - ------------- ----------- + + + ---------------- ------- -------------- + ------------- + ------------ + -------- --------- + ---------- + ---------------- + --------- + | 姓名|引擎 |版本| Row_format |行| Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | 整理|校验和| Create_options |评论| + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + - ------------- ----------- + + + ---------------- ------- -------------- + ------------- + ------------ + -------- --------- + ---------- + ---------------- + --------- + | 供应商| InnoDB | 10 |紧凑| 1 | 16384 |
16384 | 0 | 0 | 0 | 6 | 2016-07-07 00:46:08 | NULL | NULL | utf8_general_ci |
NULL | | | + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + - ------------- ----------- + + + ---------------- ------- -------------- + ------------- + ------------ + -------- --------- + ---------- + ---------------- + --------- + 1行在集(0.00秒)
请帮忙。
答案 0 :(得分:3)
我发现问题出在我的连接上。我在模型中使用自定义连接而不是默认连接。在Laravel中使用DB facade时,我认为它使用的是默认连接,即 mysql ,而不是供应商。
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Shop extends Authenticatable
{
protected $connection = 'vendor';\\custom connection
protected $fillable = [
'shop_id','vendor_id','name','address','pincode','phone','shop_type'
];
public function Vendor(){
return $this->belongsTo('App\Models\Vendor','vendor_id','vendor_id');
}
public function Products(){
return $this->belongsToMany('App\Models\Product')->withPivot('mfg_date', 'exp_date','active','quantity');
}
}
我的数据库配置文件中的连接:
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => 'shop',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB',
],
'vendor' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => 'shop',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB',
]
],
现在,我的店铺管理员以下列方式调用交易。
class shopController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getView()
{
if(Auth::user()->shop_status==0)
return View::make('shop')->with('name',json_encode(Auth::user()->Vendor_Detail()->get(['first_name', 'last_name'])));
else
return redirect('/home');
}
public function addShop(ShopDataRequest $request){
//get the logged in vendor's model instance. Auth uses Vendor model
$vendor = Auth::user();
//create new Shop Model
$shop = new Shop;
$shop->name = $request['shop_name'];
$shop->address = $request->addressLine1;
$shop->pincode = $request->pincode;
$shop->phone = $request->phone;
$shop->shop_type = $request->shop_type;
//getting the required connection
$connection = DB::connection('vendor');
$connection::beginTransaction();//calling the beginTransaction() on connection
try{
//save shop details
$vendor->Shops()->save($shop);
//throw custom Exception
throw new \Exception('User not created for account');
}
catch (Exception $e){
//catch exception and rollback
$connection::rollback(); //calling the rollback() on connection
}
}
}
并且它的工作完美。
答案 1 :(得分:0)
您必须使用<p>Text being displayed when suddenly
<details>
<summary>View More</summary>
here comes the text to be expanded.</p>
</details>
来表达您的疑问。仔细阅读手册trincot
答案 2 :(得分:0)
我认为问题不在Laravel的交易中,而是catch
声明。尝试替换:
try {
...
} catch (Exception $ex) {
...
}
以下内容:
try {
...
} catch (\Exception $ex) {
...
}
请注意,Exception
与\Exception
不同。
答案 3 :(得分:0)
为什么不做没有交易?
如果它不会在供应商内部创建商店,只需处理您的自定义异常即可。
如果您尝试回滚不成功的执行,则db rollback是虚函数。
try{
//save shop details
if(!$vendor->Shops()->save($shop)) {
//throw custom Exception
throw new \Exception('User not created for account');
}
}
catch (Exception $e){
//catch exception and do anything You wanted (without db::rollback)
}