2017-07-20 15 views
0

我有一個訂單的應用程序,其中添加的訂單和更高版本的電子郵件被髮送到訂單的用戶這樣表演時優化yii2 MySQL的性能寫道

if(Yii::$app->request->post()){ 


    //assign model attributes 
    if($model->save()){ 
     return [ 
        status:true ///notifies the frontend that saved successifully 
       ] 

      //the below processes are time consuming hence i would like them to queue 
      $this->sendEmail($model->orderuser); 
      $this->sendSMS($model->user_no) 
     } 

    } 

模型保存後,我想發送電子郵件和短信,但這些有時需要相當一段時間因此,我希望返回聲明通知,過程完成後發送郵件和短信

在上述架構後的返回語句其他人不執行?

當我return語句的過程需要一些有價值秒,我怎麼能執行我的計劃return語句後

+2

我」使用try/catch塊之前將它們添加d將它們保存在一個隊列中並寫一個處理所述隊列的cronjob –

+0

你對yii2中的隊列有什麼想法嗎?我在laravel中看到過一個,但是沒有關於如何在yii2中實現這一點的線索 –

+0

如果你打算使用隊列,然後有一個最近的擴展版本d你可以使用:http://www.yiiframework.com/news/141/yii-2-queue-extension-released/ – Chinmay

回答

0

你可以在$model->save()

//assign model attributes 
try { 
    $model->save(); 
} catch (yii\db\Exception $e) { 
    throw new \yii\web\HttpException(405, 'Error saving model'); 
} catch (ErrorException $e) { 
    throw new \yii\web\HttpException(405, 'Error saving model'); 
} 
$this->sendEmail($model->orderuser); 
$this->sendSMS($model->user_no); 
+0

1. Yii1有個例外,他有Yii2 2.沒有意義'嘗試/抓住「(當然他應該使用它!),但這並不能解決他的問題。他不想這樣做異步,所以他必須準備隊列系統。 – Yupik

+0

@Yupik。謝謝..正確..錯誤refrence ..我有更新的答案.. – scaisEdge