2016-07-09 10 views
0

私は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秒)

助けてください。

答えて

2

私はこの問題が私の接続であることを知りました。私は、デフォルトではなく、モデルでカスタム接続を使用しています。 LaravelでDBファサードを使用しているときは、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 
     } 


    } 
} 

とその動作が完全です。

0

私は問題がLaravelの取引ではないと信じていますが、それはcatchです。

try { 
    ... 
} catch (Exception $ex) { 
    ... 
} 

を次のように:交換してみException\Exceptionと異なること

try { 
    ... 
} catch (\Exception $ex) { 
    ... 
} 

注意。

+0

こんにちは@jockih。 – jaysingkar

+0

あなたのコードが名前空間であれば、PHPはクラスを現在の名前空間に関連して解決します。あなたのコントローラが 'App \ Http \ Controller \ ShopController'であれば' catch( Exception) 'は' App \ Http \ Controller \ Exception'を期待しています。あなたは '\ Exception'を投げているので、' \ Exception'もキャッチする必要があります。 – newbie

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) 
    } 
+0

私もこのアプローチを試みました。 実際に問題が私の接続にあった。私の答えを参照してください。 – jaysingkar

関連する問題