2016-03-01 176 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-好的,我仍然有这个问题。只有第一个 - 每个数据都得到保存。而不是每个数据的第二个和第三个。任何帮助,将不胜感激。我的意思是我有 - 每个内部为每个..这里面为每个数据没有得到第二次保存。 –