将数据更新到相关表yii2

时间:2016-12-24 03:23:25

标签: php mysql yii2

我有表USRORGANIZATION

USR表

|ID | ID_APP | NAMA_APP|

组织表

|ID | NAMA |

我尝试通过ID(关系)从USRORGANIZATION表(ID_APP和NAMA_APP)插入数据。这是我的代码:

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(USR)获取NAMA_APP(ORGANIZATION表)?

1 个答案:

答案 0 :(得分:0)

基于您的观点,假设您发布了ID_APP并需要在USR中为分离的商店检索NAMA_APP