2016-12-24 79 views
0

我有表USRORGANIZATION更新数据相关的表yii2

USR表

|ID | ID_APP | NAMA_APP| 

ORGANIZATION表

|ID | NAMA | 

我试图将数据插入到USR表(ID_APP和NAMA_APP )通过ID(关系)从ORGANIZATION。这是我的代码:

UsersController:

public function actionUpdate($id) 
    { 
     $model = $this->findModel($id); 

     if (Yii::$app->request->post()) { 
      try { 
       $state = true; 
       $data = Yii::$app->request->post(); 
       $transaction = Yii::$app->db->beginTransaction(); 
       $model->ID_APP = $data['USR']['ID_APP']; 
       $model->NAMA_APP = $data['USR']['NAMA_APP']; 

       if (!$model->save()) { 
        $ErrorMessage = $model->getErrorMessage($model->getErrors()); 
        throw new Exception($ErrorMessage); 
       } 
       $transaction->commit(); 
       $message = "Success update Application "; 
      } catch (Exception $e) { 
       $state = false; 
       $transaction->rollback(); 
       $message = $e->getMessage(); 
      } 
      if ($state) { 
       Yii::$app->session->setFlash('successApplication', $message); 
       return $this->redirect(['view', 'id' => $model->ID]); 
      } else { 
       Yii::$app->session->setFlash('errorApplication', $message); 
       return $this->render('view', ['id' => $model->ID]); 
      } 

     } else { 
      return $this->render('update', [ 
       'model' => $model, 
      ]); 
     } 
    } 

笔者认为:

<?php $org = \app\models\ORGANIZATION::find()->all(); ?> 
       <?= $form->field($model, 'ID_APP')->dropDownList(
        ArrayHelper::map($org,'ID', 'NAMA'))->label('ID APP') ?> 

我还在初学的phpyii2框架,如何从非农产品市场准入得到NAMA_APP(USR表)(ORGANIZATION) ?

回答

0

根据您浏览assumuning你发布ID_APP,需要以检索该NAMA_APP的分离店USR

// make accessible your ORGANIZAZTION model with use eg: 
    // use common\models\ORGANIZATION; or 
    use yourapp\models\ORGANIZATION; 

    public function actionUpdate($id) 
     { 
      $model = $this->findModel($id); 

      if (Yii::$app->request->post()) { 
       try { 
        $state = true; 
        $data = Yii::$app->request->post(); 
        $transaction = Yii::$app->db->beginTransaction(); 
        $model->ID_APP = $data['USR']['ID_APP']; 

        // then you can use eg: the find method 
        // related to ORGANIZAZTION active record 

        $modelOrg = ORGANIZATION::find()->where(['ID_APP']=> $data['USR']['ID_APP'])->one(); 
        $model->NAMA_APP = $modelOrg->NAMA_APP 

        ....