从YII2中的另一种相关模型中以一种形式填充外国密钥字段



我有两个相关的表(型号)[ sub 带有主键id]和[ case 带有外键sub_id]。我用id=4创建了子。我想在 sub Model 的View.php(form)中创建案例模型的数据。我做了一个"创建案例"按钮,该按钮指的是案例模型的actionCreate

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

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

看起来像图片

此按钮将我转介到案例模型的创建形式,其中我应该获取字段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 yiihelpersHtml;
use yiiwidgetsDetailView;
$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>

案例/_form.php文件:

<?php
use yiihelpersHtml;
use yiiwidgetsActiveForm;
<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>

由于缺乏任何进一步的信息,就我的理解而言,这就是您要问的 -

以图片中的示例为例,如果用户单击创建案例按钮,则将打开一个新表单(创建案例)。在该创建情况形式中,除其他输入字段外,还有一个用于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

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

相关内容

最新更新