I need to unassign status from state in migration.
$status->setStatus('approved')
->unassignState(Mage_Sales_Model_Order::STATE_NEW)
->assignState(Mage_Sales_Model_Order::STATE_PROCESSING, false)
->save();
If status is assigned to migration, it works. But if state is not assigned, there is exception and
migrations fails.
What is the best way to solve this problem?
The only solution that occured to me, is to create function trapper that catches exception. I this case
it seems that migration works even if status is not assigned to state.
function unAssignStatusFromState($status, $state)
{
try {
$status->unassignState($state);
return true;
} catch(Exception $e) {
return false;
}
}
Related
I'm developing one of my first applications with the Laravel 4 framework (which, by the way, is a joy to design with). For one component, there is an AJAX request to query an external server. The issue is, I want to cache these responses for a certain period of time only if they are successful.
Laravel has the Cache::remember() function, but the issue is there seems to be no "failed" mode (at least, none described in their documentation) where a cache would not be stored.
For example, take this simplified function:
try {
$server->query();
} catch (Exception $e) {
return Response::json('error', 400);
}
I would like to use Cache::remember on the output of this, but only if no Exception was thrown. I can think of some less-than-elegant ways to do this, but I would think that Laravel, being such an... eloquent... framework, would have a better way. Any help? Thanks!
This is what worked for me:
if (Cache::has($key)) {
$data = Cache::get($key);
} else {
try {
$data = longQueryOrProcess($key);
Cache::forever($key, $data); // only stored when no error
} catch (Exception $e) {
// deal with error, nothing cached
}
}
And of course you could use Cache::put($key, $data, $minutes); instead of forever
I found this question, because I was looking for an answer about this topic.
In the meanwhile I found a solution and like to share it here:
(also check out example 2 further on in the code)
<?php
/**
* Caching the query - Example 1
*/
function cacheQuery_v1($server)
{
// Set the time in minutes for the cache
$minutes = 10;
// Check if the query is cached
if (Cache::has('db_query'))
{
return Cache::get('db_query');
}
// Else run the query and cache the data or return the
// error response if an exception was catched
try
{
$query = $server->query(...);
}
catch (Exception $e)
{
return Response::json('error', 400);
}
// Cache the query output
Cache::put('db_query', $query, $minutes);
return $query;
}
/**
* Caching the query - Example 2
*/
function cacheQuery_v2($server)
{
// Set the time in minutes for the cache
$minutes = 10;
// Try to get the cached data. Else run the query and cache the output.
$query = Cache::remember('db_query', $minutes, function() use ($server) {
return $server->query(...);
});
// Check if the $query is NULL or returned output
if (empty($query))
{
return Response::json('error', 400);
}
return $query;
}
I recommend you to use Laravel's Eloquent ORM and/or the Query Builder to operate with the Database.
Happy coding!
We're working around this by storing the last good value in Cache::forever(). If there's an error in the cache update callback, we just pull the last value out of the forever key. If it's successful, we update the forever key.
Is there a way to proceed with a test even if assertSee returns with an error.
This is my current code:
$browser->visit('https://urltotest.co.uk/find-an-engineer/')
->type("EngineerId", $engineerId)
->click('#checkEngineer');
$test = $browser->assertSee("Engineer cannot be found");
What I would like to be able to do is go:
if ($test == false) {
return false;
} else {
return $browser->text('.engineer-search-results-container .search-result .col-md-8 .row .col-xs-10 h3');
}
But when the assertSee fails all I get back is:
Did not see expected text [Engineer cannot be found] within element [body].
As an exception. Any way I can proceed if it can't find that element?
You should be able to achieve this using a try catch to catch the exception:
try {
$browser->assertSee("Engineer cannot be found");
} catch(\Exception $e) {
return false;
}
To note, I do not know if there is a method such as $browser->fail() (like phpunit's $this->fail()) which will fail the test for you.
So, I want to update two different user accounts with the same value.
Scenario: User 1 transfers money to User 2:
$transfer_from = $account->decrement('balance', $amount);
$transfer_to = Account::where('user_wallet_address', $receiver_wallet_address)->increment('balance', $amount);
But, what I want, is something like this:
$account->decrement('balance', $amount)->$transfer_to;
As in if the decrement succeeds, then update the other user's balance, otherwise, both should fail
Thoughts?
Please use database transaction feature
See below code example:
public function readRecord() {
DB::beginTransaction();
try {
// put your code here in try block
$transfer_from = $account->decrement('balance', $amount);
$transfer_to = Account::where('user_wallet_address', $receiver_wallet_address)->increment('balance', $amount);
DB::commit();
} catch (Exception $ex) {
// If any failure case generate, it will rollback the DB operations automatically.
DB::rollBack();
echo $ex->getMessage() . $ex->getLine();
}
I hope this will help you. In this case if any failure case generate it will automatic revert the DB translocation and you don't need to do anything.
Reference URL: https://laravel.com/docs/5.7/database
Currently I am trying to upload some images parallel with dropzone.js.
This is part of the code, that handles the upload.
\DB::beginTransaction();
$maxPos = GalleryImage::lockForUpdate()->max('pos');
try {
// some other code
// ....
$galleryImage->pos = $maxPos + 1;
$galleryImage->save();
} catch (\Exception $e) {
\DB::rollBack();
\Log::error($e->getMessage());
return response('An error occured', 500);
}
\DB::commit();
If I do not use the lockForUpdate() I end up with duplicate positions in database.
The problem with above solution ist, that if the table is empty I get the error:
Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction
But only for the second image I am uploading.
Additionally the autoincrement id skips the value 2 and goes like 1,3,4,5,6,7, ...
The Positions in the table are shown correctly 1,2,3,4....
I think the problem has to do with the table beeing empty initially, as I did not notice this problem, when there are already some entries in the table.
Any suggestions what I am doing wrong? Or Maybe using lockForUpdate() in combination with an aggregate function is wrong at all...
I recommend a different approach to transactions.
try {
app('db')->transaction(function ()) {
$maxPos = GalleryImage::lockForUpdate()->max('pos');
// other code (...)
$galleryImage->pos = $maxPos + 1;
$galleryImage->save();
}
} catch (\Exception $e) {
// display an error to user
}
Should any exception occurs inside the callback, the transaction will rollback and free any locks.
I am trying to delete my queue on error.
I have tried the following:
file: global.php
Queue::failing(function($connection, $job, $data)
{
// Delete the job
$job->delete();
});
But when my queue fails like this one does:
public function fire($job, $data){
undefined_function(); // this function is not defined and will trow a error
}
Then the job is not deleted for some reasons.
Any ideas?
From their user manual, section Checking The Number Of Run Attempts:
If an exception occurs while the job is being processed, it will automatically be released back onto the queue.
I think you need to set the maximum number of tries to 1, so the job will fail on the first error.
php artisan queue:listen --tries=1
you can prevent your job from throwing exception
public function handle()
{
try {
//your code here
}catch (\Exception $e){
return true;
}
}