2015-05-07 65 views
2
更新頁面

上我有一個CheckBoxList的兩個創建和更新的觀點:的CheckBoxList框將不會出現檢查

<?php echo $form->field($model3, 'options[]')->checkboxList(['CPF' => 'CPF', 'FWL' => 'FWL', 'SDL' => 'SDL', 'CDAC' => 'CDAC']); ?> 

然後我用這個代碼保存CheckBoxList的檢查框爲字符串:

$model3->options = implode(',', $model3->options); 

在更新,我用這個代碼,將其轉換回一個數組:

$model3->options = explode(",", $model3->options); 

我預計應該有根據創建時檢查的內容檢查更新頁面中的複選框。但是複選框列表框仍未選中。

這裏是我的控制器:

public function actionCreateNewStaff() 
{ 
    $session = Yii::$app->session; 
    if($session['user_type']=='SuperAdmin'){ 
     $model1 = new User(); 
     $model2 = new UserModule(); 
     $model3 = new Employee(); 


     if($model1->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model3->load(Yii::$app->request->post())){ 
     $model1->user_type = 'BizStaff'; 
     $model1->status = 1; 
     date_default_timezone_set('Asia/Manila'); 
     $model1->date_added = date('Y/m/d', time()); 
     $model1->avatar = UploadedFile::getInstance($model1, 'avatar'); 
     $model1->creator_id = Yii::$app->user->identity->_id; 
     $model1->parent = $session['store_owner']; 
     $model1->password = sha1($model1->password); 


      if($model1->save()){ 
      $model2->user_id = $model1->_id; 
      $model2->save(); 

      $model3->user_id = $model1->_id; 
      if(isset($model3->options)){ 
      $model3->options = implode(',', $model3->options); 
      } 
      $model3->save(); 

      Yii::$app->session->setFlash('success', "Success!"); 
      } 
      else{ 
      Yii::$app->session->setFlash('error', "User Registration Failed!"); 
      } 


     return $this->redirect(['managestaff']); 

     } else {    
      return $this->renderAjax('createstaff', [ 
       'model1' => $model1, 
       'model2' => $model2, 
       'model3' => $model3, 
      ]); 
     } 
    }else{ 
     return $this->goHome(); 
    } 

} 

public function actionUpdateEmployee($id) 
{ 

    $session = Yii::$app->session; 
    $model1 = $this->findModel($id); 
    $password = $model1->password; 
    $model2 = UserModule::find()->where([ 'user_id' => new \MongoId($id) ])->one(); 
    $model3 = Employee::find()->where([ 'user_id' => new \MongoId($id) ])->one(); 
    if(isset($model3->options)){ 
     $model3->options = explode(",", $model3->options); 
    } 
    $username = $model1->username; 

    if($model1->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model3->load(Yii::$app->request->post())) { 

     if($model1->password != $password) 
     $model1->password = sha1($model1->password); 
     else 
     $model1->password = $password; 

     date_default_timezone_set('Asia/Manila'); 
     $model1->date_added = date('Y/m/d', time()); 


     $model1->save(); 
     if($model1->save()){ 
      $model2->user_id= $model1->_id; 
      $count = count($session['modules']); 
      foreach($session['modules'] as $module){ 
      if(is_int($model2->{$module})){ 
       $model2->{$module} = null; //unchecked 
      } 
      else{ 
       $model2->{$module}= '1'; //checked 
      } 
      } 
      $model2->save(); 
      if(isset($model3->options)){ 
      $model3->options = implode(',', $model3->options); 
      } 
      $model3->save(); 
      Yii::$app->session->setFlash('success', "User successfully updated!"); 
      return $this->redirect(['viewemployee', 'id' => (string)$model1->_id]); 

     } 
    } else { 
     return $this->render('updateemployee', [ 
      'model1' => $model1, 
      'model2' => $model2, 
      'model3' => $model3, 
     ]); 
    } 
} 

如何顯示我的更新頁面上的這些檢查箱子?

+0

你能告訴我們你的控制器嗎? implode和explode被調用的地方。看起來類似於我的[問題](http://stackoverflow.com/questions/29767947/yii2-how-to-map-a-csv-string-in-an-attribute-to-checkboxlist-in-a-form) 。 – robsch

+0

編輯我的問題。 – kaynewilder

+0

創建後你的db中有什麼?值是否存儲? – robsch

回答

1

經過一些嘗試我的猜測是,你只需要在ActiveField更改屬性名稱:

//from 
<?php echo $form->field($model3, 'options[]')->checkboxList([....]); ?> 

//to 
<?php echo $form->field($model3, 'options')->checkboxList([....]); ?> 

我希望這是正確的。你也應該做的是在內爆:

$model3->options = empty($model3->options) ? '' : implode(',', $model3->options); 

因爲它可能是沒有選中複選框。然後一個空字符串將被髮送到服務器。

+0

它的工作!謝謝。 – kaynewilder