0

我正在维护一个带有模型和子记录网格的表单。我希望网格显示孩子的网址,而不是父母的网址。

我有两个共享父子关系的数据库表。我只展示了重要的领域。

describe ops;
+---------------+--------------+
| serial_number | int(11)      |
+---------------+--------------+
describe opsitem;
+---------------+--------------+
| opsitem_id    | int(11)      |
| ops_id        | int(11)      |   # foreign key
| serial_number | int(11)      |
+---------------+--------------+

在我的控制器中,我为 Ops 显示一个表单,然后为子记录创建一个 activeRecord - Opsitem

class OpsController extends Controller

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


        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->ops_id]);
        } else {

            $searchModel = new OpsitemSearch();
            $dataProvider = $searchModel->search(
                ['OpsitemSearch' => ['ops_id' => $model->ops_id]]
            );

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

我的表单包含 ops(父)的表单字段,然后是包含 oppsitem(子)记录的网格

// $model is Parent - Ops
<?php echo $this->render('_form', [
    'model' => $model,
 ]) ?>
 // $searchModel is Opsitem - Child
<?php echo GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                'ops_item_id',
                'ops_id',
                'serial_number'
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{update}{delete}',
                ],
            ],
        ]); ?>

单击网格上的“更新”按钮指向我url

/ops/update?id=1234

我想

/opsitem/update?id=1234

4

2 回答 2

2

您需要controller为更改控制器添加属性。就像,

<?php echo GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                'ops_item_id',
                'ops_id',
                'serial_number'
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{update}{delete}',
                    'controller' => 'opsitem', 
                ],
            ],
        ]); ?>
于 2015-11-07T09:22:30.930 回答
1

您可以自定义gridview按钮。例如,

'template' => '{update} {delete}',
'buttons' => [
            'update' => function ($url, $model) {
                  return Html::a('Update',\Yii::$app->getUrlManager()->createUrl(['/opsitem/update', 'id' => 1234]),['class' => 'any class']);
                    },
            ],  
于 2015-11-07T07:18:29.350 回答