2016-03-01 179 views
0

我試圖通過這些模型保存數據集。PHP:將foreach數據保存在foreach中

模型1:服務

生成一系列服務日期。

模型2:響應

對於每個服務的條目,用戶需要增加的響應的定數。

所以,我想保存多個服務條目以獲得多個響應。代碼相當自我解釋,但如果需要,我可以添加更多信息。我找不到一個簡單的解決方案來做到這一點,儘管我覺得這很簡單。

if (isset($_POST['Service'], $_POST['Response'])) 
    { 

     $model->attributes = $_POST['Service']; 
     $valid = $model->validate(); 
     // date manipulation 
     $start = new DateTime($model->date_booked); 
     $start->format('Y-m-d'); 
     $end = new DateTime($model->end_date); 
     $end->format('Y-m-d'); 
     $interval = DateInterval::createFromDateString($model->interval); 
     $range = new DatePeriod($start, $interval, $end); 

     if ($valid) 
     { 
      foreach ($range as $key => $value) 
      { 
       $schedule = new Service; 
       $schedule->attributes = $_POST['Service']; 
       $schedule->date_booked = $value->format('Y-m-d'); 
       // If I were to save here, the following, Response Model 
       // will not be validated! 
       // $schedule->save(); 


       foreach ($_POST['Response'] as $j => $k) 
       { 
         $response[$j] = new Response; 
         $response[$j]->attributes = $_POST['Response'][$j]; 
         // If I were to save the Service Models here, 
         // evidently, entries are doubled up! 
         // $service->save(); 

         $response[$j]->service_id = $service->id; 
         $valid = $response[$j]->validate() && $valid; 
         // $response[$j]->save(); 


       } 
      } 
     } 
    } 

謝謝!

+1

首先你爲什麼使用嵌套的foreach?嵌套的foreach在php中存在問題。當你想要嵌套循環時,你的代碼應該更好地使用for循環的基本結構。 – Greg

+0

@Greg,我已經使用了foreach,因爲我需要它來訪問DatePeriod。請告知,如果你知道另一種方式。 – Glicious

回答

1

我不得不通過另一個foreach循環來運行。是的,我確實覺得我在循環迭代,所以如果有人有另一個優雅的解決方案,通過一切手段分享給我。 :-)

現在就完成了。

if (isset($_POST['Service'], $_POST['Response'])) 
    { 
     // Assign and validate Service mcrypt_module_is_block_algorithm 
     $model->attributes = $_POST['Service']; 
     $valid = $model->validate(); 
     // date manipulation 
     $start = new DateTime($model->date_booked); 
     $start->format('Y-m-d'); 
     $end = new DateTime($model->end_date); 
     $end->format('Y-m-d'); 
     $interval = DateInterval::createFromDateString($model->interval); 
     $range = new DatePeriod($start, $interval, $end); 

     // Assign and Validate Response Populated questions 
     foreach ($_POST['Response'] as $j => $k) 
     { 
       $response[$j] = new Response('populate'); // populate scenario 
       $response[$j]->attributes = $_POST['Response'][$j]; 
       $valid = $response[$j]->validate() && $valid; 
     } 

     if ($valid) 
     { 
      foreach ($range as $key => $value) 
      { 
       $schedule = new Service; // static model 
       $schedule->attributes = $_POST['Service']; 
       $schedule->date_booked = $value->format('Y-m-d'); 
       $schedule->save(); 

       foreach ($_POST['Response'] as $x => $y) 
       { 
         $response[$x] = new Response('populate'); // populate scenario 
         $response[$x]->attributes = $_POST['Response'][$x]; 
         $response[$x]->service_id = $schedule->id; 
         $response[$x]->save(); 

       } 
      } 
     } 
    } 
+0

你有沒有找到任何解決方案?即使即時通訊面臨同樣的問題。 –

+0

@SalmanRiyaz - 我沒有,我仍然使用相同的代碼,當我將應用程序遷移到Yii2時,代碼會發生變化。我正在進行遷移前的規劃過程。所以,當我到達這個區域時,我會提供一個更優雅的方式來做到這一點。 – Glicious

+0

@ Glicious-好的,我仍然有這個問題。只有第一個 - 每個數據都得到保存。而不是每個數據的第二個和第三個。任何幫助,將不勝感激。我的意思是我有 - 每個內部爲每個..這裏面爲每個數據沒有得到第二次保存。 –