在yii2中从另一种形式的相关模型填充一种形式的外键字段

时间:2016-12-14 12:28:22

标签: php mysql yii2

我有两个相关的表(模型)[ Sub 主键id]和[ Case 外键sub_id]。我用id=4创建了Sub。我想在子模型的view.php(窗体)中创建案例模型的数据。我做了一个"创建案例"按钮,它引用Case模型的actionCreate

这是我的"创建案例" sub / view.php中的按钮:

<?= Html::a(Yii::t('app','Create Case'), ['/case/create', 'sub_id' => $model->id], ['class' => 'btn btn-primary']) ?>

看起来像是在 picture

这个按钮引用了我对Case模型的创建形式,我应该得到字段sub_id = 4.现在我的_form.php有

<?= $form->field($model, 'sub_id')->textInput() ?>

我应该更改什么才能获得ID为父模型的自动填充字段sub_id?

更新:我从相应的视图中添加了相关代码,即控制器文件。 我没有改变模型文件。

CaseController.php文件如下所示

class CaseController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    public function actionIndex()
    {
        $searchModel = new CaseSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    public function actionCreate($sub_id)
    {
        $model = new Case();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'parent' => $sub_id
            ]);
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }
    protected function findModel($id)
    {
        if (($model = Case::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}

sub / view.php文件:

<?php
use yii\helpers\Html;
use yii\widgets\DetailView;

$this->title = $model->id . ": " . $model->fullname;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Subs'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sub-view">

<h3><?= Html::encode($this->title) ?></h3>

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        'address_id',
        'address.region.name',
        [
            'label' => 'address',
            'value' => 'Street: ' . $model->address->street . ' House ' . $model->address->house . ' Flat ' . $model->address->flat
        ],
    ],
]) ?>

<p>
    <?= Html::a(Yii::t('app', 'Create Case'), ['/case/create', 'sub_id'=>$model->id], ['class' => 'btn btn-success']) ?>
</p>

</div>

case / _form.php文件:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

<div class="case-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'id')->textInput() ?>

<?php if($model->isNewRecord && isset($parent_id)) {
    $model->sub_id = $parent_id;
} ?>

<?= $form->field($model, 'sub_id')->textInput(['readonly' => true, 'value' => $model->sub_id]) ?>

<?= $form->field($model, 'case_date')->textInput() ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>

1 个答案:

答案 0 :(得分:1)

由于缺乏任何进一步的信息,据我所知,这就是你所要求的 -

以图片为例,如果用户点击“创建案例”按钮,则会打开一个新表单(“创建案例”)。在该Create Case表单中,除了其他输入字段外,还有一个sub_id字段,默认情况下应填充值4(因为在图片中,用户Harry Potter的ID为4)。

基于以上所述,您只需要执行以下操作 - 在您的操作中(在创建案例的CaseController内),您可以传递sub_id,如下所示 -

/* ** CaseController ** */
public function actionCreate($sub_id) 
{
    //....other code

    return $this->render('create', ['model' => $model,'parent_id' => $sub_id]);
}

然后在显示“创建案例”表单的_form.php内,您只需这样做 -

/* ** _form.php ** */
//... other code

//if you are using _form.php for Edit Form as well, 
//this prevents the value from the DB being over-written
if($model->isNewRecord && isset($parent_id)) {
    $model->sub_id = $parent_id;
}
<?= $form->field($model, 'sub_id')->textInput() ?>

//... other code

这应该足以显示从父表单传递的值。