2017-03-10 4 views
-2

終了した場合にのみ、テーブルを切り捨て:symfonyの+ Doctrineはforeachの一連のは、私は私のDB 5エンティティで持っているだけでなく

Customer 
Location 
Machine_1 
Machine_2 
Machine_3 

Customerが持つOneToMany関係にあるLocation(1 Customer持つことができ、複数のLocations)、Machine_1Machine_2およびMachine_3LocationLocationの1つは複数のMachinesを持つことができます)との関係にあります。

私はすでにdbに格納されているエンティティのすべてのデータを持っています。 3台のマシンのデータを更新できるAPIサービス(POST)があります。 は、これは私がそれをやっている方法です:

/** 
    * @Route("/saveData") 
    * @Method("POST") 
    **/ 
    public function saveDataAction (Request $request) { 
     $data = $request->getContent(); 
     $dataDecode = json_decode($data); 

     if (is_object($dataDecode)) { 
     foreach ($dataDecode->customer as $customer) { 
      //get data 
      foreach ($dataDecode->location as $location) { 
      //getdata 
      foreach ($dataDecode->machine_1 as $machine_1) { 
       //get and set data 
      } 
      foreach ($dataDecode->machine_2 as $machine_2) { 
       //get and set data 
      } 
      foreach ($dataDecode->machine_3 as $machine_3) { 
       //get and set data 
      } 
      } 
     } 

私は、新しいデータを挿入する前に、すべての機械テーブルを切り捨てる必要があるが、これはすべてがforeachループの内側に細かい行く場合にのみ発生することがあります。

このような何かが、唯一のコードの残りの部分がうまく実行された場合:

if (is_object($dataDecode)) { 
    $em->createQuery('DELETE FROM AppBundle:Machine_1')->execute(); 
    $em->createQuery('DELETE FROM AppBundle:Machine_2')->execute(); 
    $em->createQuery('DELETE FROM AppBundle:Machine_3')->execute(); 
    ..... 
} 

私はそれをどのように行うことができますか?ありがとう!

+0

をあなたは「foreachのがうまく終了」何を意味するのですか?あなたは何を試しましたか、そしてなぜそれはうまくいかなかったのですか? – yivi

+0

@yivi私はすべてがうまくいくということを意味し、私は何の誤りもありません。データを取得して設定するプロセスがうまく終了することを知る前に、テーブルを切り捨てることは望ましくありません。 – Dygne

+0

何を試しましたか? – yivi

答えて

0

PHPのExceptionメカニズムを使用できます。 「コードの残りの部分がうまく実行された場合」が、あなたのようなものを使用でき、私はあなたが何を意味を正確に知らない :

/** 
* @Route("/saveData") 
* @Method("POST") 
**/ 
public function saveDataAction (Request $request) { 
    $data = $request->getContent(); 
    $dataDecode = json_decode($data); 

    if (is_object($dataDecode)) { 
    try{ 
     foreach ($dataDecode->customer as $customer) { 
     //get data 
     if ($something_is_wrong){ 
      throw new \Exception('message'); 
     } 
     foreach ($dataDecode->location as $location) { 
      //getdata 
      if ($something_is_wrong){ 
      throw new \Exception('message'); 
      } 
      foreach ($dataDecode->machine_1 as $machine_1) { 
      //get and set data 
      if ($something_is_wrong){ 
      throw new \Exception('message'); 
      } 
      } 
      foreach ($dataDecode->machine_2 as $machine_2) { 
      //get and set data 
      if ($something_is_wrong){ 
       throw new \Exception('message'); 
      } 
      } 
      foreach ($dataDecode->machine_3 as $machine_3) { 
      //get and set data 
      if ($something_is_wrong){ 
       throw new \Exception('message'); 
      } 
      } 
     } 
     //These lines of code will only execute if no Exception is thrown: 
     $em->createQuery('DELETE FROM AppBundle:Machine_1')->execute(); 
     $em->createQuery('DELETE FROM AppBundle:Machine_2')->execute(); 
     $em->createQuery('DELETE FROM AppBundle:Machine_3')->execute(); 
     } catch (\Exception $e){ 
     //These lines will only execute if something went wrong 
     } 
    } 
+0

私が受け取るデータはJSONファイルです。複数の場所を持つことができ、複数のマシンを持つことができます。例えば4人の顧客を受け取った場合、foreachが数回実行されることがあります。だから私は顧客のforeachが完了し、すべてがうまくいったときに切り捨てる必要があります。 – Dygne

+1

あなたのニーズに合わせてこのアイデアを適応させることができると思います。 –

+0

私にヒントを教えてもらえますか? APIサービスからの新しいデータを挿入する前に、テーブルを1回だけ切り捨てる必要があります。 – Dygne

関連する問題