2017-05-25 104 views
0

我有兩個表格:工作站和路由。該工作站表具有列(station_id,address,office_hours,date_create),Routes表包含列(routes_id,from_id,destination_id)。路由表上的from_id和destination_id是引用站點表的外鍵。Yii 2:使用保存的數據表的值將數據保存到一個表後填充另一個表

現在我想要做的是無論何時添加一個電臺,電臺的路線都是使用已經在電臺中的電臺計算出來的。例如,假設我們已經在車站表中有車站A.將站點B的結果添加到兩個路由中,A→B和B→A,從而路由A→B,A =>而B =>目的地,因此他們的ID僅在路由表中被挑選和填充因此。

我已經在車站控制器中嘗試了以下代碼,但是我沒有收到任何成功。該代碼是:

StationsController:

public function actionCreate() 
{ 
    $model = new Stations(); 

    $routesModel = new Routes(); 

    //checking whether we are getting the logged in user id value 
    Yii::info("User id=".Yii::$app->user->id); 

    $model->registered_by_id = Yii::$app->user->id; 

    $model->status = 10; 

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 

     //checking here the saved user id value in table 
     Yii::info("checking User id after saving model=".$model->registered_by_id); 

     $this->createRoutes($model->station_id); 

     return $this->redirect(['view', 'id' => $model->station_id]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

public function createRoutes($id) 
{ 
    $model = new Routes; 

    $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id); 
    $all_stations = $command->queryAll(); 

    foreach ($all_stations as $new_route) { 
     $from_id = $id; 
     $destination_id = $new_route; 
     $model->load(Yii::$app->request->post()) && $model->save(); 
     $from_id = $new_route; 
     $destination_id = $id; 
     $model->load(Yii::$app->request->post()) && $model->save(); 
    } 

    return; 
} 

該站表被填充,但是沒有填充路由表。但是,我沒有收到任何錯誤。我哪裏可能會出錯?

請儘可能地協助您。

修訂功能createRoutes()

public function createRoutes($id) 
{ 
    $model = new Routes; 

    $count = (new \yii\db\Query())->from('stations')->count(); 

    if($count > 1) { 
     $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id); 
     $all_stations = $command->queryAll(); 

     foreach ($all_stations as $new_route) { 
      $model->from_id = $id; 
      $model->destination_id = $new_route['station_id']; 
      $model->save(); 
      $model->from_id = $new_route['station_id']; 
      $model->destination_id = $id; 
      $model->save(); 
     } 
     return; 
    } 
    else 
     return; 
} 

現在,讓上述更改後,只保存到數據庫的單一路線,但有幾個站,因此幾條路應創建。如果我有10個電臺,它只會爲9到10電臺生成路由;我的假設是它只保存了foreach外觀的最後一部分,在其他情況下,不會調用$ model-> save()參數。我在這裏做錯了什麼?

回答

0

在您的createRoutes方法中,您只創建一個Routes模型並填充並將其保存在循環中,這就是爲什麼您只能獲取一條記錄的原因。相反,你只需要像這樣創建內部的foreach新模式:

public function createRoutes($id) 
{ 
    //$model = new Routes; not here 

    // $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id); 
    // $all_stations = $command->queryAll(); 

    // I am using active record instead of single query, you can do anything you want here 
    $all_stations = Stations::find() 
       ->where('station_id != :id', [':id' => $id]) 
       ->all(); 

    foreach ($all_stations as $new_route) { 
     $model = new Routes; // create record here 
     $model->from_id = $id; 
     $model->destination_id = $new_route->station_id; 
     // no need to load because these data are not in request param 
     // $model->load(Yii::$app->request->post()) && $model->save(); 
     $model->save(); 

     $model = new Routes; // and another one 
     $model->from_id = $new_route->station_id; 
     $model->destination_id = $id; 
     $model->save(); 
    } 

    return; 
} 

重構版本: 以上應該做的伎倆,但這裏是另一種優化。

Routes模型

創建一個靜態方法是這樣的:

