私はLaravelのホビープロジェクト(ショップ管理)に取り組んでいます。私はDB :: beginTransaction()とDB :: rollback()を使用しようとしていますが、動作していません。私のコードによれば、私はエントリがDBに移入されるべきではないと信じています。私はLaravelでトランザクションを使用することができません
私はすでに可能性を探っていますが、解決策は見つかりませんでした。 そして、私のMySQLテーブルは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;
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)ベンダーDBエンジンを示す
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ステータスWHERE
Name
= 'ショップ'; + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + | 名前|エンジン |バージョン| 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> SHOW TABLE STATUS WHERE
Name
= 'ベンダー'; + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + | 名前|エンジン |バージョン| 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秒)
助けてください。
ロールバック()後もコミットする必要がありますか? – jaysingkar
私はcommit()を使ってみました。それでも同じ:-( – jaysingkar
保存後に使用する必要があります。例外をスローする前に – LONGMAN