i have the following code:
$new_models = DB::transaction(function () use ($supplier, $address, $addressDetail) {
$new_supplier = $this->setNewSupplier($supplier);
$new_address = $this->setNewAddress($address);
$new_addressDetail = $this->setNewAddressDetail($addressDetail,$new_address->id);
$this->syncSupplierAddress($new_supplier->id,$new_address->id);
$this->updateControlAp($new_supplier->supplier_id);
return [$new_supplier, $new_address, $new_addressDetail];
});
The set methods are basically creating the models object with save() at the end;
Now this works perfectly fine if 2nd...nth fails BUT NOT if the first one fails.
If $this->setNewSupplier($supplier);
fails than i get
"PDOException in Connection.php line 541:
There is no active transaction"
Am i doing something wrong here ? Also if i comment $this->rollBack(); from the catch in vendor Connection.php it actually gives me the SQL error. Important part here is that this isn't working only if first save() fails
PS. I am using PostgreSQL not MySQL but i don't think its related
There are different ways to make transactions in Laravel, another one could be this:
...
$new_models = [];
try {
DB::beginTransaction();
$new_supplier = $this->setNewSupplier($supplier);
$new_address = $this->setNewAddress($address);
$new_addressDetail = $this->setNewAddressDetail($addressDetail,$new_address->id);
$this->syncSupplierAddress($new_supplier->id,$new_address->id);
$this->updateControlAp($new_supplier->supplier_id);
$new_models = [$new_supplier, $new_address, $new_addressDetail];
DB::commit();
} catch(\Exception $e) {
DB::rollback();
// Handle Error
}
...
Related
I have this simple function and I want to make laravel transaction. It is inserting the first(SecondaryShare) while the second(Primary Share) contains error and I want to rollback and delete the SecondaryShare once the error occured.
try {
DB::transaction(function () use ($request) {
$Share = new SecondaryShares();
$Share->secondary_name = $request->secondaryName;
$Share->primary_id = $request->primaryId
$Share->save();
//error in primaryname==> correct is primary_name
$Share = new PrimaryShares();
$Share->primaryname = $request->primaryName;
$Share->percentage = $request->percentage;
$Share->visibility = $visibility;
$Share->save();
});
} catch (\Exception $e) {
dd('failed');
}
dd('worked');
How can I fix it?
Thanks in advance.
You are using the transaction system the wrong way. There is two ways to use it and you are mixing them up, although that should never be done.
Option 1: Transaction Closure
You can put your code into a transaction closure. As soon as an Exception (actually Throwable) or any exception type inheriting from these types is thrown, the transaction will be rolled back and the exception will be re-thrown by the closure handler:
DB::transaction(function () {
$model1 = MyModel::create();
// model2 will not be created if model1 couldn't be created
$model2 = MyModel::create();
});
You can also wrap the transaction closure with a try-catch to catch any transaction exception:
try {
DB::transaction(function () {
$model1 = MyModel::create();
// model2 will not be created if model1 couldn't be created
$model2 = MyModel::create();
});
} catch (\Exception $e) {
Log::error('Insert failed', ['exception' => $e]);
return redirect()->back()->withInput();
}
return redirect()->route('form.success');
Option 2: Transaction Control
Or alternatively, you can control the transaction logic yourself. But be careful, this way is more dangerous because it happens quite easily that one forgets a rollback or commit:
DB::beginTransaction();
$model1 = MyModel::create();
if ($model1->exists !== true) {
DB::rollBack();
Log::error('Insert 1 failed');
return redirect()->back()->withInput();
}
$model2 = MyModel::create();
if ($model2->exists !== true) {
DB::rollBack();
Log::error('Insert 2 failed');
return redirect()->back()->withInput();
}
DB::commit();
return redirect()->route('form.success');
In my opinion, using option 2 yields a lot more code and is less readable.
By the way, Laravel is using custom database exceptions. It will throw a \Illuminate\Database\QueryException and no \PDOException.
My database is InnoDB but i figured out that the tables is MyISAM.
solved by adding this line of code to migration:
$table->engine = "InnoDB";
I separate the model queries on a trait class, because I hate to read a long block of codes with the model queries, and because I find it convenient if I reused the same function. But I found a problem once an error occur.
I was trying to run rollback function from eloquent once an error occur but, unfortunately, rollback wont work as I am expecting.
Am I doing it wrong?
Are there any other ways to implement this?
try {
DB::beginTransaction();
// UserDetails
$userdetailsID = $this->saveUserDetails($request,$userData->id);
if($userdetailsID){
$result = $this->saveUser(
$request,
$this->getHashValue($request->password),
$userdetailsID,
$userData->id,
$this->cleanTobeSafeAsDirectory(crypt(($userdetailsID.$userData->companyid), 'rl'))
);
if($result){
$updalodResult = $this->uploadThisImage( $request , 'images/uploads/users/'.$userdetailsID.'/icon/', '_'.$this->getHashValue($userdetailsID),'userImageAvatar');
if($updalodResult['success']){
$resul = $this->getThisUserDetials($userdetailsID);
$resul->photo_name = $updalodResult['filename'];
$resul->save();
$imageFilePath = 'images/uploads/users/'.$userdetailsID.'/icon/'.$updalodResult['filename'];
$this->cropImageJpegOnly($imageFilePath,$request->img_x,$request->img_y,$request->img_w,$request->img_h,$request->img_width,$request->img_height);
}
DB::commit();
return $this->returnAsAppSuccess('User information added.');
}
DB::rollBack();
return $this->returnAsAppError('Failed to user security details.' );
}
DB::rollBack();
return $this->returnAsAppError('Failed to user security details.' );
} catch (PDOException $e) {
DB::rollBack();
return $this->returnAsAppError('Failed to insert User information.');
}
I've use Larvel 5.0 with Database transaction all the method in my previous web application it work as well and we are really love it because this application help me much more than our estimated
so we have create another webs application by using this newest version of this framework and used the same Database structure but finaly it would not work for me and another one to.
I have as more peoples and post on some toturial website for asking any belp but not yet get any solution so I record this video for sure about this case.
Issue: My issue I've disabled (//commit()) method all data still can insert into Database.
final function Add()
{
if ($this->request->isMethod('post')) {
//No you will see this method use with Try Catch and testing again
//DB::beginTransaction(); // Ihave testing with outside of try and inside again
Try{
DB::beginTransaction();
$cats = new Cat();
$catD = new CategoryDescriptions();
$cats->parent_id = $this->request->input('category_id');
$cats->status = ($this->request->input('status')) ? $this->request->input('status') : 0;
if (($res['result'] = $cats->save())== true) {
$catD->category_id = $cats->id;
$catD->language_id = 1;
$catD->name = $this->request->input('en_name');
if (($res['result'] = $catD->save()) === true) {
$catD2 = new CategoryDescriptions();
$catD2->category_id = $cats->id;
$catD2->language_id = 2;
$catD2->name = $this->request->input('kh_name');
$res['result'] = $catD2->save();
}
}
if(!empty($res)) {
//DB::commit();
}
return [$res,($res['result'] = $catD->save())];
}catch(\Exception $e){ // I have already try to use Exception $e without backslash
DB::rollback();
}
}
$cat = Cat::with(['CategoryDescriptions', 'children'])->where('status', 1)->get();
return view('admin.categories.add', ['cat' => $cat]);
}
You can check on my video to see that .
Check on my video
I don't know why your code did not work. But you can try with this code I think it's will work. Laravel transaction documentation
try{
DB::transaction(function)use(/*your variables*/){
// your code
});
}catch(\PDOException $exception){
//debug
}
If any exception occurs it will automatically rollback. If you want manual rollback then inside transaction you can throw a manual exception based on your logic.
I am connecting to MySQL through PDO with Zend\Db from ZF2. How can I report the last errorInfo()?
Here's what I have:
$sqlWriter = new Sql($this->getAdapter());
$insert = $sqlWriter->insert('table_name')->columns(array_keys($data))->values($data);
$stmt = $sqlWriter->prepareStatementForSqlObject($insert);
try {
$stmt->execute();
$object->id = $this->getAdapter()->driver->getLastGeneratedValue();
} catch (\Exception $e) {
//
// HOW CAN I display errorInfo() here?
//
throw new Exception\Exception('Unable to insert record...');
}
I have tried calling methods on the adapter, driver, statement, platform, result, etc... But all to no avail...
EDIT: I found that I can get the info I am looking for by posting the following at the top of the catch block:
$pdoException = $e->getPrevious();
var_dump($pdoException);
I'll leave the question open however since it would be good to know how to execute PDO::errorInfo() directly.
It's my first time to use DB::transaction() but how exactly does it work if a transaction fails or is successful? In the example below, do I have to manually assign a value to return true, or if it fails will the method either return false or totally exit the transaction (therefore skipping the rest of the code)? The docs aren't so helpful on this.
use Exception;
use DB;
try {
$success = DB::transaction(function() {
// Run some queries
});
print_r($success);
} catch(Exception $e) {
echo 'Uh oh.';
}
Solution
I wrote down this solution for others who might be wondering.
Since I was more concerned about returning a boolean value depending on the success of my query, with a few modifications it now returns true/false depending on its success:
use Exception;
use DB;
try {
$exception = DB::transaction(function() {
// Run queries here
});
return is_null($exception) ? true : $exception;
} catch(Exception $e) {
return false;
}
Take note that the variable $exception is never returned since if something goes wrong with your query, the catch is immediately triggered returning false. Thanks to #ilaijin for showing that an Exception object is thrown if something goes wrong.
By giving a look at function transaction it does its process inside a try/catch block
public function transaction(Closure $callback)
{
$this->beginTransaction();
// We'll simply execute the given callback within a try / catch block
// and if we catch any exception we can rollback the transaction
// so that none of the changes are persisted to the database.
try
{
$result = $callback($this);
$this->commit();
}
// If we catch an exception, we will roll back so nothing gets messed
// up in the database. Then we'll re-throw the exception so it can
// be handled how the developer sees fit for their applications.
catch (\Exception $e)
{
$this->rollBack();
throw $e;
}
So throws an Exception (after the rollback) if fails or returns $result, which is the result of your callback
There is a short version if you want to use the default transaction method that ships with Laravel without handling it manually.
$result = DB::transaction(function () {
// logic here
return $somethingYouWantToCheckLater;
});
You can also use the following
DB::rollback();