public static function create($data) 
{ 
    $model = new self; 
    $model->from_id = $data['from_id']; 
    $model->destination_id = $data['destination_id']; 
    return $model->save(); 
} 

然後重寫createRoutes方法是這樣的:

public function createRoutes($id) 
{ 
    $all_stations = Stations::find() 
       ->where('station_id != :id', [':id' => $id]) 
       ->all(); 

    foreach ($all_stations as $new_route) { 
     Routes::create([ 
      'from_id' => $id, 
      'destination_id' => $new_route->station_id, 
     ]); 

     Routes::create([ 
      'from_id' => $new_route->station_id, 
      'destination_id' => $id, 
     ]); 
    } 

    return; 
} 

而且我們有很多更清潔的版本了。

+1

感謝您的解決方案@leninhasda,我修改了建議的代碼,它像魅力一樣工作。事實上,我已經注意到,與以前不同,插入值的時間更短。 **雖然有一個更正** 在'createRoutes()'函數的最後一部分中,您有一個使用'Rooutes ::'而不是'Routes ::'的錯字。我試圖編輯,但它重新發布我早些時候發佈的答案。 否則,非常感謝。 – japheth

+0

你能回答我以前問過的其他問題嗎? [鏈接](https://stackoverflow.com/questions/44164746/yii2-admin-rbac-get-roles-from-database-and-display-as-dropdownlist-on-signup-u) – japheth

+0

啊......很好趕上@ japheth和感謝錯字報告,現在修復它。我會考慮你的鏈接,如果我能,我一定會嘗試回答它。 – leninhasda

0

也許Yii :: $ app-> request-> post()在函數createRoules中不起作用,因爲它不是一個動作函數,但是您可以使用$ model-> getErrors()來查找錯誤模型

+0

感謝您的洞察力@vasillis,我已經修改了代碼,直到它現在正在工作。問題在於,不是插入到數據庫的所有路由,而只是插入foreach循環的最後一個路由,而不是插入所有其他生成的路由;因此只插入一條路線。讓我更新上面的代碼。 – japheth

+0

最後,我可以將$ model->命令更改爲 $ sql ='INSERT INTO routes(from_id,destination_id)VALUES('。$ from_id。','。$ destination_id。')'; Yii :: $ app-> db-> createCommand($ sql) - > execute(); – japheth

0

我知道這是如此粗糙,意味着很多系統在查詢時間方面,但我拼命尋找答案。所以到目前爲止,這對我來說已經奏效。

public function actionCreate() 
{ 
    $model = new Stations(); 

    $routesModel = new Routes(); 

    //checking whether we are getting the logged in user id value 
    Yii::info("User id=".Yii::$app->user->id); 

    $model->registered_by_id = Yii::$app->user->id; 

    $model->status = 10; 

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 

     //checking here the saved user id value in table 
     Yii::info("checking User id after saving model=".$model->registered_by_id); 

     $this->createRoutes($model->station_id); 

     return $this->redirect(['view', 'id' => $model->station_id]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

public function createRoutes($id) 
{ 
    $model = new Routes; 

    $count = (new \yii\db\Query())->from('stations')->count(); 

    if($count > 1) { 
     $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id); 
     $all_stations = $command->queryAll(); 

     foreach ($all_stations as $new_route) { 
      $from_id = $id; 
      $destination_id = $new_route['station_id']; 
      $sql = 'INSERT INTO routes(from_id, destination_id) VALUES('.$from_id.','.$destination_id.')'; 
      Yii::$app->db->createCommand($sql)->execute(); 
      // $model->save(); 
      $from_id = $new_route['station_id']; 
      $destination_id = $id; 
      $sql = 'INSERT INTO routes(from_id, destination_id) VALUES('.$from_id.','.$destination_id.')'; 
      Yii::$app->db->createCommand($sql)->execute(); 
      // $model->save(); 
     } 
     return; 
    } 
    else 
     return; 
} 

我仍然歡迎更好的解決方案,特別是使上述如此高效。感謝帥哥。

相關問題