[英]Update data to related tables yii2
I have table USR
and ORGANIZATION
: 我有表
USR
和ORGANIZATION
:
USR table USR表
|ID | ID_APP | NAMA_APP|
ORGANIZATION table 组织表
|ID | NAMA |
I tried to insert data to USR
table (ID_APP and NAMA_APP) from ORGANIZATION
by ID (relationship). 我试图通过ID(关系)将数据从
ORGANIZATION
插入USR
表(ID_APP和NAMA_APP)。 This my code : 这是我的代码:
UsersController : 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,
]);
}
}
My View : 我的观点 :
<?php $org = \app\models\ORGANIZATION::find()->all(); ?>
<?= $form->field($model, 'ID_APP')->dropDownList(
ArrayHelper::map($org,'ID', 'NAMA'))->label('ID APP') ?>
I'm still beginner on php
and yii2
framework How to get NAMA_APP ( USR
table) from NAMA ( ORGANIZATION
)? 我仍然是
php
和yii2
框架的初学者。如何从NAMA( ORGANIZATION
)获取NAMA_APP( USR
表)?
Based on you view assumuning that you post the ID_APP and need to retrive the NAMA_APP for a separated store in USR 根据您的假设,假设您发布了ID_APP,并且需要为USR中的单独商店检索NAMA_APP
// 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
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.