2016-07-09 9 views
0

Ich arbeite an einem Hobby-Projekt (Shop-Management) mit Laravel. Ich versuche DB :: beginTransaction() und DB :: rollback() zu verwenden, aber es funktioniert nicht. Nach meinem Code glaube ich, dass keine Einträge in der DB eingetragen sein sollten.Ich kann keine Transaktionen in Laravel verwenden

Ich habe bereits nach Möglichkeiten gegoogelt, konnte aber keine Lösung finden. Und meine MySQL-Tabellen sind InnoDB

Hier ist meine Shop-Controller-Datei.

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(); 
     } 


    } 
} 

Modelle:
1) Shop:

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) Der Anbieter

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-Tabelle Details DB-Engine zeigt.

mysql> SHOW TABLE STATUS WHERE Name = 'shops'; + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + | Name | Motor | Version | Row_format | Zeilen | Avg_row_length | Datenlänge | Max_data_length | Index_Länge | Daten_frei | Auto_increment | Create_time | Aktualisierungszeit | Check_time | Sortierung | Prüfsumme | Create_options | Kommentar | + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + | Geschäfte | InnoDB | 10 | Kompakt | 1 | 16384 |
16384 | 0 | 16384 | 0 | 17 | 2016-07-03 04:56:27 | NULL | NULL | utf8_general_ci |
NULL | | | + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + 1 Reihe einstellen (0.00 Sek.)

mysql> SHOW TABLE STATUS WHERE Name = 'Verkäufer'; + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + | Name | Motor | Version | Row_format | Zeilen | Avg_row_length | Datenlänge | Max_data_length | Index_Länge | Daten_frei | Auto_increment | Create_time | Aktualisierungszeit | Check_time | Sortierung | Prüfsumme | Create_options | Kommentar | + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + | Anbieter | InnoDB | 10 | Kompakt | 1 | 16384 |
16384 | 0 | 0 | 0 | 6 | 2016-07-07 00:46:08 | NULL | NULL | utf8_general_ci |
NULL | | | + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + 1 Reihe im Satz (0.00 Sek.)

Bitte helfen Sie.

Antwort

2

Ich fand heraus, dass das Problem mit meiner Verbindung war. Ich verwende eine benutzerdefinierte Verbindung in Modellen und nicht die Standardeinstellung. Und wenn ich DB-Fassade in Laravel benutze, denke ich, dass es die Standardverbindung verwendete, d. H. mysql und nicht die Verkäufer.

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'); 
    } 
} 

Verbindungen in meiner Datenbank-Konfigurationsdatei:

'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', 
     ] 
    ], 

Nun, mein Geschäft Controller rufen Transaktionen in folgenden Weise.

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 
     } 


    } 
} 

und es funktioniert perfekt.

0

Ich glaube, das Problem ist nicht in Laravel-Transaktion, aber es ist die catch-Anweisung. Versuchen Sie zu ersetzen:

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

mit den folgenden:

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

Beachten Sie, dass Exception als \Exception unterscheidet.

+0

Hi @ jockih. Könnten Sie bitte erklären, was der Unterschied zwischen Exception und Exception ist? – jaysingkar

+0

Wenn Ihr Code Namespace ist, wird PHP die Klasse relativ zum aktuellen Namespace auflösen. Sagen wir, Ihr Controller ist 'App \ Http \ Controller \ ShopController', dann' catch (Exception) 'erwartet' App \ Http \ Controller \ Exception'. Da Sie '\ Exception' werfen, müssen Sie auch' \ Exception' fangen. – newbie

0

Warum nicht einfach ohne Transaktionen auskommen?

Wenn es Shop nicht innerhalb des Anbieters erstellen wird, behandeln Sie einfach Ihre benutzerdefinierte Ausnahme und das ist alles.

db rollback ist eine Dummy-Funktion, wenn Sie versuchen, die fehlgeschlagene Ausführung rückgängig zu machen.

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

Ich habe auch diesen Ansatz versucht, aber konnte nicht bekomme es zur Arbeit Eigentlich Problem war mit meiner Verbindung.Siehe meine Antwort. – jaysingkar

Verwandte Themen